How to get the user input from the Spinner - Javafx - javafx

I created a Spinner with JavaFx accepting Integer only. The user choose a number between 0 and 10 (number of turns he decides to rest ingame).
However, I want to get this value when the user clicks on the "OK" button.
Here is the line where I want to call the user input. (I tried .getValue() but it's not working) :
#Override
public void initialize(URL url, ResourceBundle rb) {
RestOK.setOnAction(e -> ToF.rest((int) RestSpinner.getValue(), restTime));
}
And this is the fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cc.view.RestController">
<children>
<Label layoutX="66.0" layoutY="46.0" text="How many turns do you need for resting?" />
<Separator layoutX="81.0" layoutY="83.0" prefWidth="240.0" />
<Spinner fx:id="RestSpinner" editable="true" layoutX="94.0" layoutY="109.0">
<valueFactory>
<SpinnerValueFactory.IntegerSpinnerValueFactory max="10" min="0" />
</valueFactory>
</Spinner>
<Button fx:id="RestCancel" cancelButton="true" layoutX="170.0" layoutY="160.0" mnemonicParsing="false" onAction="#closeRest" text="Cancel" />
<Button fx:id="RestOK" layoutX="276.0" layoutY="109.0" mnemonicParsing="false" text="OK" />
</children>
</AnchorPane>
Thank you

Related

How to bind Pane with another Pane in JavaFX

I'm finding the way that how to bind the size of a pane with the outer pane.
I could bind the size ofRectangle with Pane,
But couldn't bind an inner pane (Red dotted rectangle) with outer pane (Blue dotted rectangle)
Why I couldn't do it
It seems to be that Pane does not provide bind method for widthProperty
Objects
At least, bind width with TextArea of inner Pane with outer Pane
I'll make many new tabs, therefore, I prefer to bind inner Pane with outer Pane rather than bind TextArea directly to outer Pane.
Situation
Blue and Orange rectangles are bound well with pane dropPane
But Blue border pane's width should be the same size as a red border pane!
My Sources
main.kt:
import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene.*
import javafx.scene.control.*
import javafx.scene.paint.Paint
import javafx.scene.shape.Rectangle
import javafx.stage.Stage
import javafx.scene.layout.*
import javafx.scene.paint.*
fun main(args : Array<String>) {
println("EntryPoint")
Application.launch(PaneBindingTest().javaClass, *args)
}
class PaneBindingTest : Application() {
private var tabCount = 0
private fun generateNewTab (): Tab {
val tab = Tab()
tabCount += 1
tab.text = "Tab$tabCount"
val fxml = javaClass.getResource("fxml/Tab.fxml")
val aTabPane: Pane = FXMLLoader.load(fxml)
aTabPane.border = Border(BorderStroke(Paint.valueOf("Red"),BorderStrokeStyle.DASHED, CornerRadii.EMPTY, BorderWidths.DEFAULT))
tab.content = aTabPane
return tab
}
override fun start(primaryStage: Stage) {
primaryStage.title = "EntryPoint"
primaryStage.isAlwaysOnTop = true
val fxml = javaClass.getResource("fxml/EntryPoint.fxml")
val root: Parent = FXMLLoader.load(fxml)
val scene = Scene(root)
val epPane= root.lookup("#EPPane") as AnchorPane // Entry Point Pane
val dropPane= root.lookup("#DropPane") as AnchorPane // Drop Pane
val tabPane= root.lookup("#TabPane") as TabPane // Tab Pane
val singleDropPoint= root.lookup("#ForSingle") as Rectangle // Single-ArchiveSet drop point
val multiDropPoint = root.lookup("#ForMulti") as Rectangle // Multi-ArchiveSet drop point
//epPane.background = Background(BackgroundFill(Paint.valueOf("Yellow"), CornerRadii(0.0), Insets(0.0,0.0,0.0,0.0)))
//dropPane.background = Background(BackgroundFill(Paint.valueOf("Green"), CornerRadii(0.0), Insets(0.0,0.0,0.0,0.0)))
tabPane.tabClosingPolicy = TabPane.TabClosingPolicy.ALL_TABS // or SELECTED_TAB, UNAVAILABLE
tabPane.border = Border(BorderStroke(Paint.valueOf("Blue"),BorderStrokeStyle.DASHED, CornerRadii.EMPTY, BorderWidths.DEFAULT))
singleDropPoint.heightProperty().bind(epPane.heightProperty().divide(32).multiply(23))
multiDropPoint.yProperty().bind(singleDropPoint.yProperty().add(dropPane.heightProperty().divide(4).multiply(3)))
multiDropPoint.heightProperty().bind(epPane.heightProperty().divide(4))
primaryStage.scene = scene
primaryStage.show()
val newTab = generateNewTab()
tabPane.tabs.add(newTab)
tabPane.selectionModel.select(newTab)
}
}
fxml/EntryPoint.fxml
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Rectangle?>
<AnchorPane fx:id="EPPane" prefHeight="640.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TabPane fx:id="TabPane" prefHeight="640.0" prefWidth="1152.0" tabClosingPolicy="ALL_TABS" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="128.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
</TabPane>
<AnchorPane fx:id="DropPane" layoutY="128.0" maxWidth="128.0" minWidth="128.0" prefWidth="128.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Rectangle fx:id="ForSingle" arcHeight="16.0" arcWidth="16.0" fill="DODGERBLUE" height="360.0" width="120.0" stroke="#1100ff" strokeType="INSIDE" strokeWidth="8.0" AnchorPane.leftAnchor="4.0" AnchorPane.rightAnchor="4.0" AnchorPane.topAnchor="4.0"/>
<Rectangle fx:id="ForMulti" arcHeight="16.0" arcWidth="16.0" fill="#ff9d1f" height="240.0" width="120.0" stroke="#ff8800" strokeType="INSIDE" strokeWidth="8.0" AnchorPane.leftAnchor="4.0" AnchorPane.rightAnchor="4.0" AnchorPane.bottomAnchor="4.0"/>
</children>
</AnchorPane>
</children>
</AnchorPane>
fxml/Tab.fxml:
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TextArea fx:id="FilePaths" layoutX="4.0" layoutY="80.0" prefHeight="128.0" AnchorPane.leftAnchor="4.0" AnchorPane.rightAnchor="4.0" AnchorPane.topAnchor="72.0" />
<RadioButton layoutX="14.0" layoutY="206.0" mnemonicParsing="false" selected="true" text="Hash/Size only">
<toggleGroup>
<ToggleGroup fx:id="AnalyzeMethod" />
</toggleGroup>
</RadioButton>
<RadioButton layoutX="120.0" layoutY="206.0" mnemonicParsing="false" text="FileName" toggleGroup="$AnalyzeMethod" />
<RadioButton layoutX="200.0" layoutY="206.0" mnemonicParsing="false" text="Directory Structure" toggleGroup="$AnalyzeMethod" />
<CheckBox fx:id="CheckDiff" layoutX="336.0" layoutY="206.0" mnemonicParsing="false" text="Show Diff" />
<CheckBox fx:id="CheckSame" layoutX="420.0" layoutY="206.0" mnemonicParsing="false" text="Show Same" />
<ComboBox fx:id="ComboBox" layoutX="840.0" layoutY="202.0" prefWidth="150.0" />
<HBox fx:id="LabelBox" layoutX="12.0" layoutY="14.0" prefHeight="64.0" prefWidth="978.0" AnchorPane.leftAnchor="4.0" AnchorPane.rightAnchor="4.0" AnchorPane.topAnchor="4.0" />
</children>
</AnchorPane>
I've found that almost the same question, but no answer was there.
You do not need a binding to achieve this. The problem is the fact that you set the constraints for maxHeight/maxWidth to USE_PREF_SIZE, i.e. to a fixed value that can very well be reached when resizing the window. Remove those constraints to allow the AnchorPane to grow as necessary:
Tab.fxml
<AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="480.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
...
</AnchorPane>
Furthermore I recommend avoiding the use of AnchorPanes, if you can avoid it. It's hard to achieve responsive layouts with this kind of layout. VBox and HBox would do much better jobs in this case.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.Region?>
<VBox prefHeight="480.0"
prefWidth="1000.0" xmlns="http://javafx.com/javafx/10.0.1"
xmlns:fx="http://javafx.com/fxml/1">
<padding>
<Insets topRightBottomLeft="4" />
</padding>
<children>
<HBox fx:id="LabelBox" prefHeight="64.0" prefWidth="978.0" />
<TextArea VBox.vgrow="ALWAYS" fx:id="FilePaths"
prefHeight="128.0">
</TextArea>
<HBox spacing="10" minWidth="-Infinity">
<VBox.margin>
<Insets top="5" left="0" right="0" bottom="0" />
</VBox.margin>
<children>
<RadioButton mnemonicParsing="false" selected="true"
text="Hash/Size only">
<toggleGroup>
<ToggleGroup fx:id="AnalyzeMethod" />
</toggleGroup>
</RadioButton>
<RadioButton mnemonicParsing="false" text="FileName"
toggleGroup="$AnalyzeMethod" />
<RadioButton mnemonicParsing="false"
text="Directory Structure" toggleGroup="$AnalyzeMethod" />
<CheckBox fx:id="CheckDiff" mnemonicParsing="false"
text="Show Diff" />
<CheckBox fx:id="CheckSame" mnemonicParsing="false"
text="Show Same" />
<Region HBox.hgrow="ALWAYS" /> <!-- placeholder to grow/shrink -->
<ComboBox fx:id="ComboBox" prefWidth="150.0" />
</children>
</HBox>
</children>
</VBox>

Using Unicode characters with JavaFX

I've been playing around with swing for some time and decided to check out FX now. So far I'm finding it a lot easier and more fun to work with as compared to swing but I've run into a small speed bump and after hours of looking around I just can't find a solution.
I am unable to use \u when I try to add it through the fxml file It works fine if I don't use the fxml but I want to use the scene builder as it is more convenient.
Here's the small piece of code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.131" fx:controller="baitform.designDocController">
<children>
<Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
<Label layoutX="145.0" layoutY="129.0" text="\u0644\u0627\u062B\u0627\u0646\u0649" />
</children>
</AnchorPane>
The error I keep getting is
Caused by: javafx.fxml.LoadException: Invalid escape sequence.
Not sure if relevant, but I'm using jdk1.8.0_131 & netbeans 8.2
If anyone could point me in the right direction here I'd really appreciate it.
FXML is an XML, and so you need to use XML escaping:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.131" fx:controller="baitform.designDocController">
<children>
<Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
<Label layoutX="145.0" layoutY="129.0" text="لاثانى" />
</children>
</AnchorPane>
That being said, if you are able to input the characters, you can just insert them as is.
See also: https://www.w3.org/International/questions/qa-escapes

fx:script shows NullPointerException in scene builder

I am new with JavaFX FXML and I was trying fx:script with this "NMAESTAGE.fxml" file below:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?language javascript?>
<VBox fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox alignment="CENTER" prefWidth="200.0">
<children>
<Label prefHeight="17.0" prefWidth="83.0" text="Control name:" />
<Region prefHeight="25.0" prefWidth="0.0" HBox.hgrow="ALWAYS" />
<TextField />
</children>
</HBox>
<HBox prefHeight="26.0" prefWidth="228.0">
<children>
<Region HBox.hgrow="ALWAYS" />
<Button mnemonicParsing="false" onAction="nextAction()" prefHeight="25.0" prefWidth="53.0" style="-fx-background-radius: 0; -fx-background-color: rgb(0,110,215);" text="Next" textFill="WHITE">
<fx:script>
function nextAction(){
root.getScene().getWindow().hide();
}
</fx:script>
</Button>
</children>
</HBox>
</children>
<padding>
<Insets bottom="30.0" left="30.0" right="30.0" top="50.0" />
</padding>
</VBox>
But the problem is, when I try to open the fxml file in scene builder, the fx:script tag is causing a NullPointerException. It says that "make sure NAMESTAGE.fxml is a valid fxml file".
The application compiles, builds, and runs without any error.
Removing the 5 lines with fx:script tag along with the onAction="nextAction()" part, somehow solve the problem. I don't understand what I am doing wrong here. Could anybody help?
For me it's a bug, introduced by Scene Builder 2.0; the older versione (1.1) doesn't have the problem

create note application from javafx

How do I make textarea get user input from the textfield ? How to i control these buttons from the controller and is there any possible way that i can make the textfield act as textarea when the user submit , the user may edit it when click on the textarea to edit?
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxnote.Note2Controller">
<top>
<AnchorPane prefHeight="56.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="btn2" layoutX="555.0" layoutY="10.0" mnemonicParsing="false" onMouseClicked="#test" text="x" />
</children>
</AnchorPane>
</top>
<center>
<TextArea prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets left="10.0" right="10.0" />
</BorderPane.margin></TextArea>
</center>
<padding>
<Insets bottom="20.0" />
</padding>
<bottom>
<VBox prefHeight="149.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<TextField prefHeight="111.0" prefWidth="600.0">
<VBox.margin>
<Insets />
</VBox.margin>
</TextField>
<AnchorPane prefHeight="38.0" prefWidth="580.0">
<children>
<Button fx:id="btsub" layoutX="434.0" layoutY="21.0" mnemonicParsing="false" text="submit" />
<Button fx:id="btcl" layoutX="518.0" layoutY="21.0" mnemonicParsing="false" text="clear" />
</children>
</AnchorPane>
</children>
<BorderPane.margin>
<Insets left="10.0" right="10.0" />
</BorderPane.margin>
</VBox>
</bottom>
</BorderPane>
To add actions in the controller for the buttons:
Add the controller class in SceneBuilder
To specify the controller's class check the right side of the SceneBuilder under the tab "Controller" is a text field named "Controller class". Also notice there is a list with all the node that have an "fx-id".
Add fx:id to all the nodes you need in controller
For any component that you need to work with in the controller add an fx:id. For actions check add a method name in the tab "Code" of any component. For example if you want an action for the button "Submit" add the name of the method there in the field "On Action" named "submitPushed"
In the controller's class add all the Nodes with fx:id
For every node you attributed with an fx:id you need to add it as a property in the controller class with the annotation #FXML. For a TextField with the fx:id textSubmit you will ad a property in the controller
#FXML TextField textSubmit
In the controller class define the methods for the actions
For the button named "Submit" with the action "submitPushed" you need to declare the method of the action
#FXML submitPushed(ActionEvent event){
//add code here for what the button should do
}
If you want to add the TextField's content to the TextArea when the button Submit is pushed
#FXML submitPushed(ActionEvent event){
String content=textField.getText();
String contentTextArea = textArea.getText();
textArea.setText(contentTextArea+"\n"+content);
}

JavaFX misaligned header on TableView or extra column

Pic of the TableView header
I need help aligning the headers for the columns on this TableView. The problem seems to be related to setting the TableView's column resize policy to constrained-resize. Can someone explain how to fix this? Thanks.
I am setting the policy to constrained-resize to get rid of the extra column based on this SO post: TableView has more columns than specified
My stack: Java 8, JavaFX 8, and scenebuilder
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="200.0" maxWidth="322.0" prefHeight="200.0" prefWidth="320.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">
<center>
<TableView fx:id="statusTable" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="322.0">
<columns>
<TableColumn fx:id="statusDescription" minWidth="120.0" prefWidth="210.0" text="Description" />
<TableColumn fx:id="status" prefWidth="97.0" text="Priority" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</center>
</BorderPane>

Resources