gui development with gluon scene builder plugin for intellij · 2018. 5. 18. · connect scene...
Post on 18-Jan-2021
27 Views
Preview:
TRANSCRIPT
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:
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.
InSceneBuilder,firstofallshowthelocationoftheControllerfile:intheleftmenuchoosethe last tab Controller, and type sample.Controller (the location of theController.javafile):
Thendeletethedefaultpane–GridPane:
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.
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:
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 {
@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.
top related