Home  >  Article  >  Java  >  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,

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,

Barbara Streisand
Barbara StreisandOriginal
2024-10-26 19:50:30437browse

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, and Table Models in Java Swing
* Mastering Communicati

Passing Values between JFrames

Understanding Program Design

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.

Passing References

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.

Retrieving Data at the Right Time

Determining when to retrieve data from a child window depends on its type:

  • Modal Dialogs: When a modal dialog returns, the code immediately after making the dialog visible is the ideal time to retrieve its state.
  • Non-Modal Dialogs: For non-modal dialogs, you may need to use listeners to determine when to extract information.

Example

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn