import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.*; public class Notepad extends JFrame{ /** * @param args */ Notepad() { final JFrame f = new JFrame(); Container cp = f.getContentPane(); final JLabel dl = new JLabel(""); cp.add(dl,BorderLayout.NORTH); final JLabel fl = new JLabel(""); cp.add(fl,BorderLayout.SOUTH); JFileChooser fc =new JFileChooser("."); fc.setControlButtonsAreShown(false); cp.add(fc,BorderLayout.CENTER); //The MenuBar JMenuBar mb = new JMenuBar(); setJMenuBar(mb); //The Menu JMenu m1 = new JMenu("File"); JMenu m2 = new JMenu("Edit"); JMenu m3 = new JMenu("View"); //The Menu Items JMenuItem i1 = new JMenuItem("New"); JMenuItem i2 = new JMenuItem("Open"); JMenuItem i3 = new JMenuItem("Close"); mb.add(m1); mb.add(m2); mb.add(m3); m1.add(i1); m1.add(i2); m1.add(i3); //The TextArea final JTextArea ta = new JTextArea(); add(ta); //The New ActionListener i1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { ta.setText(" "); } }); //The Open ActionListener i2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JFileChooser tfc = (JFileChooser) ae.getSource(); String command = ae.getActionCommand(); if(command.equals(JFileChooser.APPROVE_SELECTION)) { File sf = tfc.getSelectedFile(); dl.setText(sf.getParent()); fl.setText(sf.getName()); } else if(command.equals(JFileChooser.CANCEL_SELECTION)) { dl.setText(" "); fl.setText(" "); } f.pack(); f.setVisible(true); } }); } public static void main(String[] args) { // TODO Auto-generated method stub Notepad m = new Notepad(); m.setSize(500,500); m.setVisible(true); } }
The JFileChooser is not opening when the 'open' is clicked.Instead,it is throwing a series of exceptions.