Home java How to interact between windows in JavaFX

How to interact between windows in JavaFX

Author

Date

Category

There is a basic window, in it field and button. By pressing the button opens a modal window with a text field and a button. We enter text in a text field, click on the button and the field value of the main window must change.
How to do it? I don’t want to do a bunch of static fields, maybe there is some more correct way?
(both windows – different classes)


Answer 1, Authority 100%

import javafx.application.application;
Import javafx.scene.scene;
Import javafx.scene.control.button;
Import javafx.scene.control.dialog;
import javafx.scene.control.textinputDialog;
import javafx.scene.layout.borderpane;
Import javafx.stage.stage;
Import java.util.optional;
Public Class Testso EXTENDS Application
{
  Public Static Void Main (String [] Args)
  {
    Launch (Args);
  }
  @Override
  Public Void Start (Stage PrimaryStage) Throws Exception
  {
    BORDERPANE PANE = NEW BORDERPANE ();
    Button BTN = New Button ("Text");
    BTN.SetonAction (E - & GT; {
      Dialog & lt; String & GT; dialog = new textinputdialog ("ENTER NEW TEXT HERE");
      Dialog.Settitle ("Change Button Text");
      Optional & lt; String & GT; optional = dialog.showandwait ();
      optional.IFPRESENT (BTN :: SETTEXT);
    });
    Pane.SetCenter (BTN);
    SCENE SCENE = NEW SCENE (PANE, 500, 500);
    PrimaryStage.Setscene (Scene);
    primarystage.show ();
  }
}

In the example above, when you press the button, a modal window with a text field and a button occurs. When you enter the text in the field and later by pressing the button will change the text on the button.

p.s. Need 8 java


Answer 2

import javafx.application.application;
Import javafx.scene.scene;
Import javafx.scene.control.button;
import javafx.scene.control.label;
import javafx.scene.Layout.vbox;
Import javafx.stage.stage;
/ **
 *
 * @Author Den.
 * /
Public Class JavaFXApplication78 Extends Application {
  Public Static Void Main (String [] Args) {
    Application.Launch (Args);
  }
  Windclass WC = New Windclass ();
  @Override
  Public Void Start (Stage Primarystage) {
    Button BTN = New Button ("Open Modal Window");
    Label lbl = new label ("unchanged label");
    BTN.SetonAction (ACT - & GT; {
      WC.ShowWindow (LBL, PRIMARYSTAGE);
    });
    Vbox root = new vbox (LBL, BTN);
    SCENE SCN = NEW SCENE (ROOT, 300, 250);
    PrimaryStage.Setscene (SCN);
    primarystage.settitle ("Basic Window");
    primarystage.show ();
  }
}

and second class

import javafx.scene.scene;
Import javafx.scene.control.button;
import javafx.scene.control.label;
import javafx.scene.layout.Stackpane;
Import javafx.stage.modality;
Import javafx.stage.stage;
import javafx.stage.window;
/ **
 *
 * @Author Den.
 * /
Public Class Windclass {
  Public Void ShowWindow (Label LBL, Window MOD) {
    Stage STG = New Stage ();
    Button BTN = New Button ("Change Tag");
    BTN.SetonAction (ACT - & GT; {
      lbl.settext ("Tag changed");
      STG.Close ();
    });
    Stackpane root = new stackpane (BTN);
    SCENE SCN = NEW SCENE (ROOT, 150, 100);
    STG.Setscene (SCN);
    STG.Settitle ("New Modal Window");
    STG.INITMODALITY (modality.window_modal);
    STG.INITOWNER (MOD);
    STG.Showandwait ();
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions