Thursday, April 23, 2015

Finding square root of a value.

class findsqrt
{
  public static void main(String args[])
  {
    int x;
    if(args.length<1)
    {
      System.out.println("java findsqrt ###");
      System.exit(0);
    }
    x=Integer.parseInt(args[0]);
    double up,down,middle=0;
    //Finding the upper range value
    for(up=1;up<=200;up++)
       if(up*up> x) break;
    down=up-1;
    int i;
    for(i=0;i<15;i++)
    {
       middle=(up+down)/2.0;
       if(middle*middle>x)
         up=middle;
       else
         down=middle;
       System.out.println(down +"\t"+ middle +"\t"+ up);
    }
    System.out.println("Sqrt = " + middle);
  }
}

Monday, December 1, 2014

JList Sample







import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class myJList extends JFrame implements ActionListener
{
    String months[]={"January","February","March","April",
            "May","June","July","August","September",
            "October","November", "December"};
    JList jlist1;
    JPanel pane,pane2;
    JScrollPane jsp;
    JButton jbadd,jbdel,jbrall;
    DefaultListModel dlm;
    public myJList()
    {
        super("JList Example");
        setSize(300,200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dlm=new DefaultListModel();
        int i;
        for(i=0;i<months.length;i++)
            dlm.addElement(months[i]);
        jlist1=new JList(dlm);
        jsp=new JScrollPane(jlist1);
        pane=new JPanel();
        pane2=new JPanel();
        jbadd=new JButton("Add");
        jbdel=new JButton("Remove");
        jbrall=new JButton("Remove All");
        pane2.add(jbadd);
        pane2.add(jbdel);
        pane2.add(jbrall);
        jbadd.addActionListener(this);
        jbdel.addActionListener(this);
        jbrall.addActionListener(this);
        pane.setLayout(new BorderLayout());
        pane.add("Center",new JPanel().add(jsp));
        pane.add("North",pane2);
        setContentPane(pane);
       
        setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==jbrall)
        {    dlm.removeAllElements();
        }
        if(e.getSource()==jbdel)
            dlm.removeElementAt(jlist1.getSelectedIndex());
        if(e.getSource()==jbadd)
        {
            String inputValue = JOptionPane.showInputDialog("Please input a value");
            if(!inputValue.equals(""))
            {
                dlm.addElement(inputValue);
            }
        }
    }
    public static void main(String args[])
    {
        new myJList();
    }
}

Sunday, November 30, 2014

Test Input String Palindrone or not. (Palindrone mean symmetrical string)

class palindrone2
{    public static void main(String args[])
    {    if(args.length==0)
            System.out.println("java palindrone YourString");
        else
        {    String st1=new String(args[0]);
            boolean palin=true;
            int i,j=st1.length()-1;
            for(i=0;i<st1.length()/2;i++)
                if(st1.charAt(i)!=st1.charAt(j--))
                {    palin=false;
                    break;
                }
            if(palin)
                System.out.println("Palindrone");
            else
                System.out.println("Not palindrone");
        }
    }
}

Sunday, November 16, 2014

JTable and TableModel

import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;

public class SimpleTableDemo extends JFrame {
    private boolean DEBUG = true;

    public SimpleTableDemo() {
        super("SimpleTableDemo");

        Object[][] data = {
            {"Mary", "Campione",
             "Snowboarding", new Integer(5), new Boolean(false)},
            {"Alison", "Huml",
             "Rowing", new Integer(3), new Boolean(true)},
            {"Kathy", "Walrath",
             "Chasing toddlers", new Integer(2), new Boolean(false)},
            {"Mark", "Andrews",
             "Speed reading", new Integer(20), new Boolean(true)},
            {"Angela", "Lih",
             "Teaching high school", new Integer(4), new Boolean(false)}
        };

        String[] columnNames = {"First Name",
                                "Last Name",
                                "Sport",
                                "# of Years",
                                "Vegetarian"};

        final JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));

        if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(table);
                }
            });
        }

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this window.
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    private void printDebugData(JTable table) {
        int numRows = table.getRowCount();
        int numCols = table.getColumnCount();
        javax.swing.table.TableModel model = table.getModel();

        System.out.println("Value of data: ");
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + model.getValueAt(i, j));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }

    public static void main(String[] args) {
        SimpleTableDemo frame = new SimpleTableDemo();
        frame.pack();
        frame.setVisible(true);
    }
}

Wednesday, October 15, 2014

JFileChooser with multiple FileFilters


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;
class FileTypeFilter extends FileFilter
{
    private String extension;
    private String description;

    public FileTypeFilter(String extension, String description)
    {
        this.extension = extension;
        this.description = description;
    }

    public boolean accept(File file)
    {
        if (file.isDirectory()) {
            return true;
        }
        return file.getName().endsWith(extension);
    }

    public String getDescription()
    {
        return description + String.format(" (*%s)", extension);
    }
}
class JFCtest extends JFrame implements ActionListener
{
    JButton jb1;
    JFileChooser fileChooser;
   
    public JFCtest()
    {
        super("JFileChooser MultiFilters");
        setSize(300,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().add(new JPanel().add(jb1=new JButton("Open")));
        jb1.addActionListener(this);
        setVisible(true);
       

    }
    public void actionPerformed(ActionEvent e)
    {
        fileChooser =new JFileChooser();
        fileChooser.setCurrentDirectory(new File("D:\\myjava"));
        FileFilter jpgFilter = new FileTypeFilter(".jpg", "JPEG Image");
        FileFilter txtFilter = new FileTypeFilter(".txt", "Text File");
        FileFilter javaFilter = new FileTypeFilter(".java", "Java File");

        
        fileChooser.addChoosableFileFilter(jpgFilter);
        fileChooser.addChoosableFileFilter(txtFilter);
        fileChooser.addChoosableFileFilter(javaFilter);
        int result = fileChooser.showOpenDialog(this);
            if (result == JFileChooser.APPROVE_OPTION)
        {
                File selectedFile = fileChooser.getSelectedFile();
                    System.out.println("Selected file: " + selectedFile.getAbsolutePath());
                }
    }
    public static void main(String args[])
    {
        try {
                    // Set System L&F
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }   catch (UnsupportedLookAndFeelException e) {// handle exception
                                  }
                catch (ClassNotFoundException e) {// handle exception
                             }
                catch (InstantiationException e) {// handle exception
                             }
            catch (IllegalAccessException e) {// handle exception
                             }
        new JFCtest();
    }
}

Monday, February 3, 2014

Caps Lock Status Displaying

import java.awt.*;
import java.awt.event.*;
class statuscaps
{
   public static void main(String args[])
   {
      boolean status = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
      if(status)
         System.out.println("You Caps Lock is on");
      else
         System.out.println("You Caps Lock is off");
   }
}

Set Caps Lock on Program

class capslockon
{
   public static void main(String args[])
   {
      java.awt.Toolkit.getDefaultToolkit().setLockingKeyState(java.awt.event.KeyEvent.VK_CAPS_LOCK, Boolean.TRUE);
   }
}