JFileChooser launch problems
I am having problems starting any application on my development machine that uses the 'JFileChooser' Swing object. When I originally developed the application and tested it , the File Chooser window opened and everything was fine but like after a week , I tried running the app again and nothing would show at all.
At first I thought it was a Threading problem( My original app used a little multi-threading), so I coped and pasted the following code ( I got the code from the Internet ) to test it out:
package com.kwm.util.test;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
final JFrame frame = new JFrame("JFileChooser Demo");
final JFileChoose开发者_如何学Gor fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.setCurrentDirectory(new File("C:\\tmp"));
JButton btn1 = new JButton("Show Dialog");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fc.showDialog(frame, "Choose");
}
});
JButton btn2 = new JButton("Show Open Dialog");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int retVal = fc.showOpenDialog(frame);
if (retVal == JFileChooser.APPROVE_OPTION) {
File[] selectedfiles = fc.getSelectedFiles();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < selectedfiles.length; i++) {
sb.append(selectedfiles[i].getName() + "\n");
}
JOptionPane.showMessageDialog(frame, sb.toString());
}
}
});
JButton btn3 = new JButton("Show Save Dialog");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fc.showSaveDialog(frame);
}
});
Container pane = frame.getContentPane();
pane.setLayout(new GridLayout(3, 1, 10, 10));
pane.add(btn1);
pane.add(btn2);
pane.add(btn3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
And still I see nothing. I am guessing the Machine has a problem but am unable to identify what the problem is.
Any help with this would be greatly appreciated.
EDIT 1 The OS Version is Windows Server 2003 Enterprise Version, SP1
The Java version is : 1.5.0_12 ( both JDK and JRE)
I am also afraid that this may be related to a Network problem... JFileChooser is looking for a network directory and the DNS may be conflicting. Is there a way to check this? Maybe check what JVM is logging?
@Andrew Thompson is right: starting on the event dispatch thread may not be the problem, but related bugs are notoriously protean and difficult to reproduce. For reference, I've a shown a common re-factoring below. It tested successfully on Mac OS X 10.5, using
$ java -version java version "1.5.0_28"
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
private static void createGUI() throws HeadlessException {
final JFrame frame = new JFrame("JFileChooser Demo");
final JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.setCurrentDirectory(new File("C:\\tmp"));
JButton btn1 = new JButton("Show Dialog");
btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fc.showDialog(frame, "Choose");
}
});
JButton btn2 = new JButton("Show Open Dialog");
btn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int retVal = fc.showOpenDialog(frame);
if (retVal == JFileChooser.APPROVE_OPTION) {
File[] selectedfiles = fc.getSelectedFiles();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < selectedfiles.length; i++) {
sb.append(selectedfiles[i].getName());
sb.append("\n");
}
JOptionPane.showMessageDialog(frame, sb.toString());
}
}
});
JButton btn3 = new JButton("Show Save Dialog");
btn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fc.showSaveDialog(frame);
}
});
Container pane = frame.getContentPane();
pane.setLayout(new GridLayout(3, 1, 10, 10));
pane.add(btn1);
pane.add(btn2);
pane.add(btn3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Had a similar problem with JFileChooser when using threads, uploaded a post here. I got a lot of help by reading this wiki http://en.wikipedia.org/wiki/Event_dispatching_thread (although its already been mentiond). My problem was that I had a scanner that was running in the thread, always waiting for input before showing GUI.
精彩评论