I have created a form and I added a JFileChooser on it.
When I start the form , the JFileChooser appears when the form loads.
I gave used netbeans fileChooser tutorial.(http://netbeans.org/kb/docs/java/gui-filechooser.html)
Any ideas?
Thank you.
skiabox 0 Light Poster
Dani AI
Generated
Short explanation: the JFileChooser was placed into the JFrame’s visible layout by the GUI builder, so it’s treated like any other component and is shown when the frame is displayed. was right to question calling the chooser from the constructor/event timing, and ’s hint about the chooser being added to the window points to the real cause. Moving the chooser out of the content pane (or not adding it to the GroupLayout) prevents it from appearing with the main form.
Two safe approaches:
- Keep a non-visual chooser in the form (NetBeans’ Other Components) and call its dialog methods from the menu action. This keeps the chooser managed by the form but not laid out visibly.
- Create a fresh JFileChooser inside the actionPerformed handler and call showOpenDialog/showSaveDialog on demand. This is simplest and avoids designer-layout issues.
When replacing the display panel with the chart, reassigning the mainPanel reference won’t update the GUI. The visual container itself must be updated and refreshed, for example:
mainPanel.removeAll();
mainPanel.add(chart.myPanel);
mainPanel.revalidate();
mainPanel.repaint(); Additional notes: avoid long file-processing on the Event Dispatch Thread — use a SwingWorker for IO or parsing. If using the GUI builder, drop the chooser outside the form area so NetBeans places it under Other Components. Finally, prefer invoking the chooser as a dialog for typical open/save workflows rather than embedding it in the main layout.
Recommended Answers
Jump to Post— Majestics 84Please paste ur code... It seems u are calling file chooser in constructor rather then in actionPerformed Method. Still itsnt clear until you paste your code.
Jump to Post— JamesCherrill 4,733Probably because on line 102 you add the file chooser to your window?
All 5 Replies
Majestics 84 Posting Pro
Please paste ur code... It seems u are calling file chooser in constructor rather then in actionPerformed Method. Still itsnt clear until you paste your code.
skiabox 0 Light Poster
Here is the code :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package nysemarketpick;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
/**
*
* @author skiabox
*/
public class MainForm extends javax.swing.JFrame {
/**
* Creates new form MainForm
*/
public MainForm() {
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() {
fileChooser = new javax.swing.JFileChooser();
mainPanel = new javax.swing.JPanel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
Open = new javax.swing.JMenuItem();
Exit = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenu3 = new javax.swing.JMenu();
jMenu4 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Nyse Stock Pick");
setSize(new java.awt.Dimension(1920, 1080));
fileChooser.setDialogTitle("This is my open dialog");
org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 1070, Short.MAX_VALUE)
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 621, Short.MAX_VALUE)
);
jMenu1.setText("File");
Open.setText("Open");
Open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
OpenActionPerformed(evt);
}
});
jMenu1.add(Open);
Exit.setText(" Exit");
Exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ExitActionPerformed(evt);
}
});
jMenu1.add(Exit);
jMenuBar1.add(jMenu1);
jMenu2.setText("Options");
jMenuBar1.add(jMenu2);
jMenu3.setText("Tools");
jMenuBar1.add(jMenu3);
jMenu4.setText("Help");
jMenuBar1.add(jMenu4);
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(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(0, 266, Short.MAX_VALUE)
.add(fileChooser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 266, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(mainPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(43, Short.MAX_VALUE))
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(0, 135, Short.MAX_VALUE)
.add(fileChooser, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(0, 135, Short.MAX_VALUE)))
);
pack();
}// </editor-fold>
private void OpenActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fileChooser.getSelectedFile();
//What to do with the file
PriceVolumeChart chart = new PriceVolumeChart(file.getName());
mainPanel = chart.myPanel;
}
else
{
System.out.println("File access cancelled by user.");
}
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem Exit;
private javax.swing.JMenuItem Open;
private javax.swing.JFileChooser fileChooser;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenu jMenu4;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPanel mainPanel;
// End of variables declaration
}
skiabox 0 Light Poster
Netbeans generates much of this code when you add a JFileChooser in a JFrame
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Probably because on line 102 you add the file chooser to your window?
Edited by JamesCherrill
skiabox 0 Light Poster
I've fixed that by adding the fileChooser to Other Components node.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.