Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

CITC(CE)

Advanced JAVA

PRACTICAL 1
A I M : C re a te a m e nu- ba s e d a pplic a tion us ing Swing whic h ope n s a file dia log box and a llows us e r to se le c t a file from loc a l ha rd driv e s . Dis pla y na m e of the file se le c te d into t e x t box a nd c re a te an e x e c uta ble ja r file for this a pplic a tion. O BJ E CT I V E : To learn imple menta tion of Sw ing components in J ava. SO F T WA RE REQUI RED : J DK 1 .5.0 HA RD WA RE REQUI RED : None K NO WLED G E REQUI RED : Background about the JFC and Swing. How to compile and run programs that use Swing components. T HE ORY / LOG I C :

Swing provides three generally useful top-level container classes: J F r a m e , J D i a l o g , an d J A p p l e t . When using these classes, you should keep these facts in mind: To appear onscreen, every GUI component must be part of a c o n t a i n m e n t hierarchy. Each GUI component can be contained only once. Each top-level container has a content pane that, generally speaking, contains (directly or indirectly) the visible components in that top-level container's GUI. You can optionally add a menu bar to a top-level container.

1 of 4

CITC(CE)

Advanced JAVA

CODE:
import import import import java.awt.*; java.awt.event.*; javax.swing.*; javax.swing.event.*;

class SwingMenu extends JFrame{ Container frameContainer; JTextField tf = new JTextField(50); JMenuBar menubar = new JMenuBar(); JMenu filemenu = new JMenu("File"); JMenuItem open = new JMenuItem("Open"); JMenuItem exit = new JMenuItem("Exit"); public SwingMenu(){ super("Swing Menu"); filemenu.add(open); filemenu.add(exit); menubar.add(filemenu); setJMenuBar(menubar); frameContainer = getContentPane(); frameContainer.setLayout(null); tf.setBounds(100,100,120,30); frameContainer.add(tf); addWindowListener(new WindowHandler()); open.addActionListener(new MenuItemHandler()); exit.addActionListener(new MenuItemHandler()); setSize(300,300); show(); } public static void main(String args[]){ SwingMenu app = new SwingMenu(); } public class WindowHandler extends WindowAdapter{ public void WindowClosing(WindowEvent e){ System.exit(0); } } public class MenuItemHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ String cmd = e.getActionCommand(); if(cmd.equals("Exit")) System.exit(0); else { JFileChooser chooser = new JFileChooser();

2 of 4

CITC(CE)

Advanced JAVA

int returnVal = chooser.showOpenDialog(frameContainer); if(returnVal == JFileChooser.APPROVE_OPTION) { tf.setText(chooser.getSelectedFile().getName()); } } } } }

O UT P UT :
F:/JAVA>java SwingMenu

3 of 4

CITC(CE)

Advanced JAVA

A D VA NT A G ES : Tool tips, Painting and borders, Application-wide pluggable look and feel, Custom properties, Support for layout, Support for accessibility, Support for drag and drop, Double buffering, Key bindings A P P LI CA T I ONS : One can create portable GUI applications using light-weight components. Q UE ST I ONS : Question 1: Which package is required for all Swing applications? Answer 1: javax.swing Question 2: Which method do you use to enable and disable components such as JButtons? What class is it defined in? Answer 2: setEnabled, which is defined in the Component class.

4 of 4

You might also like