gpt4 book ai didi

java - JFileChooser 如何返回退出值?

转载 作者:行者123 更新时间:2023-11-30 01:38:35 24 4
gpt4 key购买 nike

使用JFileChooser的典型方法包括检查用户是否单击“确定”,如以下代码所示:

private void addModelButtonMouseClicked(java.awt.event.MouseEvent evt) {                                            
JFileChooser modelChooser = new JFileChooser();
if(modelChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ){
File selectedFile = modelChooser.getSelectedFile();
if(verifyModelFile(selectedFile)){
MetModel newModel;
newModel = parser.parse(selectedFile, editedCollection.getDirectory() );
this.editedCollection.addModel(newModel);
this.modelListUpdate();
}
}
}

我试图在继承JFrame的我自己的窗口中模仿这种行为。我认为这种处理表单的方式比将要编辑的集合传递到新表单更方便。但我意识到,如果我想在 JFrame 中有一个方法返回诸如退出状态之类的内容,我需要让它等待用户单击“确定”或“取消”而不卡住表单/对话框窗口。

那么,showOpenDialog() 是如何工作的呢?当我尝试检查实现时,我发现只有一行方法带有注释“编译代码”。

最佳答案

I tried to mimic this behavior in my own window inheriting JFrame.

JFrame 不是模态或“阻塞”组件。请改用模式 JDialogJOptionPane

例如

Blocking Chooser

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/** Typical output:
[JTree, colors, violet]
User cancelled
[JTree, food, bananas]
Press any key to continue . . .
*/
class ConfirmDialog extends JDialog {

public static final int OK_OPTION = 0;
public static final int CANCEL_OPTION = 1;

private int result = -1;

JPanel content;

public ConfirmDialog(Frame parent) {
super(parent,true);

JPanel gui = new JPanel(new BorderLayout(3,3));
gui.setBorder(new EmptyBorder(5,5,5,5));
content = new JPanel(new BorderLayout());
gui.add(content, BorderLayout.CENTER);
JPanel buttons = new JPanel(new FlowLayout(4));
gui.add(buttons, BorderLayout.SOUTH);

JButton ok = new JButton("OK");
buttons.add(ok);
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
result = OK_OPTION;
setVisible(false);
}
});

JButton cancel = new JButton("Cancel");
buttons.add(cancel);
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
result = CANCEL_OPTION;
setVisible(false);
}
});

setContentPane(gui);
}

public int showConfirmDialog(JComponent child, String title) {
setTitle(title);
content.removeAll();
content.add(child, BorderLayout.CENTER);
pack();
setLocationRelativeTo(getParent());

setVisible(true);

return result;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("Test ConfirmDialog");
final ConfirmDialog dialog = new ConfirmDialog(f);
final JTree tree = new JTree();
tree.setVisibleRowCount(5);
final JScrollPane treeScroll = new JScrollPane(tree);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b = new JButton("Choose Tree Item");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
int result = dialog.showConfirmDialog(
treeScroll, "Choose an item");
if (result==ConfirmDialog.OK_OPTION) {
System.out.println(tree.getSelectionPath());
} else {
System.out.println("User cancelled");
}
}
});
JPanel p = new JPanel(new BorderLayout());
p.add(b);
p.setBorder(new EmptyBorder(50,50,50,50));
f.setContentPane(p);
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}

关于java - JFileChooser 如何返回退出值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7009207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com