Get values from a JDialog

edited August 2015 in Questions about Code

I'm making a sketch, where I need to load values (two Strings) from a JDialog, after the "Apply" button is pressed. Here is the dialog's code:

class RuleInputWindow
  private JDialog frm;
  private JPanel axp, rlp, sthp, buttons, _window;
  private JTextField axiomIn;
  private JTextField rulesIn;
  private JButton _apply, _close;

  private String axiomOut, rulesOut; // <--- These are the values I want to get
  public boolean isApplied = false;

  RuleInputWindow(Frame parent)
    axp = new JPanel();
    axp.setLayout(new FlowLayout(FlowLayout.LEFT));
    axiomIn = new JTextField(20);
    axp.add(new JLabel("Axiom:"));

    rlp = new JPanel();
    rlp.setLayout(new FlowLayout(FlowLayout.LEFT));
    rulesIn = new JTextField(20);
    rlp.add(new JLabel("Rules:"));

    sthp = new JPanel();
    sthp.add(new JLabel("(example: A->AB; B->A)"));

    buttons = new JPanel();
    buttons.setLayout(new FlowLayout());
    _apply = new JButton("Apply");
    _close = new JButton("Close");

    frm = new JDialog(parent,"Set rules:");
    _window = new JPanel();
    _window.setLayout(new BoxLayout(_window, BoxLayout.Y_AXIS));

  void update()
      new ActionListener()
        public void actionPerformed(ActionEvent e)

          axiomOut = axiomIn.getText();
          rulesOut = rulesIn.getText();
          if (Parameters.ENABLE_DEBUG)
            println("--- Input dialog ---");
            println("Axiom: " + axiomOut);
            println("Rules: " + rulesOut);
          isApplied = true;

      new ActionListener()
        public void actionPerformed(ActionEvent e)

  void dispose()

  String getAxiom() { return this.axiomOut; }
  String getRules() { return this.rulesOut; }

Also, here is the function that calls the dialog and saves the values:

void setRulesFromPopUp()
  RuleInputWindow riw = new RuleInputWindow(frame);

  if (riw.isApplied)
    lsyst.setSystem(riw.getAxiom(),riw.getRules()); // Here is where I want to store the values

But these values can't be stored. So, how can I do it?


  • First question: does it works? Historically, we avoid to use Swing in Processng, because it tends to crash sketches (problems with mixing threading systems...). That's why some GUI libraries has been developed.

    Second question: well, the code looks OK, superficially. What do you mean by "these values can't be stored"? What prevents the storing?

  • I mean the dialog should set the values of the lsyst.setSystem(riw.getAxiom(),riw.getRules()) method, after the "Apply" button is pressed.

    PS: I did that before, but now I don't remember how to.

  • Answer ✓

    Look at your code calling the dialog: you create it, so I suppose it is displayed. Then you add event listeners. Then you check riw.isApplied.
    But when this is tested, the dialog hasn't closed yet, the user didn't had time to click anything, so the test is always false.

    In short, such dialog is asynchronous, that's why there are event listeners, to get feedback when the user does something.

    Note: either that, or the dialog would block as soon as it is displayed, so your update() function isn't called (waiting on the dialog), so the buttons have no event listener...

  • Well, now I turned this dialog into an extension of JDialog (I mean, class RuleInputWindow extends JDialog implements ActionListener) and did the method that returns the values based on this, and the returned value is a set of two Strings.

    Btw, thanks for your review.

Sign In or Register to comment.