26 April, 2012

Matrix Transpose


public class Matrixtrans
{
public static void main (String[] args)
{
int [] [] a ={ {1,2,3} , {4,5,6} , {7,8,9} };
int m=3, n=3;
int [] [] c = new int [m][n];

for (int i=0; i<=m-1; i++)
{       for(int j=0; j<=n-1; j++)
            c[j][i]=a[i][j];
}

for (int i=0; i<=m-1; i++)
{     
      for(int j=0; j<=n-1; j++)
            System.out.printf("%d\t",c[i][j]);
      System.out.println();
}

}
}

19 April, 2012

Process Builder Demo

import java.io.IOException;

public class ProcessBuilderDemo
{
  public static void main(String args[]) throws IOException
  {
    ProcessBuilder probldr = new ProcessBuilder("notepad.exe", "rajpootfile");
    probldr.start();
  }
}

16 April, 2012

GridBag Layout Example

import java.awt.*;
import javax.swing.*;

public class GridBagLayoutTester
{
  final static boolean shouldFill = true;
  final static boolean shouldWeightX = true;
  final static boolean RIGHT_TO_LEFT = false;

  public static void addComponentsToPane(Container pane)
  {
    if (RIGHT_TO_LEFT)
    {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    if (shouldFill)
    {
      gbc.fill = GridBagConstraints.HORIZONTAL;        // natural height, maximum width
    }
    button = new JButton("Button 1");
    if (shouldWeightX)
    {
      gbc.weightx = 0.5;
    }
    gbc.gridx = 0;
    gbc.gridy = 0;
    pane.add(button, gbc);

    button = new JButton("Button 2");
    gbc.gridx = 1;
    gbc.gridy = 0;
    pane.add(button, gbc);

    button = new JButton("Button 3");
    gbc.gridx = 2;
    gbc.gridy = 0;
    pane.add(button, gbc);

    button = new JButton("Long Button 4");
    gbc.ipady = 40;                    // make this component tall
    gbc.weightx = 0.0;
    gbc.gridwidth = 3;
    gbc.gridx = 0;
    gbc.gridy = 1;
    pane.add(button, gbc);

    button = new JButton("5");
    gbc.ipady = 0;                    // reset to default
    gbc.weighty = 1.0;                // request any extra vertical space
    gbc.anchor = GridBagConstraints.PAGE_END;        // bottom of space
    gbc.insets = new Insets(10, 0, 0, 0);        // top padding
    gbc.gridx = 1;                    // aligned with button 2
    gbc.gridwidth = 2;                // 2 columns wide
    gbc.gridy = 2;                    // third row
    pane.add(button, gbc);
  }

  private static void createAndShowGUI()
  {
    JFrame frame = new JFrame("GridBag Layout");    // Create and set up the window.
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addComponentsToPane(frame.getContentPane());    // Set up the content pane.
    frame.pack();                    // Display the window.
    frame.setVisible(true);
  }

  public static void main(String[] args)
  {
    javax.swing.SwingUtilities.invokeLater( new Runnable() { public void run() {createAndShowGUI();} }  );
  }
}



OUTPUT -:


12 April, 2012

Drag and Drop between JTextArea and JTextField

import javax.swing.*;

public class DnDBetweenJTextAreaAndJTextFieldDemo
{

  public static void main(String[] args)
  {

    JFrame f = new JFrame("Drag and Drop");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new JPanel());
    JTextField tf = new JTextField(25);
    tf.setText("http://jaisinghrajpoot.blogspot.in/");
    f.add(tf);

    JTextArea ta = new JTextArea(4, 25);
    ta.setText("Demonstrating\ndrag and drop");
    f.getContentPane().add(new JScrollPane(ta));
    ta.setDragEnabled(true);
    tf.setDragEnabled(true);
    f.pack();
    f.setVisible(true);

  }

}

07 April, 2012

Delete All Files From Directory

import java.io.File;
public class DeleteFilesInDirectory
{
//********************************************

public static void DeleteFilesInDirectory(String DirectoryName)
{
String dirname = DirectoryName;
File f1 = new File(dirname);
if (f1.isDirectory())
{
    System.out.println("Directory of " + dirname);
    String s[] = f1.list();
    for (int i=0; i <s.length; i++)
    {
    File f = new File(dirname + "/" + s[i]);
    if (f.isDirectory())
    {
          System.out.println(s[i] + " is a directory in " + dirname);
    }
    else
    {
          //System.out.println(s[i] + " is a file");
          File d1=new File("Data\\"+s[i]);
          d1.delete();
    }
     }
}
else
{
    System.out.println(dirname + " directory is not found");
}

}
//********************************************

public static void main(String args[])
{
String DirName="Data";
DeleteFilesInDirectory(DirName);
}

//********************************************
}