I have GridPane in my JavaFX window and I span two columns.
When I click on grid lines visible in scenebuilder there is line in the middle of merged cells.
How can I remove or not show this line?
Here is one approach: In this approach, I make the GridPane's background black. Then I stretch each Node to fill their Cell and make the Node's background white. Also on the GridPane, I set all the Insets to 5 and the VGap and HGap to 5. I suggest you use CSS and take #James_D's approach here.
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane hgap="5.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: black;" vgap="5.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60">
<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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" style="-fx-background-color: white;" text="Label" GridPane.columnSpan="2147483647" />
<Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" style="-fx-background-color: white;" text="Label" GridPane.rowIndex="1" />
<Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" style="-fx-background-color: white;" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" style="-fx-background-color: white;" text="Label" GridPane.rowIndex="2" />
<Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" style="-fx-background-color: white;" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</GridPane>
But I would suggest you use CSS and follow #James_D's approach here.
Related
In scene builder preview button is on exact spot i.e bottom right corner without any gap but when i run my application there is gap.
Button is on a pane and pane is placed on top of anchor pane.
How can i fix this issue?
same thing happening with other components when i placed them on far right or far bottom.
Please examin the sample. Pay attention to min_width, min_height, pref_width, pref_height, max_width, max_height, vgrow, hgrow attributues in SceneBuiler.
The main concept is that type of pane you use sholud give you desired layout. It should not be hardcoded.
<?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.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox style="-fx-background-color: yellow;">
<children>
<StackPane VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
</StackPane>
<StackPane VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
</StackPane>
<StackPane layoutX="10.0" layoutY="210.0" VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
</StackPane>
<StackPane layoutX="10.0" layoutY="276.0" VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</StackPane>
<StackPane layoutX="10.0" layoutY="310.0" VBox.vgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Menu Button" />
</children>
</StackPane>
</children>
</VBox>
<VBox style="-fx-background-color: red;" HBox.hgrow="ALWAYS">
<children>
<StackPane prefHeight="150.0" prefWidth="200.0" style="-fx-background-color: green;" VBox.vgrow="ALWAYS">
<children>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" style="-fx-background-color: purple;">
<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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Label" />
<TextField GridPane.columnIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Label text="Label" GridPane.rowIndex="1" />
<Label text="Label" GridPane.rowIndex="2" />
</children>
</GridPane>
</children>
</StackPane>
<HBox alignment="TOP_RIGHT">
<children>
<Button mnemonicParsing="false" text="Button" />
</children>
</HBox>
</children>
</VBox>
</children>
</HBox>
Have you put constraints in the layout section of the button? It could be related to that. Check layout x and layout of the button and the pref size x and y of AnchorPane, if that is same, the button must be on the left-bottom corner of the pane.
I want to create three pop-up windows in JavaFX. These windows are almost the same, but they differ in styles. For example, the user deletion window has a black title field, green to activate the user, and red to block. I want to create only one such window in FXML, and then pass the styles as a parameter. How can I do this and is this even possible?
you can either create a custom dialog window or use javafx native dialog window
---------------here i i have created a custom dialog window and i change the colors and icons dynamically as i need
-------FXML-------------------
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?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.HBox?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane xmlns:fx="http://javafx.com/fxml/1" style="-fx-border-color: red;" xmlns="http://javafx.com/javafx/8.0.141">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="194.0" minHeight="10.0" prefHeight="117.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="113.0" minHeight="10.0" prefHeight="113.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<AnchorPane prefHeight="113.0" prefWidth="400.0" style="-fx-background-color: e34c5e;">
<children>
<ImageView fitHeight="82.0" fitWidth="104.0" layoutX="153.0" layoutY="18.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="17.0" AnchorPane.leftAnchor="153.0" AnchorPane.rightAnchor="165.0" AnchorPane.topAnchor="18.0">
<image>
<Image url="#../Images/eror_icon.png" />
</image>
</ImageView>
</children></AnchorPane>
<AnchorPane layoutX="10.0" layoutY="10.0" prefHeight="129.0" prefWidth="400.0" style="-fx-background-color: white;" GridPane.rowIndex="1">
<children>
<GridPane layoutX="84.0" layoutY="-5.0" prefHeight="99.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="43.0" minHeight="10.0" prefHeight="39.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="27.0" minHeight="10.0" prefHeight="22.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="36.0" minHeight="10.0" prefHeight="36.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" contentDisplay="CENTER" prefHeight="50.0" prefWidth="401.0" text="Item Not Selected !">
<font>
<Font name="System Bold" size="19.0" />
</font>
</Label>
<Label alignment="CENTER" layoutX="10.0" layoutY="43.0" prefHeight="50.0" prefWidth="401.0" text="Item to Purchase Is Not Selected..Select an Item" GridPane.rowIndex="1" />
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" GridPane.rowIndex="2">
<children>
<JFXButton fx:id="btnOk" buttonType="RAISED" onMouseClicked="#btnOkClicked" prefHeight="32.0" prefWidth="121.0" style="-fx-background-color: e34c5e; -fx-background-radius: 80;" text="Ok" textFill="WHITE">
<HBox.margin>
<Insets bottom="2.0" top="2.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="15.0" />
</font>
</JFXButton>
</children>
</HBox>
</children>
</GridPane>
</children>
</AnchorPane>
</children>
</GridPane>
--------------------Controller--------
package Controllers;
import javafx.scene.control.Dialog;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class CustomAlertDialogBox {
private Dialog currentDialog;
public CustomAlertDialogBox(Dialog dialog) {
this.currentDialog = dialog;
}
public void btnOkClicked(MouseEvent mouseEvent) {
Stage stage = (Stage) currentDialog.getDialogPane().getScene().getWindow();
stage.close();
}
}
check this for native dialog box
http://code.makery.ch/blog/javafx-dialogs-official/
my gridpane is :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="569.0" prefWidth="794.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.controller.test">
<children>
<GridPane layoutX="10.0" layoutY="270.0" prefWidth="775.0" rotate="0.0">
<children>
<Button fx:id="btn1" mnemonicParsing="false" prefWidth="67.0" rotate="0.0" text="btn1" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<Button fx:id="btn2" mnemonicParsing="false" prefWidth="67.0" rotate="0.0" text="btn2" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Button fx:id="btn3" mnemonicParsing="false" prefWidth="67.0" rotate="0.0" text="btn3" GridPane.columnIndex="2" GridPane.rowIndex="0" />
<Button fx:id="btn4" mnemonicParsing="false" prefWidth="67.0" rotate="0.0" text="btn4" GridPane.columnIndex="3" GridPane.rowIndex="0" />
</children>
<columnConstraints>
<ColumnConstraints maxWidth="594.0" minWidth="10.0" prefWidth="69.0" />
<ColumnConstraints maxWidth="594.0" minWidth="10.0" prefWidth="69.0" />
<ColumnConstraints maxWidth="558.0" minWidth="10.0" prefWidth="67.0" />
<ColumnConstraints maxWidth="507.0" minWidth="10.0" prefWidth="507.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
i need the result like below image when visible(false) for btn1,btn2
i use scene builder. thanks
In addition to making the components invisible by setting the visible property to false ('visible="false"'), you have set the managed property to false (managed="false").But with explicit 'ColumnConstraints' in your fxml, 'managed=false' will not have effect. So you may want to avoid using ColumnConstraints and instead use GridPane child component's properties to set dimensions.
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.
I am trying to add datepicker field in .fxml (JavaFX). But I don't what is correct way to do?
Please help me how to add datepicker field in .fxml file.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="705.9998779296875" prefWidth="499.9998779296875" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="processbuilder.controler.NewActivityController">
<children>
<GridPane prefHeight="615.9998779296875" prefWidth="499.9998779296875" AnchorPane.bottomAnchor="90.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label prefHeight="24.0" prefWidth="53.0" text="Name : " GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0" GridPane.valignment="CENTER" />
<Label text="Description :" translateY="30.0" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="TOP" />
<TextField fx:id="name" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<TextArea fx:id="desc" prefWidth="200.0" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="177.0" minWidth="10.0" prefWidth="76.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="394.0" minWidth="10.0" prefWidth="394.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="194.0" minHeight="10.0" prefHeight="49.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="317.0" minHeight="10.0" prefHeight="265.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="194.0" minHeight="10.0" prefHeight="42.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="261.0" minHeight="10.0" prefHeight="261.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<Button fx:id="create" mnemonicParsing="false" onAction="#handleCreateActivity" prefWidth="64.0" text="Create" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="345.0" AnchorPane.rightAnchor="91.0" />
<Button fx:id="cancel" mnemonicParsing="false" onAction="#handleCancel" prefHeight="20.9998779296875" prefWidth="63.9998779296875" text="Cancel" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="116.0" AnchorPane.rightAnchor="320.0" />
<Button fx:id="modify" layoutX="345.0" layoutY="655.0" mnemonicParsing="false" onAction="#handleModify" prefWidth="63.9998779296875" text="Modify" visible="false" />
</children>
</AnchorPane>
Just use a DatePicker tag to add a DatePicker in your fxml
A sample example where I have added a DatePicker to the provided FXML is:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="705.9998779296875" prefWidth="499.9998779296875" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<GridPane prefHeight="615.9998779296875" prefWidth="499.9998779296875" AnchorPane.bottomAnchor="90.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label prefHeight="24.0" prefWidth="53.0" text="Name : " GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="0" GridPane.valignment="CENTER" />
<Label text="Description :" translateY="30.0" GridPane.columnIndex="0" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="TOP" />
<TextField fx:id="name" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<TextArea fx:id="desc" prefWidth="200.0" wrapText="true" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label prefHeight="24.0" prefWidth="87.0" text="DatePicker : " GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER" />
<DatePicker fx:id="datepicker" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="177.0" minWidth="10.0" prefWidth="76.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="394.0" minWidth="10.0" prefWidth="394.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="194.0" minHeight="10.0" prefHeight="49.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="317.0" minHeight="10.0" prefHeight="265.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="194.0" minHeight="10.0" prefHeight="42.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="261.0" minHeight="10.0" prefHeight="261.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<Button fx:id="create" mnemonicParsing="false" onAction="#handleCreateActivity" prefWidth="64.0" text="Create" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="345.0" AnchorPane.rightAnchor="91.0" />
<Button fx:id="cancel" mnemonicParsing="false" onAction="#handleCancel" prefHeight="20.9998779296875" prefWidth="63.9998779296875" text="Cancel" AnchorPane.bottomAnchor="30.0" AnchorPane.leftAnchor="116.0" AnchorPane.rightAnchor="320.0" />
<Button fx:id="modify" layoutX="345.0" layoutY="655.0" mnemonicParsing="false" onAction="#handleModify" prefWidth="63.9998779296875" text="Modify" visible="false" />
</children>
</AnchorPane>
<DatePicker xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" />
But if you work with FXML you should use the Scene Builder, which is a WYSIWYG editor.