创建新的 JFrame 然后关闭后继续执行代码

Continue code execution after new JFrame is created and then closed

所以这可能是个愚蠢的问题,但我就是想不通。 基本上我有我的 MainWindow,当我按下一个按钮时,应该出现一个名为 PartSelectionWindow 的新 window,它包含一个 JTable(JTable 填充了一个自定义 table 模型)。然后我 select 一行并按确定返回主窗口并处理信息 selected。问题是:

如果我为 PartSelectionWindow 使用 JFrame,在 window 关闭后,代码执行不会在 MainWindow 按钮事件中继续

如果我使用 JDialog,Jtable 没有填写自定义模型并且它的按钮没有响应,只能通过单击 JDialog 的 X 来关闭它。就好像它被禁用了一样。调试时我注意到它确实在关闭 JDialog 后尝试填写 table。我在某处读到这可能是因为代码在 setVisible 设置为 true 后暂停,所以我尝试在 setVisible 设置为 true 之前放置代码来填充 JTable 但它仍然不起作用。

必填代码:

    //MainWindow button event when tryng to use new JFrame
    private void btnSetupListActionPerformed(java.awt.event.ActionEvent evt) {                                             
        //call to new JFrame
        new partsWindow();
        //after closing partsWindow the next line of code does NOT execute
        txtPartNum.setText(PartsWindow.jpart.getPartNumber());
    }  

    //MainWindow button event when tryng to use new JDialog
    private void btnSetupListActionPerformed(java.awt.event.ActionEvent evt) {                                             
        //call to new JDialog
        new partsDialog(this, true);
        //after closing partsWindow the next line of code does NOT execute
        txtPartNum.setText(PartsWindow.jpart.getPartNumber());
    }  

JDialog 的构造函数window

public partsDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    this.setTitle("Setup Material Client");
    this.setVisible(true);
    this.setLocationRelativeTo(null);
    jTable1.getTableHeader().addMouseListener(new partsDialog.ColumnFitAdapter());
    jTable1.getTableHeader().setReorderingAllowed(false);
    GetBomForSetupMaterial = jtrace.GetBomForSetupMaterial(Main.station);
    jTable1.setModel(new PartModel(GetBomForSetupMaterial.getPartPositionList()));
}

如有任何帮助,我们将不胜感激。

对于第二个 window,执行 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) 或类似的操作。我在 phone 上,所以我无法获得确切的代码。这行代码将使框架终止,而不是程序

为了说明我的评论,您可以使用这样的代码(简化)

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class JDialogTest extends JDialog {
private static final long serialVersionUID = 1L;
private String text;
private JTextField textField;

public JDialogTest(JFrame owner, String text){
    super(owner,true);
    this.text = text;
    init();
}


private void init() {
    this.getContentPane().setLayout(new BorderLayout());
    JButton btnContinue = new JButton("Close me and continue");
    btnContinue.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JDialogTest.this.dispose(); 
        }
    });
    textField = new JTextField();
    this.getContentPane().add(new JLabel(text),BorderLayout.NORTH);
    this.getContentPane().add(textField,BorderLayout.CENTER);
    this.getContentPane().add(btnContinue,BorderLayout.SOUTH);
    this.pack();
}

public JTextField getTextField() {
    return textField;
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setVisible(true);
    JDialogTest test = new JDialogTest(frame, "Hello");
    System.out.println("I open");
    test.setLocationRelativeTo(null);
    test.setVisible(true);
    System.out.println("Good you closed it value is " + test.getTextField().getText());
    frame.setVisible(false);

}

 }

我刚刚传递了一些文本以结束 JLabel 你应该传递你的 table 或关于如何创建它的信息

在您的主要 window 中将其设为 JFrame。当它被构建时,在那里创建你的对话框,但将其可见设置为 false。该对话框应该是 modal = false。当您将其设置为 false 时,main window 将等到 JDialog 不再可见以继续其代码执行:

Public class MainWindow extends JFrame {
    protected JDialog = yourDialog;

public MainWindow {
    yourDialog = new JDialog(this, false);
    //* rest of your construction *//
}

private void yourButtonPressedActionPreformed(java.awt.eventActionEvent evt) {
    yourDialog.setVisible(true);
    //* The code halted here until you have set the visibility of that dialog to false. 
        Make the dialog do that to itself when the button is pressed *//
    foobar = yourDialog.getYourData();
}

将 JDialog 模态设置为 false 将停止主 window 中的代码操作,直到它完成,但由于您只将其可见性设置为 false,您仍然可以根据需要从中获取数据。