Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JFileChooser

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default JFileChooser

    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.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: JFileChooser

    What exceptions?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: JFileChooser

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JMenuItem cannot be cast to javax.swing.JFileChooser
    at Notepad$2.actionPerformed(Notepad.java:70)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unk nown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4

    Default Re: JFileChooser

    Hi, you are wrong when doing this:

    JFileChooser tfc = (JFileChooser) ae.getSource();

    Because the object that generates the event is the menu item, thus this exception:

    java.lang.ClassCastException: javax.swing.JMenuItem cannot be cast to javax.swing.JFileChooser

    There are some useful examples about JFilechooser here and there.

  5. The Following User Says Thank You to nambill For This Useful Post:

    Karthik Prabhu (June 23rd, 2012)

  6. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    20
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: JFileChooser

    Thank u very much....It has worked.

Similar Threads

  1. JFileChooser Please Help
    By mulligan252 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: October 9th, 2011, 11:03 AM
  2. JFileChooser frustrations!
    By chrisivey1980 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 3rd, 2011, 05:32 AM
  3. JFileChooser setting directory
    By fox in forum AWT / Java Swing
    Replies: 2
    Last Post: February 18th, 2011, 11:58 AM
  4. Can't get it to save stuff with JFileChooser
    By javapenguin in forum What's Wrong With My Code?
    Replies: 35
    Last Post: August 11th, 2010, 04:13 AM
  5. JFileChooser
    By FretDancer69 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 3rd, 2010, 06:35 AM