Java 对话框关闭后JFileChooser挂起
首先,我想说我对荡秋千还不熟悉。因此,如果使用JFileChooser选择目录,我所要做的就是。我可以打开、导航和选择目录。当我按下任何关闭对话框的按钮时,问题就出现了。当我这样做时,我的应用程序就会冻结。当它冻结时,对话框返回的面板将变为白色。当我使用调试器时,挂起会在对话框关闭且未到达if语句后立即发生。我也在Eclipse插件中做这件事,如果这让事情变得不同的话。特别是,它位于视图内部。代码如下:Java 对话框关闭后JFileChooser挂起,java,eclipse,swing,io,Java,Eclipse,Swing,Io,首先,我想说我对荡秋千还不熟悉。因此,如果使用JFileChooser选择目录,我所要做的就是。我可以打开、导航和选择目录。当我按下任何关闭对话框的按钮时,问题就出现了。当我这样做时,我的应用程序就会冻结。当它冻结时,对话框返回的面板将变为白色。当我使用调试器时,挂起会在对话框关闭且未到达if语句后立即发生。我也在Eclipse插件中做这件事,如果这让事情变得不同的话。特别是,它位于视图内部。代码如下: public class TexturePacker extends ViewPart {
public class TexturePacker extends ViewPart {
public void createPartControl(Composite parent) {
Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
frame = SWT_AWT.new_Frame(composite);
frame.add(new TexturePackerPanel(frame));
}
}
public class TexturePackerPanel extends JPanel {
//This is called from initialize(), which is called in the constructor
private void initializeConfigPanel() {
JPanel configPanel = new JPanel();
JTextBoxk outputDirectory = new JTextField();
configPanel.add(inputDirectory);
JButton fileButton = new JButton("Folder");
fileButton.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent arg0) {
JFileChooser file = new JFileChooser(outputDirectory.getText());
file.setDialogTitle("Select Output Directory");
file.setDialogType(JFileChooser.OPEN_DIALOG);
file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = file.showDialog(frame, "Choose");
if(returnVal == JFileChooser.APPROVE_OPTION) {
outputDirectory.setText(file.getSelectedFile().getAbsolutePath());
}
}
//Other blank MouseListener methods//
});
configPanel.add(fileButton);
}
}
系统信息:Windows 8 64位
Java 7
Eclipse 4.2 SR1 EE版
我很确定这个问题是由于Swing在eclipse中没有很好地发挥作用造成的。我已经成功地让它使用SWT目录对话框工作。所以我要把整个JPanel转换成SWT。感谢大家的帮助,我现在对Swing的工作原理有了更多的了解。为此,你真的应该使用ActionListener而不是MouseListener 除此之外,我认为这是一个线程问题。您应该阅读中有关并发性的文档 将
JFileChooser
零件用
SwingUtilities.invokeLater(new Runnable() {
public void run() {
\\Your JFileChooser bit
}
编辑1:
我运行了以下经过轻微编辑的代码,但无法重现您的问题
public static void main(final String[] args){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JPanel configPanel = new JPanel();
final JButton fileButton = new JButton("Folder");
final JFrame frame = new JFrame();
frame.setVisible(true);
frame.add(configPanel);
// JButton outputDirectory = new JButton("XX");
fileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent arg0) {
final JFileChooser file = new JFileChooser();
file.setDialogTitle("Select Output Directory");
file.setDialogType(JFileChooser.OPEN_DIALOG);
file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
final int returnVal = file.showDialog(frame, "Choose");
if(returnVal == JFileChooser.APPROVE_OPTION) {
// outputDirectory.setText(file.getSelectedFile().getAbsolutePath());
System.out.println(returnVal);
}
}
});
configPanel.add(fileButton);
}
});
}
您确定要在中执行此代码吗?@user592704我在中添加了JTextBox初始化。为什么ActionListener比MouseListener更有利,我尝试了上面的方法,但没有任何帮助。此外,此方法SwingUtilities.isEventDispatchThread()表示我的代码在事件分派线程中MouseeEvent包含查看代码时不需要的信息,例如鼠标单击的位置。对于MouseListener中所有您不需要的方法,您的代码更加冗长。我无法准确地再现您的问题,因为您的代码不是如上@Andrew Thopmson所述的SSCE。我不确定我还能展示多少,没有那么多。我在一个视图中运行它,在我创建的eclipse插件中。所以我怀疑这就是造成问题的原因。