Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Recommendation: don't use NetBeans to generate your Swing code as it actually hinders your learning of Swing and gets you into bad habits like popping JFrames willy-nilly. Learn from the tutorials how to code Swing by hand. Then get your words perhaps in a dialog window such as provided by a JOptionPane or JDialog or perhaps by swapping JPanels with a CardLayout.</p> <p>If you use modal dialogs, you'll know when one dialog has finished as code will resume right after the dialog was set visible. Then you can query the dialog class for the data obtained. Otherwise if you persist in using JFrames, you'll need to add WindowListeners to the frames to know when they've been made invisible which is a little more difficult to do.</p> <p>For e.g., </p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FrameFoolery { private static void createAndShowGui() { JFrame frame = new JFrame("Frame Foolery"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new MainPanel(frame)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class MainPanel extends JPanel { private String words = ""; private JDialog dialog1; private JDialog dialog2; private Panel1 panel1; private Panel1 panel2; public MainPanel(final JFrame frame) { add(new JButton(new AbstractAction("Show Dialog 1") { public void actionPerformed(ActionEvent ae) { if (panel1 == null) { panel1 = new Panel1(); dialog1 = new JDialog(frame, "Dialog 1", true); dialog1.getContentPane().add(panel1); dialog1.pack(); Point pt = frame.getLocation(); dialog1.setLocation(pt.x - 100, pt.y - 100); } dialog1.setVisible(true); words = panel1.getWords(); } })); add(new JButton(new AbstractAction("Show Dialog 2") { public void actionPerformed(ActionEvent ae) { if (panel2 == null) { panel2 = new Panel1(); dialog2 = new JDialog(frame, "Dialog 2", true); dialog2.getContentPane().add(panel2); dialog2.pack(); dialog2.setLocationRelativeTo(frame); Point pt = frame.getLocation(); dialog2.setLocation(pt.x + 100, pt.y + 100); } panel2.setWords(words); dialog2.setVisible(true); } })); } } class Panel1 extends JPanel { private JTextField wordsField = new JTextField(20); Panel1() { add(new JLabel("Words:")); add(wordsField); add(new JButton(new AbstractAction("OK") { public void actionPerformed(ActionEvent e) { Window win = SwingUtilities.getWindowAncestor(Panel1.this); win.setVisible(false); } })); } public String getWords() { return wordsField.getText(); } public void setWords(String words) { wordsField.setText(words); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload