创建新的JFrame然后关闭后,继续执行代码 [英] Continue code execution after new JFrame is created and then closed
问题描述
所以这可能是一个愚蠢的问题,但我无法弄清楚. 基本上我有MainWindow,当我按下按钮时,将出现一个名为PartSelectionWindow的新窗口,其中包含一个JTable(JTable用自定义表模型填充).然后,我选择一行,然后按OK返回到MainWindow并使用选定的信息.问题出在这里:
So this is probably a silly question but I just cant figure it out. Basically I have my MainWindow, when I press a button a new window should appear named PartSelectionWindow that contains a JTable (the JTable is filled out with a custom table model). Then I select a row and press ok to go back to the MainWindow and work with the info selected. Here's the problem:
如果我对PartSelectionWindow使用JFrame,则在关闭窗口之后,代码不会在MainWindow按钮事件中继续执行
If I use a JFrame for the PartSelectionWindow, after the window is closed the code execution does not continue in the MainWindow button event
如果我使用JDialog,则Jtable不会用自定义模型填充,并且它的按钮没有响应,只能通过单击JDialog的X来关闭它.好像它已被禁用.调试时,我注意到它确实在关闭JDialog之后尝试填写表.我在某处读到这可能是因为setVisible设置为true后代码暂停了,所以我尝试在setVisible设置为true之前将代码填充JTable,但仍然无法正常工作.
If i use a JDialog, the Jtable is not filled out with the custom model and the button for it does not respond, it can only be closed by clicking on the X of the JDialog. It's as if it was disabled. When debugging I noticed it DOES try to fill out the table AFTER closing the JDialog. I read somewhere that this may be because the code is paused after the setVisible is set to true, so I tried putting the code to fill out the JTable BEFORE the setVisible is set to true but it still does not work.
强制性代码:
//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窗口的构造函数
Constructor for 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()));
}
任何帮助将不胜感激.
推荐答案
要说明我的评论,您可以使用以下代码(简化)
To ilustrate my comment you can use code like this (simplified)
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
结尾,您应该传递表格或有关如何创建它的信息
I just passed some text to end up in JLabel
you should pass your table or info on how to create it
这篇关于创建新的JFrame然后关闭后,继续执行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!