While you initially mentioned using two JFrames, it appears that your design involves one JFrame (frame1) and a JDialog for displaying search results. This distinction is important as JDialogs are dependent on their parent window.
To pass references between GUI objects, including JFrames and JDialogs, you can use standard Java references. When one window opens another, the parent window typically retains a reference to the child window and can call methods on it.
Determining when to retrieve data from a child window depends on its type:
Here's a simple example that allows a button in frame1 to open a JDialog and populate a JTextField in frame1 with the text entered in the JDialog's JTextField:
<code class="java">import java.awt.Window; import java.awt.Dialog.ModalityType; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class WindowCommunication { public static void main(String[] args) { java.awt.EventQueue.invokeLater(() -> createAndShowUI()); } private static void createAndShowUI() { JFrame frame1 = new JFrame("frame1"); MyFramePanel panel1 = new MyFramePanel(); frame1.getContentPane().add(panel1); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.pack(); frame1.setLocationRelativeTo(null); frame1.setVisible(true); } static class MyFramePanel extends JPanel { private JTextField textField; private JButton openDialogButton; private JDialog dialog; private MyDialogPanel dialogPanel; public MyFramePanel() { textField = new JTextField(10); textField.setEditable(false); textField.setFocusable(false); openDialogButton = new JButton("Open Dialog"); openDialogButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { openDialogAction(); } }); add(textField); add(openDialogButton); } private void openDialogAction() { if (dialog == null) { Window win = SwingUtilities.getWindowAncestor(this); if (win != null) { dialog = new JDialog(win, "My Dialog", ModalityType.APPLICATION_MODAL); dialogPanel = new MyDialogPanel(); dialog.getContentPane().add(dialogPanel); dialog.pack(); dialog.setLocationRelativeTo(null); } } dialog.setVisible(true); // Here, after the modal dialog is disposed, the text from the // dialog's JTextField is retrieved and set into this JDialog's JTextField. textField.setText(dialogPanel.getFieldText()); } } static class MyDialogPanel extends JPanel { private JTextField textField; private JButton okButton; public MyDialogPanel() { textField = new JTextField(10); okButton = new JButton("OK"); okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { okButtonAction(); } }); add(textField); add(okButton); } public String getFieldText() { return textField.getText(); } private void okButtonAction() { Window win = SwingUtilities.getWindowAncestor(this); if (win != null) { win.dispose(); } } } }</code>
For retrieving data from a JTable, you can use similar techniques, but you may need to consider additional factors such as table row selection.
The above is the detailed content of Here are a few question-based titles that capture the essence of the article: * How to Pass Values Between JFrames and JDialogs in Java Swing? * Retrieving Data from Child Windows: JFrames, JDialogs,. For more information, please follow other related articles on the PHP Chinese website!