java frame关闭执行函数,创建新的JFrame然后关闭后,继续执行代码

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:

If I use a JFrame for the PartSelectionWindow, after the window is closed the code execution does not continue in the MainWindow button event

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.

Obligatory code:

//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());

}

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()));

}

Any help would be appreciated.

解决方案

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);

}

}

I just passed some text to end up in JLabel you should pass your table or info on how to create it

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值