gui development with gluon scene builder plugin for intellij · 2018. 5. 18. · connect scene...

7
GUI development with Gluon Scene Builder plugin for IntelliJ 1. Download and install Scene Builder Download Gluon Scene Builder: http://gluonhq.com/products/scene-builder/#download Remeber the path to the SceneBuilder.exe file (e.g. C:\Program Files\SceneBuilder\SceneBuilder.exe). Open IntelliJ and create a new project: File->New Project-> JavaFX->... Show IntelliJ project (once per project) the path to the SceneBuilder.exe:

Upload: others

Post on 18-Jan-2021

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GUI development with Gluon Scene Builder plugin for IntelliJ · 2018. 5. 18. · Connect Scene Builder elements with JavaFX project If you add any elements to the pane in Scene Builder,

GUIdevelopmentwithGluonSceneBuilderpluginforIntelliJ

1.DownloadandinstallSceneBuilder

DownloadGluonSceneBuilder:http://gluonhq.com/products/scene-builder/#download

RemeberthepathtotheSceneBuilder.exefile(e.g.C:\Program Files\SceneBuilder\SceneBuilder.exe).

OpenIntelliJandcreateanewproject:File->New Project->JavaFX->...

ShowIntelliJproject(onceperproject)thepathtotheSceneBuilder.exe:

Page 2: GUI development with Gluon Scene Builder plugin for IntelliJ · 2018. 5. 18. · Connect Scene Builder elements with JavaFX project If you add any elements to the pane in Scene Builder,

2.Projectfoldersandfiles

In the project src->sample folder, there are two java files: Main.java andController.java.

In theMain file, there are themainmethod and thestartmethod. Thestartmethodcontainsareferencetothesceneroot,whichisinthesample.fxml fileandthedimensionofthescene.

Itisrecommendedtodeletethedimensionsofthesceneinthestart methodandleaveitas

follows:

primaryStage.setScene(new Scene(root));

3.OpenSceneBuilder

ToopenSceneBuilder,clickonthe sample.fxml usingtherightbuttonofthemouseandchoose:->Open In SceneBuilder.

Page 3: GUI development with Gluon Scene Builder plugin for IntelliJ · 2018. 5. 18. · Connect Scene Builder elements with JavaFX project If you add any elements to the pane in Scene Builder,

InSceneBuilder,firstofallshowthelocationoftheControllerfile:intheleftmenuchoosethe last tab Controller, and type sample.Controller (the location of theController.javafile):

Thendeletethedefaultpane–GridPane:

Page 4: GUI development with Gluon Scene Builder plugin for IntelliJ · 2018. 5. 18. · Connect Scene Builder elements with JavaFX project If you add any elements to the pane in Scene Builder,

Onthe left, in theContainers menudraganddrop thePanepane to thecentreof thewindow.Nowyoucanaddanyelementstothepane(alistofelementsisontheleft,e.g.intheControls menu).Thepropertiesoftheelements(e.g.color,size,fontetc.)canbeadjusted

usingthemenuontheright.

ConstantlysavechangesmadeinSceneBuilder!

4.ConnectSceneBuilderelementswithJavaFXproject

If you add any elements to the pane in Scene Builder, they also have to be declared in theController.java file.

Page 5: GUI development with Gluon Scene Builder plugin for IntelliJ · 2018. 5. 18. · Connect Scene Builder elements with JavaFX project If you add any elements to the pane in Scene Builder,

Forexample,ifyouaddabuttonandalabeltothepaneinSceneBuilder,writethefollowingstatementsintoController.java:

@FXML

Button button = new Button();

@FXML

Label label = new Label();

@FXML isacompulsoryannotationforallelementsandmethodsusedinSceneBuilder.Ithelpscreateaconnectionbetweenthevariable/methodnameintheController.javaandfxmlfile (which contains the structure of the scene).Make sure that you import the javafxpackages when you add elements to your code into the Controller.java file (notjava.swing oranythingelse).

To connect the variable namewith Scene Builder, go to the Scene Builder and click on theelementyouwanttobindwithyourcode:

Page 6: GUI development with Gluon Scene Builder plugin for IntelliJ · 2018. 5. 18. · Connect Scene Builder elements with JavaFX project If you add any elements to the pane in Scene Builder,

Intherightmenu,findthetheCode:Button tab;inthefxid dropboxmenuchoosethenameofthevariable.IntheOn Actiondropboxmenu,choosethemethodwhichisinvokedwhen an action occurs. Note a method can be chosen if it is already created in theController.java file.

TolaunchtheGUIapplication,gothemainclassandruntheprogram.

5.Example

Studyandcopythefollowingcodeintothe Controller.java file:

package sample;import javafx.fxml.FXML;import javafx.scene.control.Button; import javafx.scene.control.Label;

public class Controller {

Page 7: GUI development with Gluon Scene Builder plugin for IntelliJ · 2018. 5. 18. · Connect Scene Builder elements with JavaFX project If you add any elements to the pane in Scene Builder,

@FXML Button button = new Button();

@FXMLLabel label1 = new Label();

@FXML Label label2 = new Label();

@FXML void vahetaSilti() {

if (label1.isVisible() && label2.isVisible()) { label2.setVisible(false);

} else {

label1.setVisible(!label1.isVisible()); label2.setVisible(!label2.isVisible()); }

}

}

InSceneBuilder,createapane,addtwolabelsandabutton.Selectappropriatenamesfortheelementsinthefxid dropboxmenu.SelectactionsforthebuttonintheOn Actiondropboxmenu.