This same question gets asked a lot on this site, and the solution is almost always the same:
If you need to have the user deal with input that the main window depends on, don't use a second JFrame when a modal dialog is what you need. Use a modal JDialog or a JOptionPane to display your dependent window. When you do this, the program flow from the main window will be interrupted until the dialog has been fully dealt with, and this way it will be easy for the main program to detect when the user has completed actions with the dependent window as the program flow will resume from immediately after the spot when the dependent window, the model dialog, has been displayed. You can then query the fields displayed by the dependent window and use the information so contained to update your JTable's model.
Note that another option is to show the second view as a new view on the main window using a CardLayout to swap views.
If on the other hand the main window doesn't absolutely depend on the user dealing with the second window, then consider showing it as either a non-modal dialog or as a separate view via a CardLayout or a JTabbedPane.
For example:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class DialogEg {
private static void createAndShowGui() {
MainWin mainPanel = new MainWin();
JFrame frame = new JFrame("DialogEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class MainWin extends JPanel {
private String[] COL_NAMES = { "Last Name", "First Name" };
private DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0);
private JTextField lastNameField = new JTextField(15);
private JTextField firstNameField = new JTextField(15);
public MainWin() {
final JPanel dataPanel = new JPanel();
dataPanel.add(new JLabel("Last Name:"));
dataPanel.add(lastNameField);
dataPanel.add(Box.createHorizontalStrut(15));
dataPanel.add(new JLabel("First Name:"));
dataPanel.add(firstNameField);
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new AbstractAction("Add Name") {
@Override
public void actionPerformed(ActionEvent arg0) {
lastNameField.setText("");
firstNameField.setText("");
int result = JOptionPane.showConfirmDialog(MainWin.this, dataPanel,
"Enter Name", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String lastName = lastNameField.getText();
String firstName = firstNameField.getText();
Object[] dataRow = new String[] { lastName, firstName };
model.addRow(dataRow);
}
}
}));
setLayout(new BorderLayout());
add(new JScrollPane(new JTable(model)), BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
}
}