Results 1 to 6 of 6
  1. #1
    letter5 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Post jfilechooser not appearing

    I'm new to java and have a problem with the Jfilechooser. It does not seem to load when i select the button which has the action performed method attached.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Then you've got a bug somewhere. You'll have to provide a lot more information if you want more help. I unfortunately left my mind-reading cap at work.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    At least show your code, what you have done so far. Comes with any error message show it here too.

  4. #4
    letter5 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Post

    Oh Though i attach the code. Here it is.


    Java Code:
    import java.io.File;
    import javax.swing.JFileChooser;
    
    
    public class Main extends javax.swing.JFrame {
        
        //Path name to image file.
        private File theFile ; 
        
        /** Creates new form Main */
        public Main() {
       
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
        private void initComponents() {
            jFileChooser1 = new javax.swing.JFileChooser();
            imageJComponent1 = new cm4062.reusables.ImageJComponent();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jMenu2 = new javax.swing.JMenu();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            imageJComponent1.setMinimumSize(new java.awt.Dimension(500, 500));
            org.jdesktop.layout.GroupLayout imageJComponent1Layout = new org.jdesktop.layout.GroupLayout(imageJComponent1);
            imageJComponent1.setLayout(imageJComponent1Layout);
            imageJComponent1Layout.setHorizontalGroup(
                imageJComponent1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 500, Short.MAX_VALUE)
            );
            imageJComponent1Layout.setVerticalGroup(
                imageJComponent1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 500, Short.MAX_VALUE)
            );
    
            jMenu1.setText("Menu");
            jMenu1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenu1ActionPerformed(evt);
                }
            });
    
            jMenuBar1.add(jMenu1);
    
            jMenu2.setText("Open Image");
            jMenu2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenu2ActionPerformed(evt);
                }
            });
    
            jMenuBar1.add(jMenu2);
    
            setJMenuBar(jMenuBar1);
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(imageJComponent1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(imageJComponent1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
            );
            pack();
        }// </editor-fold>                        
                                       
    
        private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
          int rval = jFileChooser1.showOpenDialog(this);
          if(rval == JFileChooser.APPROVE_OPTION) {
            theFile = jFileChooser1.getSelectedFile();
            imageJComponent1.load(theFile);
    }
    
        }                                      
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Main().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify                     
        private javax.swing.JFileChooser jFileChooser1;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenu2;
        private javax.swing.JMenuBar jMenuBar1;
        // End of variables declaration                   
        
    }
    Last edited by Eranga; 02-15-2009 at 05:44 PM. Reason: Edited code tags

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The code helps a lot; thanks for posting it. I see that you aren't adding the action listener to a JButton at all but rather are adding it to a JMenu.

    I suggest that you either add it to a JButton as you mentioned earlier, or a JMenuItem, and that you read the Sun tutorials on how to code Swing buttons and menus. This will answer most of your current questions and your questions to come. You can find these tutorials here:

    How to use Menus
    How to use Buttons,...

    and for a list of all of the Sun Java tutorials (a gold mine that I recommend you explore as often as possible):
    The Really Big Index

    Good luck and HTH.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. why isn't my combobox appearing on my frame?
    By matpj in forum AWT / Swing
    Replies: 9
    Last Post: 01-29-2009, 04:46 PM
  2. XTerm window appearing in full screen swing app
    By clarose in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2008, 11:56 PM
  3. using jfilechooser
    By jagadhguru in forum AWT / Swing
    Replies: 3
    Last Post: 10-30-2008, 01:17 PM
  4. tomcat resource bundle tab not appearing??
    By freddieMaize in forum NetBeans
    Replies: 2
    Last Post: 05-25-2008, 10:15 AM
  5. Log messages are not appearing in my log file
    By indu_raj in forum Java Servlet
    Replies: 1
    Last Post: 08-09-2007, 12:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •