I made an FXML in the Scenebuilder with a Tableview and Pagination.
But I am having trouble to get the pagination to work, especially the PageFactory.
I have following code:
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Pagination?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="recordsOverview" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="accentor.overview.RecordsOverviewController">
<children>
<Pagination fx:id="paginator" onMouseClicked="#pageClicked" prefHeight="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="340.0" />
<HBox alignment="TOP_RIGHT" spacing="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="60.0">
<children>
<Label prefHeight="25.0" text="Search:" />
<TextField prefHeight="25.0" prefWidth="137.0" />
<Label prefHeight="25.0" text="Sort:" />
<ChoiceBox prefHeight="25.0" prefWidth="100.0" />
<ChoiceBox prefHeight="25.0" prefWidth="100.0" />
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</HBox>
<Label prefHeight="52.0" text="Records" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<font>
<Font size="48.0" />
</font>
</Label>
<TableView fx:id="table" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="100.0">
<columns>
<TableColumn fx:id="trackid" text="#" />
<TableColumn fx:id="title" text="Title" />
<TableColumn fx:id="length" text="Length" />
<TableColumn fx:id="album" text="Album" />
<TableColumn fx:id="artists" text="Artist(s)" />
</columns>
</TableView>
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</AnchorPane>
Controller:
public void fillTable(int page) {
this.dataModel.setTracksPage(page);
this.table.setItems(FXCollections.observableArrayList(this.dataModel.getTracks().getData()));
this.paginator.setCurrentPageIndex(page);
}
#Override
public void showOverview() {
recordsOverview.setVisible(true);
}
#Override
public void setDataModel(DataModel dataModel) {
if (this.dataModel == null) {
this.dataModel = dataModel;
}
fillTable(1);
paginator.setPageCount(this.dataModel.getTracks().getTotalPages());
this.paginator.setPageFactory((Integer pageIndex) -> {
fillTable(pageIndex);
return null; // ???
}
);
}
I just want to fill the table with new data, but returning null gives me errors.
If I return something else like return new BorderPane(table), It doesn't work either, same error.
How can I solve this issue?
Your page factory needs to return the node that will be displayed as the "page". The Pagination will then manage that node and make it part of the scene graph under it. What you want to do is display the TableView in the pagination, so you should return it from the page factory.
You didn't post any of the stack traces from the errors you mention, but I'm guessing that what's happening is that when you return the table, you get an error because the TableView is already part of the scene graph, and nodes can't exist in two different places.
One solution is to omit the table entirely from the FXML, and just define it in the controller (set it up in the initialize() method, then return the reference to it from the page factory).
If you still want it defined in the FXML file, you can define elements in FXML which are not part of the scene graph by wrapping them in a <fx:define> block.
So your FXML would look something like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Pagination?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<AnchorPane fx:id="recordsOverview" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="accentor.overview.RecordsOverviewController">
<fx:define>
<TableView fx:id="table" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="100.0">
<columns>
<TableColumn fx:id="trackid" text="#" />
<TableColumn fx:id="title" text="Title" />
<TableColumn fx:id="length" text="Length" />
<TableColumn fx:id="album" text="Album" />
<TableColumn fx:id="artists" text="Artist(s)" />
</columns>
</TableView>
</fx:define>
<children>
<Pagination fx:id="paginator" onMouseClicked="#pageClicked" prefHeight="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="340.0" />
<HBox alignment="TOP_RIGHT" spacing="20.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0" AnchorPane.topAnchor="60.0">
<children>
<Label prefHeight="25.0" text="Search:" />
<TextField prefHeight="25.0" prefWidth="137.0" />
<Label prefHeight="25.0" text="Sort:" />
<ChoiceBox prefHeight="25.0" prefWidth="100.0" />
<ChoiceBox prefHeight="25.0" prefWidth="100.0" />
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</HBox>
<Label prefHeight="52.0" text="Records" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="20.0">
<font>
<Font size="48.0" />
</font>
</Label>
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</AnchorPane>
and then you do
this.paginator.setPageFactory((Integer pageIndex) -> {
fillTable(pageIndex);
return table;
}
);
Related
I am just trying out JavaFX and am forcing my way into it because it is suppose to be the future. The problem I have is that the components don't resize with the screen. I tried to change the HBoxz HPane constraints but they seem to mess it all up.
Here is my FXML file.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<?import org.kordamp.ikonli.javafx.FontIcon?>
<AnchorPane id="AnchorPane" prefHeight="671.0" prefWidth="1020.0" stylesheets="#../../../resources/css/MainCSS.css" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.arpentechnologies.software.core.paneladmin.gestionroles.FXMLGestionRolesController">
<children>
<Button id="backButton" fx:id="volverPanelAdminButton" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" onAction="#onClickVolverInicio" prefHeight="56.0" prefWidth="54.0" styleClass="backButton" stylesheets="#../../../resources/css/MainCSS.css" AnchorPane.leftAnchor="8.0">
<graphic>
<FontIcon iconLiteral="mdi-keyboard-backspace" iconSize="30" wrappingWidth="30.0" />
</graphic>
</Button>
<StackPane layoutX="14.0" layoutY="83.0" prefHeight="50.0" prefWidth="253.0" AnchorPane.leftAnchor="8.0">
<children>
<TextField prefHeight="45.0" prefWidth="283.0" promptText="Buscar...">
<font>
<Font size="18.0" />
</font>
</TextField>
</children>
</StackPane>
<StackPane layoutX="267.0" layoutY="83.0" prefHeight="50.0" prefWidth="50.0">
<children>
<FontIcon iconLiteral="mdi-send" iconSize="36" text="" />
</children>
</StackPane>
<StackPane layoutX="860.0" layoutY="77.0" prefHeight="62.0" prefWidth="200.0" AnchorPane.rightAnchor="8.0">
<children>
<Button fx:id="anadirRolButton" mnemonicParsing="false" onAction="#onClickAnadirRol" prefHeight="39.0" prefWidth="231.0" text=" Añadir Rol">
<font>
<Font size="18.0" />
</font>
<graphic>
<FontIcon iconLiteral="mdi-account-plus" iconSize="26" />
</graphic>
</Button>
</children>
</StackPane>
<HBox layoutX="14.0" layoutY="152.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="511.0" prefWidth="1060.0">
<children>
<TableView prefHeight="495.0" prefWidth="1049.0" HBox.hgrow="ALWAYS">
<columns>
<TableColumn prefWidth="75.0" text="Nombre" />
<TableColumn prefWidth="75.0" text="Rol Padre" />
<TableColumn prefWidth="75.0" text="Módulos" />
<TableColumn prefWidth="75.0" text="Permisos" />
<TableColumn prefWidth="75.0" text="Editar" />
<TableColumn prefWidth="75.0" text="Borrar" />
</columns>
</TableView>
</children>
</HBox>
<Text layoutX="425.0" layoutY="65.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Gestión de Roles" wrappingWidth="224.0546875">
<font>
<Font size="30.0" />
</font>
</Text>
</children>
</AnchorPane>
I tried making some changes to your fxml file for obtain a responsive layout to achieve your needs
The resizable tableView and the text moves from the center to the
left
Change the Anchor Constraints of HBox which contain the tableView
Put Gestión de Roles in HBox
This is your FXML file after changes :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="671.0" prefWidth="1020.0" stylesheets="#../../../resources/css/MainCSS.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button id="backButton" fx:id="volverPanelAdminButton" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" prefHeight="56.0" prefWidth="54.0" styleClass="backButton" stylesheets="#../../../resources/css/MainCSS.css" AnchorPane.leftAnchor="8.0">
<graphic>
</graphic>
</Button>
<StackPane layoutX="14.0" layoutY="83.0" prefHeight="50.0" prefWidth="253.0" AnchorPane.leftAnchor="8.0">
<children>
<TextField prefHeight="45.0" prefWidth="283.0" promptText="Buscar...">
<font>
<Font size="18.0" />
</font>
</TextField>
</children>
</StackPane>
<StackPane layoutX="267.0" layoutY="83.0" prefHeight="50.0" prefWidth="50.0">
<children>
</children>
</StackPane>
<StackPane layoutX="860.0" layoutY="77.0" prefHeight="62.0" prefWidth="200.0" AnchorPane.rightAnchor="8.0">
<children>
<Button fx:id="anadirRolButton" mnemonicParsing="false" prefHeight="39.0" prefWidth="231.0" text=" Añadir Rol">
<font>
<Font size="18.0" />
</font>
<graphic>
</graphic>
</Button>
</children>
</StackPane>
<HBox layoutX="14.0" layoutY="152.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="511.0" prefWidth="1060.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<TableView prefHeight="495.0" prefWidth="1049.0" HBox.hgrow="ALWAYS">
<columns>
<TableColumn prefWidth="75.0" text="Nombre" />
<TableColumn prefWidth="75.0" text="Rol Padre" />
<TableColumn prefWidth="75.0" text="Módulos" />
<TableColumn prefWidth="75.0" text="Permisos" />
<TableColumn prefWidth="75.0" text="Editar" />
<TableColumn prefWidth="75.0" text="Borrar" />
</columns>
</TableView>
</children>
</HBox>
<HBox alignment="CENTER" layoutX="174.0" layoutY="14.0" prefHeight="100.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Gestión de Roles" wrappingWidth="224.0546875" HBox.hgrow="ALWAYS">
<font>
<Font size="30.0" />
</font>
</Text>
</children>
</HBox>
</children>
</AnchorPane>
I've altered AnchorPane's HBox Constraints to stay fixed, thus ensuring responsiveness
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="671.0" prefWidth="1020.0" stylesheets="#../../../resources/css/MainCSS.css" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.arpentechnologies.software.core.paneladmin.gestionroles.FXMLGestionRolesController">
<children>
<Button id="backButton" fx:id="volverPanelAdminButton" layoutX="14.0" layoutY="14.0" mnemonicParsing="false" onAction="#onClickVolverInicio" prefHeight="56.0" prefWidth="54.0" styleClass="backButton" stylesheets="#../../../resources/css/MainCSS.css" AnchorPane.leftAnchor="8.0">
<graphic>
<FontIcon iconLiteral="mdi-keyboard-backspace" iconSize="30" wrappingWidth="30.0" />
</graphic>
</Button>
<StackPane layoutX="14.0" layoutY="83.0" prefHeight="50.0" prefWidth="253.0" AnchorPane.leftAnchor="8.0">
<children>
<TextField prefHeight="45.0" prefWidth="283.0" promptText="Buscar...">
<font>
<Font size="18.0" />
</font>
</TextField>
</children>
</StackPane>
<StackPane layoutX="267.0" layoutY="83.0" prefHeight="50.0" prefWidth="50.0">
<children>
<FontIcon iconLiteral="mdi-send" iconSize="36" text="?" />
</children>
</StackPane>
<StackPane layoutX="860.0" layoutY="77.0" prefHeight="62.0" prefWidth="200.0" AnchorPane.rightAnchor="8.0">
<children>
<Button fx:id="anadirRolButton" mnemonicParsing="false" onAction="#onClickAnadirRol" prefHeight="39.0" prefWidth="231.0" text=" A�adir Rol">
<font>
<Font size="18.0" />
</font>
<graphic>
<FontIcon iconLiteral="mdi-account-plus" iconSize="26" />
</graphic>
</Button>
</children>
</StackPane>
<HBox layoutX="14.0" layoutY="152.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="511.0" prefWidth="1060.0" AnchorPane.bottomAnchor="8.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="152.0">
<children>
<TableView prefHeight="495.0" prefWidth="1049.0" HBox.hgrow="ALWAYS">
<columns>
<TableColumn prefWidth="75.0" text="Nombre" />
<TableColumn prefWidth="75.0" text="Rol Padre" />
<TableColumn prefWidth="75.0" text="M�dulos" />
<TableColumn prefWidth="75.0" text="Permisos" />
<TableColumn prefWidth="75.0" text="Editar" />
<TableColumn prefWidth="75.0" text="Borrar" />
</columns>
</TableView>
</children>
</HBox>
<Text layoutX="425.0" layoutY="65.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Gesti�n de Roles" wrappingWidth="224.0546875">
<font>
<Font size="30.0" />
</font>
</Text>
</children>
</AnchorPane>
You can also add a ScrollPane
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="671.0" prefWidth="1020.0" stylesheets="#../../../resources/css/MainCSS.css" xmlns="http://javafx.com/javafx/8.0.102" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.arpentechnologies.software.core.paneladmin.gestionroles.FXMLGestionRolesController">
<children>
<ScrollPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="670.0" prefWidth="1005.0">
<children>
<HBox layoutX="389.0" layoutY="30.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Gesti�n de Roles" wrappingWidth="241.0546875" HBox.hgrow="NEVER">
<font>
<Font size="30.0" />
</font>
</Text>
</children>
</HBox>
<Button id="backButton" fx:id="volverPanelAdminButton" layoutX="14.0" layoutY="25.0" mnemonicParsing="false" onAction="#onClickVolverInicio" prefHeight="50.0" prefWidth="115.0" styleClass="backButton" stylesheets="#../../../resources/css/MainCSS.css" AnchorPane.leftAnchor="14.0">
<graphic>
<FontIcon iconLiteral="mdi-keyboard-backspace" iconSize="30" wrappingWidth="30.0" />
</graphic>
</Button>
<StackPane layoutX="14.0" layoutY="87.0" prefHeight="50.0" prefWidth="253.0" AnchorPane.leftAnchor="14.0">
<children>
<TextField promptText="Buscar...">
<font>
<Font size="18.0" />
</font>
</TextField>
</children>
</StackPane>
<StackPane layoutX="280.0" layoutY="87.0" prefHeight="50.0" prefWidth="50.0">
<children>
<FontIcon iconLiteral="mdi-send" iconSize="36" text="?" />
</children>
</StackPane>
<StackPane layoutX="784.0" layoutY="81.0" prefHeight="62.0" prefWidth="200.0" AnchorPane.rightAnchor="21.0">
<children>
<Button fx:id="anadirRolButton" mnemonicParsing="false" onAction="#onClickAnadirRol" text=" A�adir Rol">
<font>
<Font size="18.0" />
</font>
<graphic>
<FontIcon iconLiteral="mdi-account-plus" iconSize="26" />
</graphic>
</Button>
</children>
</StackPane>
<HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" AnchorPane.bottomAnchor="8.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="152.0">
<children>
<TableView HBox.hgrow="ALWAYS">
<columns>
<TableColumn prefWidth="75.0" text="Nombre" />
<TableColumn prefWidth="75.0" text="Rol Padre" />
<TableColumn prefWidth="75.0" text="M�dulos" />
<TableColumn prefWidth="75.0" text="Permisos" />
<TableColumn prefWidth="75.0" text="Editar" />
<TableColumn prefWidth="75.0" text="Borrar" />
</columns>
</TableView>
</children>
</HBox>
</children>
</AnchorPane>
</content>
</ScrollPane>
</children>
</AnchorPane>
I am working on a chatbot project in java and for the GUI I am using JavaFX, and IDE eclipse oxygen and scene builder 8.4.1.
I am having a problem adding a background image to a text area. here is a screen shot of what I did and it just shows nothing(not even an error).
following is the fxml code generated by scene builder:
<?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.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1">
<top>
<AnchorPane prefHeight="56.0" prefWidth="600.0" style="-fx-background-color: blue;" BorderPane.alignment="CENTER">
<children>
<ImageView fitHeight="51.0" fitWidth="79.0" layoutX="23.0" layoutY="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#../../../Downloads/background.jpg" />
</image></ImageView>
<Label layoutX="118.0" layoutY="36.0" text="Ibot" />
</children>
</AnchorPane>
</top>
<center>
<TextArea editable="false" prefHeight="200.0" prefWidth="200.0" style="-fx-background-image: url("../../../Downloads/background.jpg"); -fx-background-repeat: stretch;" BorderPane.alignment="CENTER" />
</center>
<bottom>
<HBox BorderPane.alignment="CENTER">
<children>
<TextField prefHeight="25.0" prefWidth="476.0" promptText="Type in here" HBox.hgrow="ALWAYS">
<HBox.margin>
<Insets />
</HBox.margin>
</TextField>
<Region prefHeight="25.0" prefWidth="60.0" />
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="57.0" text="Send">
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
</children>
<BorderPane.margin>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</BorderPane.margin>
<padding>
<Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
</padding>
</HBox>
</bottom>
</BorderPane>
It's probably not working because as specify in JavaFX CSS Reference Guide the TextArea is a association of two substructure: a ScrollPane and a Region. You have to change the style of those.
In CSS this should work:
#text-area-id .content{
-fx-background-image : url("the_path_to_your_image")
/* others properties */
}
Edit to answer comment:
For a button you just need to set the property -fx-background-image:
#your-button{
-fx-background-image : url("the_path_to_your_image")
}
I recently found the google-material-icons and now i want to make my application more good looking using icons. Right now i want to add an image to the application close MenuItem. With SceneBuilder you can just add items, but what i now wanna do is at least still use fxml. I have two questions:
Can I edit the fxml without it being overwritten by scenebuilder again?
how can i add the icon to MenuItems?
Thank you very much. In case you need it, here my FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" stylesheets="#../styles/Styles.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.freakyonline.ucone.FXMLController">
<center>
<TabPane prefHeight="167.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
<tabs>
<Tab closable="false" text="Players">
<content>
<TableView fx:id="playerTable" editable="true" onContextMenuRequested="#handlePTContextMenuRequest" onInputMethodTextChanged="#handleTextChanged" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="nickColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="75.0" text="Nickname" />
<TableColumn fx:id="groupColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="75.0" text="Group" />
<TableColumn fx:id="yearOfBirthColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="94.0" text="Year of Birth" />
<TableColumn fx:id="ageColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="72.0" text="Age" />
<TableColumn fx:id="genderColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="59.0" text="Gender" />
<TableColumn fx:id="lastQuitColumn" onEditCommit="#handlePlayerEditCommit" prefWidth="75.0" text="Last Quit" />
</columns>
<padding>
<Insets bottom="3.0" left="5.0" right="5.0" top="3.0" />
</padding>
</TableView>
</content>
</Tab>
<Tab fx:id="consoleOneTab" closable="false" onSelectionChanged="#handleConsoleOneTabSelected" text="ConsoleOne">
<content>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<TextArea fx:id="consoleOneTextArea" editable="false" wrapText="true" VBox.vgrow="ALWAYS" />
<TextField fx:id="consoleOneTextField" alignment="TOP_LEFT" onAction="#handleConsoleOneAction" promptText="type here ..." />
</children>
</VBox>
</content></Tab>
</tabs>
</TabPane>
</center>
<top>
<MenuBar fx:id="mainMenuBar" BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" onAction="#handleFileClose" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" onAction="#handleHelpAbout" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>
Yes, you can add an Image in your MenuItem without affect on fxml.
First, add FX:ID to your menu-item.
<MenuItem fx:id="close_item" mnemonicParsing="false" onAction="#handleFileClose" text="Close" />
Now use setGraphic() method to add an ImageView in your menu-item.
#FXML MenuItem close_item;
#Override
public void initialize(URL url, ResourceBundle rb) {
ImageView menuIcon = new ImageView(new Image("/path/image.png"));
menuIcon.setFitHeight(20);
menuIcon.setFitWidth(20);
close_item.setGraphic(menuIcon);
//...
//...
}
I'm having this weird behavior when I run my JavaFx app:
As you can see the elements aren't properly aligned. I've created that view using SceneBuilder and there isn't that gap in the editor.
My view is composed of a GridPane. The right cells (on the pic) of the GridPane each contains an HBox, each containing elements (buttons / text fields). I haven't configured anything specific here and am not using any CSS.
Here's the full FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MyController">
<children>
<GridPane hgap="3.0" vgap="3.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="NEVER" />
<ColumnConstraints hgrow="NEVER" />
<ColumnConstraints hgrow="ALWAYS" />
</columnConstraints>
<rowConstraints>
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="label1" />
<Label text="label2" GridPane.rowIndex="1" />
<Button defaultButton="true" mnemonicParsing="false" onAction="#handleGo" text="Go!" GridPane.rowIndex="2">
<graphic>
<TextField fx:id="iterations" alignment="CENTER" prefWidth="43.0" style="-fx-padding: 0.166667em 0.333333em 0.166667em 0.333333em;" text="50000" />
</graphic></Button>
<Button fx:id="selectFileButton" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#handleFileSelection" text="Select file" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<HBox spacing="3.0" GridPane.columnIndex="2">
<children>
<TextField fx:id="customValue" editable="false" HBox.hgrow="ALWAYS" />
<Button fx:id="deleteButton" mnemonicParsing="false" onAction="#handleDelete" text="Delete" visible="false" />
</children>
</HBox>
<HBox spacing="3.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
<children>
<Button fx:id="editButton" mnemonicParsing="false" text="Edit" />
<Button mnemonicParsing="false" text="New" />
</children>
</HBox>
<ComboBox fx:id="selection" maxWidth="1.7976931348623157E308" onAction="#handleSelection" GridPane.columnIndex="1" />
</children>
</GridPane>
</children>
</AnchorPane>
Am I missing something ?
The default alignment for controls in GridPane (Maybe for controls in general?) is CENTER_LEFT, while for HBox the default is TOP_LEFT. Adding
alignment="CENTER_LEFT"
to your HBox should align them properly.
When I increase the window, the inner elements stay at the same size.
I want that when I increase the window, that the elements also get larger/scale
Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane cacheHint="SCALE_AND_ROTATE" focusTraversable="true" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TableView fx:id="finalTable" layoutX="27.0" layoutY="358.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="190.0" prefWidth="766.0" />
<Label layoutX="27.0" layoutY="21.0" prefHeight="25.0" prefWidth="149.0" text=" Quell-Datei" />
<TableView fx:id="sourceTable" editable="true" layoutX="27.0" layoutY="50.0" maxHeight="900.0" maxWidth="900.0" minHeight="-Infinity" minWidth="-Infinity" prefHeight="190.0" prefWidth="766.0" />
<Label layoutX="27.0" layoutY="329.0" prefHeight="25.0" prefWidth="149.0" text=" Konvertierte-Datei" />
<Button fx:id="linkBtn" layoutX="313.0" layoutY="282.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#linkAction" prefHeight="30.0" prefWidth="90.0" text="Verbinden" />
<Button fx:id="splitBtn" layoutX="437.0" layoutY="282.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#splitAction" prefHeight="30.0" prefWidth="90.0" text="Trennen" />
</children>
</AnchorPane>
Im working with SceneBuilder 2.0, and I have also tried to "anchor" a button
(see here: http://i.imgur.com/GZyL5xC.png)
...but the scaling is completely wrong (see here: http://i.imgur.com/hmMi1p3.png)
I searched the whole internet for an answer, but I found nothing that could help.
Well your layout is typically a VBox layout. If your Windows isn't at a fixed size this will be a good solution, because your Buttons stay at the same height and in the center of your window. The TableViews grow and shrink as you resize the window. Like you want.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label prefHeight="25.0" prefWidth="149.0" text=" Quell-Datei" VBox.vgrow="NEVER" />
<TableView fx:id="sourceTable" editable="true" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
<Label />
<HBox alignment="CENTER" prefHeight="61.0" prefWidth="766.0" spacing="20.0" VBox.vgrow="NEVER">
<children>
<Button fx:id="linkBtn" maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" onAction="#linkAction" prefHeight="30.0" prefWidth="90.0" text="Verbinden" HBox.hgrow="NEVER" />
<Button fx:id="splitBtn" maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" onAction="#splitAction" prefHeight="30.0" prefWidth="90.0" text="Trennen" HBox.hgrow="NEVER" />
</children>
</HBox>
<Label prefHeight="25.0" prefWidth="149.0" text=" Konvertierte-Datei" VBox.vgrow="NEVER" />
<TableView fx:id="finalTable" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
</children>
<padding>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
</padding>
</VBox>