Reputation: 1514
Hi I have a java program. I use Eclipse as my tool. Also I have installed Java 7 Update 51, Java SE Development Kit 7 Update 51. My code to open the dialog box for selecting the file. It works, but the problem is the text on button or textbox on dialog box is missing sometime.
Would some one tell me how to solve this issue. Thanks in advance
There is my code:
package MyPackage;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
import java.io.*;
public class MainForm extends JFrame implements ActionListener {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MainForm();
}
public MainForm(){
super("Example");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Name the JMenu & Add Items
JMenu menu = new JMenu("File");
menu.add(makeMenuItem("Open"));
menu.add(makeMenuItem("Save"));
menu.add(makeMenuItem("Quit"));
// Add JMenu bar
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
setJMenuBar(menuBar);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
//setSize(600, 300);
//setLocation(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// Menu item actions
String command = e.getActionCommand();
if (command.equals("Quit")) {
System.exit(0);
} else if (command.equals("Open")) {
// Open menu item action
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(MainForm.this);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// Load file
} else if (returnVal == JFileChooser.CANCEL_OPTION ) {
// Do something else
}
}
else if (command.equals("Save")) {
// Save menu item action
System.out.println("Save menu item clicked");
}
}
private JMenuItem makeMenuItem(String name) {
JMenuItem m = new JMenuItem(name);
m.addActionListener(this);
return m;
}
}
Upvotes: 0
Views: 274
Reputation: 2468
A problem in your code is in which thread is being executed. Most Swing methods can only be executed in a very specific thread called Event Dispatch Thread. Because you are not doing this correctly your application will tend to have inconsistent errors. The larger it gets the easier it is that something goes wrong.
To properly execute in the EDT you need to change your main method to:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run()
{
new MainForm();
}
});
}
invokeLater
schedules the execution of the MainForm constructor to the EDT, so that the GUI initialization code is executed in the proper thread.
I don't know if this alone will solve the problem but will surely solve future non-reproducible missbehaviours. I've personally seen some threading problems other times with JFileChooser
, there are or have been several reported bugs with the thread management of this class.
Note that this is a core rule of Swing that even the Hello world complies.
Upvotes: 3