Monday 2 September 2013

FileDialog box in java


/**
 * @(#)Menu_FileDialog.java
 *
 *
 * @author - Anant Mahale
 * @version 1.00 2013/8/25
 *program for file dialog box
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Menu_FileDialog
{
    public static void main(String args[])
    {
        myframe m=new myframe();
        m.setTitle("Open & Save");
        m.setSize(400,400);
        m.setVisible(true);
        m.setLocation(100,100);
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
    class mypanel extends JPanel implements ActionListener
    {
        JTextArea txta_show;
        JMenu file;
        JMenuItem open,save;
        JMenuBar jmb=new JMenuBar();
        mypanel()
        {
            setLayout(new BorderLayout());
            file=new JMenu("File");
           
            open=new JMenuItem("Open");
            open.addActionListener(this);
           
            save=new JMenuItem("save");
            save.addActionListener(this);
           
            file.add(open);
            file.add(save);
           
            jmb.add(file);
            setJMenuBar(jmb);
           
            txta_show=new JTextArea();
            add(txta_show);
           
        }
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource()==open)
            {
               
                JFileChooser jfc=new JFileChooser();
                jfc.showOpenDialog(this);
            }
            if(ae.getSource()==save)
            {
                JFileChooser jfc=new JFileChooser();
                jfc.showSaveDialog(this);
                   
            }   
        }
    }
}

Demonstration of jlist


/**
 * @(#)Change_Bg.java
 *
 *
 * @author    - Anant Mahale
 * @version 1.00 2013/8/2
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class ChangBackgroundWithList
{
    public static void main(String args[])
    {
        myframe f=new myframe();
        f.setTitle("Jlist program");
        f.setSize(500,500);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class myframe extends JFrame
{
    mypanel p;
    myframe()
    {
        p=new mypanel();
        Container c=getContentPane();
        c.add(p);
    }
    class mypanel extends JPanel implements ListSelectionListener
    {
        JList lst;
        String[] lstColorNamess={ "black", "blue", "green", "yellow","white" };
        mypanel()
        {
           
            lst=new JList(lstColorNamess);
            lst.addListSelectionListener(this);
            add(lst);
        }
        public void valueChanged(ListSelectionEvent lse)
        {
            int i=lst.getSelectedIndex();
            if(i==0)
                setBackground(Color.black);   
                   
            if(i==1)
                setBackground(Color.blue);
               
            if(i==2)
                setBackground(Color.green);   
                   
            if(i==3)
                setBackground(Color.yellow);
               
            if(i==4)
                setBackground(Color.white);       
        }
    }
}