1

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.

David A.
  • 83
  • 1
  • 15
  • 1
    Simplifing: On JDialog.setVisible(true), the code stops and wait for you to close normaly done by dispose()... on JFrame.setVisible(true) it continues.. If you should use JDialog or JFrame is up to you restructure your code and both will work, example if JDialog you need to set table before setVisible and in JFrame when you close you can call another metodo to continue execution.. From your example the JDialog seems the most "normal" solution – Petter Friberg Oct 09 '15 at 19:51
  • On JDialog I already tried putting the code to fill out the table before setVisible(true) but that makes the JDialog to not even appear when the button is pressed. And the suggestion about the JFrame, I had thought about it but surely there has to be a better way of doing this. It seems a bit messy. – David A. Oct 09 '15 at 19:55
  • 1
    If it was me I would have made a class that extends JDialog, pass into the contructor what you need to create it and then make a initMetod() that you call in constructor, the initMetod creates and adds the table with it listiners.. – Petter Friberg Oct 09 '15 at 19:56

3 Answers3

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

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • This is awesome! Thanks so much for the reply. Unfortunately I am still unable to set the JTable model. After debugging, I found that an exception is thrown right at the line of code that is assigning the table model. jTable.setModel(...); Exception is: java.lang.ArrayIndexOutOfBoundsException: 11 . Dont know why it works in a JFrame but not in a JDialog. Any suggestions? By the way, if I remove that line of code where it assigns the table model the jDialog appears without a problem. – David A. Oct 15 '15 at 17:22
  • I'm marking this as the solution because it is pretty much what I need and I decided to create a new question for my new issue. Thanks a bunch! I'll leave the link for reference [Unable to set a DefaultTableModel to a JTable contained in a JDialog](http://stackoverflow.com/questions/33155416/unable-to-set-a-defaulttablemodel-to-a-jtable-contained-in-a-jdialog) – David A. Oct 15 '15 at 18:03
0

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

  • Thx for the response but thats not my issue, my program does not terminate thanks to that neat line of code. My issue is that the line of code after the call to the new JFrame is not executed after that new JFrame is closed. (Im referring to my btnSetupListActionPerformed event) – David A. Oct 09 '15 at 19:52
  • Thats because the program continue executing the code, so you should move the line of code that is not being executed the the onDispose method in the second frame – theredcoder Oct 09 '15 at 20:11
0

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.

Rabbit Guy
  • 1,840
  • 3
  • 18
  • 28