问题
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.
回答1:
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
回答2:
For the second window, do frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
or something like that. I'm on my phone so i can't get the exact code. This line of code will make it so the frame will be terminated, not the program
回答3:
In your main window make it a JFrame. When it is constructed create your dialog in there but set its visible to false. The dialog should be modal = false. When you set this as false the main window will wait until the JDialog is no longer visible to continue its code execution:
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();
}
Setting the JDialog modal to false will cease the code operation in your main window until it is done but since you have only set its visibility to false you can still grab data from it as you need.
来源:https://stackoverflow.com/questions/33045937/continue-code-execution-after-new-jframe-is-created-and-then-closed