So I have created a port scanner in Java.
While using Swing, everything works smoothly with 0 errors.
I however tried to convert my app to a JavaFX style of GUI, and nothing works when I press the buttons (Even though I have set the method ID's in the GUI builder).
What seems to be my problem?
My code:
package networkTools.gui;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class PortScannerController implements Initializable {
#FXML
TextField hostName;
#FXML
TextField fromPort;
#FXML
TextArea log;
#FXML
Button scan;
#FXML
Button reset;
#FXML
Label label;
#Override
public void initialize(URL url, ResourceBundle rb) {
hostName.requestFocus();
}
public void initFocus() {
hostName.requestFocus();
}
#FXML
private void onScan() {
int fp;
String h;
Socket s;
if (hostName.getText().equals("")) {
log.setText("Fill everything correct.");
return;
} else if (fromPort.getText().equals("")) {
log.setText("Fill everything correct..");
return;
} else if (!fromPort.getText().matches("[0-9]*")) {
log.setText("Give a number for a port ");
return;
}
// scan.disable(false);
reset.setText("Stop");
log.setText("");
log.clear();
label.setText("");
h = hostName.getText();
fp = Integer.parseInt(fromPort.getText());
label.setText("Port " + fp + " being tested (max +- 15 sec.)");
try {
s = new Socket(h, fp);
log.appendText("Poort " + fp + " is open.\n");
log.clear();
s.close();
} catch (Exception er) {
log.appendText("Poort " + fp + " is closed");
}
// scan.setEnabled(true);
reset.setText("Reset");
label.setText("Press scan to start.");
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="networkTools.gui.PortScannerController">
<children>
<StackPane prefHeight="80.0" prefWidth="600.0">
<children>
<Label id="title" alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="50.0" prefWidth="400.0" text="Port Scanner" StackPane.alignment="CENTER">
<font>
<Font name="Arial Black" size="18.0" />
</font>
</Label>
</children>
</StackPane>
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="295.0" minWidth="10.0" prefWidth="131.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="407.0" minWidth="10.0" prefWidth="269.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="50.0" minHeight="50.0" prefHeight="50.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="100.0" text="Host Name">
<font>
<Font name="Arial Black" size="12.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="100.0" text="Port" GridPane.rowIndex="1">
<font>
<Font name="Arial Black" size="12.0" />
</font>
<GridPane.margin>
<Insets left="10.0" />
</GridPane.margin>
</Label>
<TextField fx:id="fromPort" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
<font>
<Font name="Arial Black" size="12.0" />
</font>
</TextField>
<ToggleButton id="commandbutton" fx:id="onScan" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Scan" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
<font>
<Font name="Arial Black" size="12.0" />
</font>
</ToggleButton>
<ToggleButton id="commandbutton" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Reset" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
<font>
<Font name="Arial Black" size="12.0" />
</font>
</ToggleButton>
<TextField fx:id="hostName" GridPane.columnIndex="1">
<GridPane.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</GridPane.margin>
<font>
<Font name="Arial Black" size="12.0" />
</font>
</TextField>
</children>
</GridPane>
<TextArea id="textpane" fx:id="log" editable="false" prefHeight="148.0" prefWidth="600.0">
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
</VBox.margin>
</TextArea>
<StackPane prefHeight="80.0" prefWidth="380.0" VBox.vgrow="NEVER">
<VBox.margin>
<Insets bottom="10.0" left="20.0" />
</VBox.margin>
<children>
<Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="25.0" prefWidth="200.0" text="Press Scan to start" textFill="#1815c9">
<font>
<Font name="Arial Black" size="13.0" />
</font>
</Label>
</children>
</StackPane>
</children>
</VBox>
There is nowhere that you associate the handler with the button. You need an onAction="#onScan" in the FXML for the "Scan" button:
<ToggleButton id="commandbutton" onAction="#onScan" fx:id="onScan" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="25.0" prefWidth="100.0" text="Scan" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
There are several other things that look incorrect in your FXML:
You have several elements with the same CSS id ("commandButton" for example); you should use a styleClass instead of an id if you want to share style among several components.
You declare an fx:id="onScan" for the button above, but there is no field in your controller called onScan.
It's not really clear why you are using a ToggleButton instead of a regular Button for what appears to be an action. ToggleButtons are generally intended for toggling between two different states, not issuing a command/action.
Related
I am dealing with JavaFX , I am trying to get my graphical composant from my FXML page into the java classes, but it always return null value
Page.fxml
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<ScrollPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<content>
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" fx:id="Mycontent">
<top>
<fx:include source="TopMenu.fxml" />
</top>
<center>
<fx:include source="Operation.fxml" />
</center>
<left>
<fx:include source="SideBar_Inhumer.fxml" />
</left>
<bottom>
<fx:include source="Footer.fxml" />
</bottom>
</BorderPane>
</content>
</ScrollPane>
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.BorderPane;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML
public BorderPane Mycontent;
#FXML
public void goToDemandeur(){
System.out.println(Mycontent);
//Mycontent.setCenter(FXMLLoader.load(getClass().getResource("Demandeur.fxml")));
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
My code always print "null"
if I try for example Mycontent.getCenter()it gives me this error
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
(which normal because Mycontent is null, but why it is null? )
PS: my method goToDemandeur() is called into an other fxml page SideBar_Inhumer.fxml after a click onMouseClicked="#goToDemandeur"
SideBar_Inhumer.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox layoutX="77.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Pane >
<children>
<Label alignment="CENTER" text="Inhumer" >
<font>
<Font size="24.0" />
</font>
<padding>
<Insets bottom="20.0" left="25.0" right="25.0" top="30.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #F08080;" onMouseClicked="#goToDemandeur">
<children>
<Label text="Demandeur" >
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</Pane>
<Pane style="-fx-background-color: #C6E2B9;">
<children>
<Label text="Defunt">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #FBF6A5;">
<children>
<Label text="Emplacement">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #FFCC99;">
<children>
<Label text="Prestataire">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #D8B46D;">
<children>
<Label text="Opération">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
</children>
</VBox>
Main.java
package View;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("../View/Page.fxml"));
primaryStage.setTitle("Finalys");
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
Scene scene =new Scene(root, 600, 475);
primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());
primaryStage.setScene(scene);
primaryStage.show();
scene.getStylesheets().add(Main.class.getResource("bootstrap2.css").toExternalForm());
}
public static void main(String[] args) {
launch(args);
}
}
I want to add a listener to a textField in a window that appears when I press a button on a previous window. The problem is that the listener doesn't work at all. It doesn't detect any change.
public class WindowTwoController {
private Stage stage;
#FXML
private TextField imieTF = new TextField();
public void show() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("winTwo.fxml"));
Parent root = loader.load();
stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Sell Art");
stage.setScene(new Scene(root, 500, 500));
imieTF.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
stage.showAndWait();
}
I'm changing the value of the textField, but nothing is printed out to the console. The show method is called when a button is pressed on the previous window. Please help me. This is my winTwo.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<GridPane alignment="center" hgap="10" prefHeight="441.0" prefWidth="500.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="windowTwo.WindowTwoController">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<VBox prefHeight="354.0" prefWidth="455.0">
<children>
<Label alignment="CENTER" prefHeight="45.0" prefWidth="457.0" text="Sprzedaż dzieła">
<font>
<Font size="30.0" />
</font>
</Label>
<Separator prefWidth="200.0" />
<GridPane prefHeight="139.0" prefWidth="455.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Imię">
<font>
<Font size="28.0" />
</font>
</Label>
<Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Nazwisko" GridPane.rowIndex="1">
<font>
<Font size="28.0" />
</font>
</Label>
<TextField fx:id="imieTF" GridPane.columnIndex="1">
<GridPane.margin>
<Insets right="20.0" />
</GridPane.margin></TextField>
<TextField fx:id="nazwiskoTF" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="20.0" />
</GridPane.margin>
</TextField>
</children>
</GridPane>
<Separator prefWidth="200.0" />
<Label alignment="CENTER" prefHeight="17.0" prefWidth="450.0" text="Klient powracający">
<font>
<Font size="22.0" />
</font>
</Label>
<Separator prefWidth="200.0" />
<GridPane prefHeight="126.0" prefWidth="455.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" prefHeight="40.0" prefWidth="220.0" text="Poziom Zaprzyjaźnienia" textFill="#00000080">
<font>
<Font size="19.0" />
</font>
</Label>
<ComboBox fx:id="cb" opacity="0.5" prefHeight="25.0" prefWidth="207.0" GridPane.columnIndex="1" />
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="175.0" text="Akceptuj" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="25.0" />
</GridPane.margin>
</Button>
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="175.0" text="Anuluj" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="27.0" />
</GridPane.margin>
</Button>
</children>
</GridPane>
</children>
</VBox>
</children>
</GridPane>
You should add an anotation to the private member imieTF like so
public class WindowTwoController {
#FXML
private TextField imieTF;
#FXML
private void initialize() {
imieTF.textProperty().addListener((observable, oldValue, newValue) ->
{
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
}
public static void show() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("winTwo.fxml"));
Parent root = loader.load();
stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle("Sell Art");
stage.setScene(new Scene(root, 500, 500));
stage.showAndWait();
}
}
This should bind the TextField instance to the controller. But from what I can find out the FXML will create a new instance of WindowTwoController when the window belonging to the fxml file gets created.
Also see https://www.callicoder.com/javafx-fxml-form-gui-tutorial/ for a basic example on how this works.
Note that all manipulations of the textfield should be part of the JavaFX flow and cannot be done in a manual instance of WindowTwoController. Keep in mind that JavaFX will create it's own instance of WindowTwoController during the loader.load(); operation.
I'm trying to create a TabView in JavaFX. I would like to have some of the tabs distanced from the other tabs, since their functions belong to another category.
This is what it looks like now:
And as you can see I want the two last tabs to be moved to the right side without affecting the rest.
Here's my FXML-file:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="800.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Application.MainController">
<center>
<VBox alignment="BOTTOM_CENTER" prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>a
<TabPane accessibleRole="BUTTON" nodeOrientation="LEFT_TO_RIGHT" prefHeight="651.0" prefWidth="1000.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Courses">
<content>
<TableView prefHeight="665.0" prefWidth="1000.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
</TableView>
</content>
</Tab>
<Tab text="Education Matrix">
<content>
<TableView prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn prefWidth="75.0" text="C1" />
<TableColumn prefWidth="75.0" text="C2" />
</columns>
</TableView>
</content>
</Tab>
<Tab text="Employee">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Calendar">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="companiesTab" text="Companies">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab fx:id="providerTab" text="Provider">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
<VBox.margin>
<Insets />
</VBox.margin>
</TabPane>
</children>
</VBox>
</center>
<bottom>
<GridPane prefHeight="105.0" prefWidth="1000.0" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" style="-fx-background-color: grey;">
<children>
<Button mnemonicParsing="false" text="Button" />
</children>
<padding>
<Insets left="20.0" />
</padding>
</HBox>
<HBox alignment="CENTER_RIGHT" prefHeight="100.0" prefWidth="200.0" style="-fx-background-color: grey;" GridPane.columnIndex="2">
<children>
<Button mnemonicParsing="false" text="Button" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<padding>
<Insets right="20.0" />
</padding>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" style="-fx-background-color: grey;" GridPane.columnIndex="1">
<children>
<Button mnemonicParsing="false" text="Button" />
</children>
</HBox>
</children>
</GridPane>
</bottom>
<top>
<GridPane prefHeight="106.0" prefWidth="1000.0" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="494.0" minWidth="10.0" prefWidth="289.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="711.0" minWidth="10.0" prefWidth="711.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0">
<children>
<ChoiceBox prefWidth="150.0" />
</children>
<padding>
<Insets left="20.0" />
</padding>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" GridPane.columnIndex="1">
<children>
<Label text="Smart Academy Manager">
<font>
<Font name="Arial" size="31.0" />
</font>
</Label>
</children>
</HBox>
</children>
</GridPane>
</top>
</BorderPane>`enter code here`
Original TabPane does not provide tools for custom tab layout. But a similar result can be obtained using some trick.
You can set invisible Tab into required place and bind it's width with width of the TabPane.
First we need to calc splitter's width. This is the width of the TabPane minus the sum of the widths of the Tabs, excluding the splitter. Tab object can be obtained as Node from TabPane using the command .lookup(). We also need to consider the width associated with the styles (padding, spacing). For this example I will set this number by hard (for default theme). After calcs we set size of the splitter using .setStyle():
private void autoSizeSplitter(TabPane tabPane, Tab splitter) {
double width = tabPane.getWidth();
Set<Node> tabs = tabPane.lookupAll(".tab");
for (Node node : tabs) {
if (node.getId() == null || !node.getId().equals(splitter.getId())) {
width = width - node.getBoundsInParent().getWidth();
}
}
double PADDING = 20;
width = width - PADDING;
splitter.setStyle("-fx-background-color:transparent; -fx-pref-width: " + width + ";");
}
Finally we set the listener on the TabPane width property and autosize splitter for first time in initialization:
tabPane.widthProperty().addListener(observable -> autoSizeSplitter(tabPane, splitter));
Platform.runLater(() -> autoSizeSplitter(tabPane, splitter));
Full example:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;
import java.util.Set;
public class Sample extends Application {
#Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
Tab splitter = new Tab();
splitter.setDisable(true);
splitter.setId("splitter");
tabPane.getTabs().add(new Tab("111"));
tabPane.getTabs().add(new Tab("222"));
tabPane.getTabs().add(splitter);
tabPane.getTabs().add(new Tab("333"));
tabPane.getTabs().add(new Tab("444"));
tabPane.getTabs().add(new Tab("555"));
Scene scene = new Scene(tabPane, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
tabPane.widthProperty().addListener(observable -> autoSizeSplitter(tabPane, splitter));
Platform.runLater(() -> autoSizeSplitter(tabPane, splitter));
}
private void autoSizeSplitter(TabPane tabPane, Tab splitter) {
double width = tabPane.getWidth();
Set<Node> tabs = tabPane.lookupAll(".tab");
for (Node node : tabs) {
if (node.getId() == null || !node.getId().equals(splitter.getId())) {
width = width - node.getBoundsInParent().getWidth();
}
}
double PADDING = 20;
width = width - PADDING;
splitter.setStyle("-fx-background-color:transparent; -fx-pref-width: " + width + ";");
}
public static void main(String[] args) {
launch(args);
}
}
For a university project I have to create a GUI for an online version of a table game, the player can have some cards which I created by extending JavaFX ImageView and I want to put them inside the interface at runtime.
Therefore I inserted a JavaFX ListView inside a SplitPane but when I run my application the scroll bar doesn't work.
The cards are perfectly displayed and the scroll bar is visible, but when I try to use it, it just seems like a disabled button.
The same happens if I use a ScrollPane containing an HBox.
I tried to not use preferred sizes, I tried ScrollBarPolicies, nothing seems to work, in the end I came to the conclusion that the problem is somewhere inside the SplitPane.
I even thought maybe the JavaFX Application Thread stopped but when the scrollbar doesn't work it is actually running.
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<SplitPane fx:id="mainPlayerRootPane" disable="true" dividerPositions="0.10275689223057644" prefHeight="337.0" styleClass="mainPlayerRootPane" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.view.GUI.MainPlayerPaneController">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<GridPane prefHeight="335.0" prefWidth="78.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="116.0" minHeight="10.0" prefHeight="80.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="118.0" minHeight="10.0" prefHeight="82.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="138.0" minHeight="10.0" prefHeight="86.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="85.0" minHeight="10.0" prefHeight="85.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Pane prefHeight="85.0" translateX="4.0">
<children>
<ImageView fitHeight="77.0" fitWidth="73.0" layoutX="5.0" layoutY="1.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../../resources/images/VictoryPoints.png" />
</image>
</ImageView>
<Label fx:id="victoryPointsLabel" alignment="TOP_CENTER" contentDisplay="CENTER" opacity="0.9" prefHeight="79.0" prefWidth="70.0" text="99" textAlignment="JUSTIFY" textFill="WHITE">
<font>
<Font name="Trattatello" size="36.0" />
</font>
</Label>
</children>
</Pane>
<Pane prefHeight="85.0" translateX="4.0" GridPane.rowIndex="1">
<children>
<ImageView fitHeight="79.0" fitWidth="71.0" layoutY="4.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../../resources/images/Coins.png" />
</image>
</ImageView>
<Label fx:id="coinsLabel" alignment="CENTER" contentDisplay="CENTER" opacity="0.9" prefHeight="82.0" prefWidth="77.0" text="20" textFill="#222020">
<font>
<Font name="Trattatello" size="30.0" />
</font>
</Label>
</children>
</Pane>
<Pane prefHeight="85.0" prefWidth="81.0" translateX="4.0" GridPane.rowIndex="2">
<children>
<ImageView fitHeight="79.0" fitWidth="73.0" layoutX="17.0" layoutY="2.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../../resources/images/Servants.png" />
</image>
</ImageView>
<Label fx:id="servantsLabel" alignment="CENTER" contentDisplay="CENTER" opacity="0.9" prefHeight="101.0" prefWidth="70.0" text="10" textFill="#bcc600">
<font>
<Font name="Trattatello" size="24.0" />
</font>
</Label>
</children>
</Pane>
<Pane prefHeight="85.0" translateX="4.0" GridPane.rowIndex="3">
<children>
<ImageView fitHeight="79.0" fitWidth="71.0" layoutX="1.0" layoutY="3.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../../resources/images/Emporiums.png" />
</image>
</ImageView>
<Label fx:id="emporiumsLabel" alignment="CENTER" contentDisplay="CENTER" layoutY="8.0" prefHeight="73.0" prefWidth="71.0" text="10">
<font>
<Font name="Trattatello" size="36.0" />
</font>
</Label>
</children>
</Pane>
</children>
</GridPane>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0">
<children>
<SplitPane dividerPositions="0.5345345345345346" orientation="VERTICAL" prefHeight="335.0" styleClass="mainPlayerRootPane" stylesheets="#style.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0">
<children>
<SplitPane dividerPositions="0.5" prefHeight="174.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0">
<children>
<ListView fx:id="cardsListView" editable="true" orientation="HORIZONTAL" prefHeight="172.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0">
<children>
<ListView fx:id="permitsListView" orientation="HORIZONTAL" prefHeight="172.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="134.0" prefWidth="710.0" />
</items>
</SplitPane>
</children>
</AnchorPane>
</items>
</SplitPane>
Its controller class:
package client.view.GUI;
import java.util.List;
import client.model_properties.PlayerProperty;
import javafx.collections.ListChangeListener;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
public class MainPlayerPaneController {
#FXML
private Label victoryPointsLabel;
#FXML
private Label coinsLabel;
#FXML
private Label servantsLabel;
#FXML
private Label emporiumsLabel;
#FXML
private SplitPane mainPlayerRootPane;
#FXML
private ListView<PoliticalCardView> cardsListView;
#FXML
private ListView<PoliticalCardView> permitsListView;
#FXML
private void initialize(){
}
public void initializeMainPlayerPane(PlayerProperty mainPlayer) {
this.victoryPointsLabel.textProperty().bind(mainPlayer.getVictoryPoint().asString());
this.coinsLabel.textProperty().bind(mainPlayer.getCoins().asString());
this.servantsLabel.textProperty().bind(mainPlayer.getnOfServants().asString());
this.emporiumsLabel.textProperty().bind(mainPlayer.getAvailableEmporiums().asString());
this.addToDeckView(mainPlayer.getDeck());
mainPlayer.getDeckProperty().addListener(new DeckListChangeListener());
}
private void addToDeckView(List<? extends String> cards) {
PoliticalCardView auxCard;
for(String n : cards) {
auxCard = new PoliticalCardView(n);
auxCard.initializeCardView();
this.cardsListView.getItems().add(auxCard);
}
}
private class DeckListChangeListener implements ListChangeListener<String> {
#Override
public void onChanged(javafx.collections.ListChangeListener.Change<? extends String> c) {
List<? extends String> added = c.getAddedSubList();
List<? extends String> removed = c.getRemoved();
for(String n : removed) {
Node toRemove = cardsListView.getItems().stream()
.filter( r -> ((PoliticalCardView) r).getColor().equals(n))
.findFirst()
.get();
cardsListView.getItems().remove(toRemove);
}
addToDeckView(added);
}
}
}
And the class i created to represent the cards.
package client.view.GUI;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import utility.UserInterfaceUtilities;
public class PoliticalCardView extends ImageView {
private Image cardImage;
private String color;
public PoliticalCardView(String color) {
this.color = color;
}
public void initializeCardView() {
this.cardImage = UserInterfaceUtilities.POLITICAL_COLORS_TO_IMAGES.get(this.color);
this.setImage(cardImage);
this.setFitHeight(150);
this.setFitWidth(100);
}
public String getColor() {
return this.color;
}
}
I would like to know where's the problem since in two days of research I haven't found nothing similar on the internet and also i used other scrollbars which work just fine.
The problem is here:
<SplitPane fx:id="mainPlayerRootPane" disable="true" ..... >
Your SplitPane is disabled.
When you disable a Node by setting its disabledProperty, all of its children become disabled.
Indicates whether or not this Node is disabled. A Node will become
disabled if disable is set to true on either itself or one of its
ancestors in the scene graph.
Remove disable="true" from the FXML declaration to avoid disabling your ListView or ScrollPane.
I'm building an application using JavaFx and scene builder, however everything works fine, except when I add the Controller class.
I get the following error:
Exception in Application start method Exception in thread "main"
java.lang.RuntimeException: Exception in Application start method at
com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at
com.sun.javafx.application.LauncherImpl$$Lambda$1/868693306.run(Unknown
Source) at java.lang.Thread.run(Thread.java:745) Caused by:
javafx.fxml.LoadException:
/C:/Users/M%20ROSE/Documents/Info%20Trivia/out/production/Info%20Trivia/sample/gameScene1.fxml:15
However the moment I remove the fx:controller attribute from this line in my fxml code it works perfectly.
<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
Here are the relevant codes
Controller Class
package sample;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.* ;
import javafx.geometry.* ;
import javafx.scene.text.* ;
import javafx.scene.image.* ;
import javafx.scene.control.* ;
import java.lang.* ;
import javafx.scene.layout.* ;
import javafx.geometry.Insets ;
import javafx.scene.layout.GridPane ;
import javafx.scene.control.Button ;
import javafx.scene.control.Label ;
public class Controller {
//Initialize fxml controls
public Button trueButton;
public Button falseButton;
public Label playerLabel;
public Label questionLabel;
public Label scoreValue;
public ImageView questionImage;
public Rectangle redBar;
public Rectangle greenBar;
//Create array for level 1 questions
String[][] levelOneData = {
{"This is a Sequence Diagram","f", null},
{"This diagram is for a database","t", null},
{"This is a rack diagram","t", null},
{"This is a flow chart","f", null},
{"This is a kind of UML diagram","t", null}
};
Image[] levelOneImages = new Image[] {
new Image("res/images/l1q1.png"),
new Image("res/images/l1q2.png"),
new Image("res/images/l1q3.png"),
new Image("res/images/l1q4.png"),
new Image("res/images/l1q5.png")
};
public void levelOneInitializer(){
questionLabel.setText(levelOneData[0][0]);
questionImage.setImage(levelOneImages[0]);
System.out.println("done");
}
}
Main Class
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("gameScene1.fxml"));
primaryStage.setTitle("Info Trivia");
primaryStage.setScene(new Scene(root, 800, 450));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
gameScene1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<BorderPane maxHeight="450.0" maxWidth="800.0" minHeight="450.0" minWidth="800.0" prefHeight="400.0" prefWidth="800.0" styleClass="questionInstance" stylesheets="#style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<top>
<HBox styleClass="questheader" BorderPane.alignment="CENTER">
<children>
<ImageView pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#res/images/userICon.png" />
</image>
</ImageView>
<Label id="playerName" fx:id="playerLabel" text="Player 1" textFill="#a2c2b1">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
<Region prefHeight="0.0" prefWidth="382.0" />
<Label id="ScoreLabel" layoutX="40.0" layoutY="10.0" text="Score: " textFill="#a2c2b1">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
<Label id="scoreValue" fx:id="scoreValue" layoutX="129.0" layoutY="10.0" text="0" textFill="#a2c2b1">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
</children>
<BorderPane.margin>
<Insets left="50.0" right="50.0" />
</BorderPane.margin>
<padding>
<Insets bottom="10.0" left="15.0" right="15.0" top="10.0" />
</padding>
</HBox>
</top>
<left>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets left="50.0" />
</BorderPane.margin>
<children>
<ImageView fx:id="questionImage" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#res/images/blankimage.png" />
</image>
<StackPane.margin>
<Insets left="35.0" />
</StackPane.margin>
</ImageView>
</children>
</StackPane>
</left>
<bottom>
<HBox BorderPane.alignment="CENTER">
<children>
<Rectangle fx:id="greenBar" arcHeight="5.0" arcWidth="5.0" fill="#34b316" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="10.0" />
<Rectangle fx:id="redBar" arcHeight="5.0" arcWidth="5.0" fill="#a93535" height="28.0" stroke="BLACK" strokeType="INSIDE" strokeWidth="0.0" width="620.0" />
</children>
<padding>
<Insets bottom="40.0" left="85.0" right="85.0" />
</padding>
</HBox>
</bottom>
<center>
<BorderPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<BorderPane.margin>
<Insets right="85.0" />
</BorderPane.margin>
<top>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Label fx:id="questionLabel" text="This Is a Question" textFill="#191919">
<font>
<Font name="Arial" size="24.0" />
</font>
</Label>
</children>
</StackPane>
</top>
<center>
<HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="trueButton" mnemonicParsing="false" prefWidth="100.0" text="True">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
<Region prefWidth="200.0" />
<Button fx:id="falseButton" mnemonicParsing="false" prefWidth="100.0" text="False" />
</children>
<BorderPane.margin>
<Insets />
</BorderPane.margin>
<padding>
<Insets left="20.0" top="20.0" />
</padding>
</HBox>
</center>
</BorderPane>
</center>
</BorderPane>
You controller have to implement Initializable, Try with the following :
public class Controller implement Initializable{
//Initialize fxml controls
#FXML
public Button trueButton;
#FXML
public Button falseButton;
#FXML
public Label playerLabel;
#FXML
public Label questionLabel;
#FXML
public Label scoreValue;
#FXML
public ImageView questionImage;
#FXML
public Rectangle redBar;
#FXML
public Rectangle greenBar;
//Create array for level 1 questions
String[][] levelOneData = {
{"This is a Sequence Diagram","f", null},
{"This diagram is for a database","t", null},
{"This is a rack diagram","t", null},
{"This is a flow chart","f", null},
{"This is a kind of UML diagram","t", null}
};
Image[] levelOneImages = new Image[] {
new Image("res/images/l1q1.png"),
new Image("res/images/l1q2.png"),
new Image("res/images/l1q3.png"),
new Image("res/images/l1q4.png"),
new Image("res/images/l1q5.png")
};
public void levelOneInitializer(){
questionLabel.setText(levelOneData[0][0]);
questionImage.setImage(levelOneImages[0]);
System.out.println("done");
}
}