views:

193

answers:

2

The following code was generated automatically by Netbeans 6.8 Mac version

public class fileBrowser extends javax.swing.JPanel {

/** Creates new form fileBrowser */
public fileBrowser() {
    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.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jFileChooser1 = new javax.swing.JFileChooser();

    setName("Form"); // NOI18N

    jFileChooser1.setName("jFileChooser1"); // NOI18N

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(org.jdesktop.layout.GroupLayout.TRAILING, jFileChooser1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
        .add(jFileChooser1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE)
    );
}// </editor-fold>


// Variables declaration - do not modify
private javax.swing.JFileChooser jFileChooser1;
// End of variables declaration

}

I'm trying to make a button that call it (to allow the user to select a file) with the following code:

 private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
fileBrowser fileBrowser = new fileBrowser();
fileBrowser.setVisible(true);//why not working?

}

Well...when I click the button, I only get an empty form...Any idea where is the bug?

+2  A: 

A JFileChooser is not a component per se, like a button. It's a dialog. So this is working "correctly". Check the JFileChooser Java Doc for how to use JFileChooser.

Will Hartung
+2  A: 

You should not be using a MouseListener for clicking on a button. You should be using an ActionListener.

Read the JFileChooser API and follow the link to the Swing tutorial on "How to Use File Choosers" for a working example of how to display a file chooser. Basically your code will look something like the code in the ActionListener from the example program.

camickr