How to create a checkbox in a button? (javafx) - javafx

How to create a checkbox in a button? If it is possible to create it through the fxml file it would be better for me, but also another option would be good.
I tried to put the CheckBox tag inside the Button tag. But it didn't work
<Button text="Write All" styleClass="smallSideButton" style="-fx-pref-width: 120px;" GridPane.rowIndex="2" GridPane.columnIndex="7">
<CheckBox/>
</Button>

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.layout.StackPane?>
<StackPane 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">
<children>
<Button mnemonicParsing="false" text="Button">
<graphic>
<CheckBox mnemonicParsing="false" text="CheckBox" />
</graphic>
</Button>
</children>
</StackPane>

Related

JavaFX FXML Align to the left

I'm trying to align the Text to the left but keep the picture in the center. How can I achieve this? (the image can be change, it's just there for reference)
Here's my code:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<VBox prefHeight="300.0" prefWidth="600.0" alignment="CENTER" fx:controller="controller.LoginController" xmlns:fx="http://javafx.com/fxml/1" spacing="10">
<HBox alignment="TOP_RIGHT" spacing="10">
<Button fx:id="loginBtn" text="Login" onAction="#handleLogin"/>
<Button fx:id="exitBtn" text="Exit" onAction="#handleExit" />
</HBox>
<Label text="STPFX" />
<Label text="Payroll Management System"/>
<ImageView fitHeight="300" fitWidth="300" >
<image><Image url ="#stp.jpg" /></image>
</ImageView>
</VBox>
You should create VBox for the 2 labels with alignment = left and maxWidth.
<VBox prefHeight="300.0" prefWidth="600.0" alignment="CENTER" fx:controller="controller.LoginController" xmlns:fx="http://javafx.com/fxml/1" spacing="10">
<HBox alignment="TOP_RIGHT" spacing="10">
<Button fx:id="loginBtn" text="Login" onAction="#handleLogin"/>
<Button fx:id="exitBtn" text="Exit" onAction="#handleExit" />
</HBox>
<VBox alignment="CENTER_LEFT" maxWidth="1.7976931348623157E308">
<Label text="STPFX" />
<Label text="Payroll Management System"/>
</VBox>
<ImageView fitHeight="300" fitWidth="300" >
<image><Image url ="#stp.jpg" /></image>
</ImageView>
</VBox>

JavaFx layout missbeheavior with prefered size and min Size

Disclaimer: I am a JavaFx beginner and come from the wpf world
I have a problem with the behavior of the prefered height and the max height. The min height is ignored and the prefered height is used. What do i wrong?
Code Example: (this is only to illustrate the problem)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<VBox fx:controller="sample.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Pane minHeight="100.0" style="-fx-background-color: lightblue;" VBox.vgrow="ALWAYS" />
<Pane fx:id="buttonPane" style="-fx-background-color: lightgreen;">
<children>
<Button minHeight="0.0" minWidth="0.0" onAction="#buttonPaneClick" prefHeight="200.0" text="Button" />
</children>
</Pane>
</children>
</VBox>
Problem: the Button is only half showed and not scaled down. But the min height is 0 so i would expectd that the button goes to 0 when not more place is available but he is cropped out.
From the Pane documentation:
This class may be used directly in cases where absolute positioning of children is required since it does not perform layout beyond resizing resizable children to their preferred sizes.
(my emphasis).
So the Pane containing the button will resize it to its preferred size no matter what. If you replace the pane containing the button with a layout container that does more layout than this (e.g. a HBox) then you will see the behavior you are expecting:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Pane minHeight="100.0" style="-fx-background-color: lightblue;" VBox.vgrow="ALWAYS" />
<HBox fx:id="buttonPane" style="-fx-background-color: lightgreen;">
<children>
<Button minHeight="0.0" minWidth="0.0" prefHeight="200.0" text="Button" />
</children>
</HBox>
</children>
</VBox>

Javafx background image using scene builder

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")
}

ButtonBar changes position when Stage is resizable

I have a BoarderPane (decoration: UTILITY) where a ButtonBar with two buttons is put to BOTTOM:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pw.yaps.client.jfx.view.SettingsViewController">
<bottom>
<ButtonBar BorderPane.alignment="CENTER">
<buttons>
<Button disable="true" mnemonicParsing="false" text="OK" />
<Button cancelButton="true" mnemonicParsing="false" onAction="#handleCancelAction" text="Cancel" />
</buttons>
<padding>
<Insets bottom="12.0" right="12.0" />
</padding>
</ButtonBar>
</bottom>
</BorderPane>
When the Stage is NOT resizable the layout looks like that:
but when I set it resizable it looks different:
How can this behavior be avoided?

Video exceeds window's size - JavaFX and FXML

i've a trouble with JavaFX and FXML.
I've a simple FXML file structured in this way:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.media.MediaView?>
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="480.0" minWidth="720.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mediaplayer.MediaPlayerControllerSplit">
<center>
<SplitPane dividerPositions="0.5" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" BorderPane.alignment="CENTER">
<items>
<AnchorPane fx:id="mediaPaneL" style="-fx-background-color: green;">
<children>
<MediaView fx:id="mediaViewL" />
</children>
</AnchorPane>
<AnchorPane fx:id="mediaPaneR" style="-fx-background-color: red;">
<children>
<MediaView fx:id="mediaViewR" />
</children>
</AnchorPane>
</items>
</SplitPane>
</center>
<bottom>
<ToolBar minHeight="-Infinity" minWidth="-Infinity" style="-fx-background-color: black;" BorderPane.alignment="BOTTOM_CENTER">
<items>
<Button mnemonicParsing="false" onAction="#openVideo" text="Openfile" />
<Button mnemonicParsing="false" onAction="#playVideo" text=">" />
<Button mnemonicParsing="false" onAction="#pauseVideo" text="||" />
<Button mnemonicParsing="false" onAction="#stopVideo" text="stop" />
<Button mnemonicParsing="false" text="<<<" />
<Button mnemonicParsing="false" text="<<" />
<Button mnemonicParsing="false" text=">>" />
<Button mnemonicParsing="false" text=">>>" />
<Button mnemonicParsing="false" onAction="#exitVideo" text="EXIT" />
</items>
</ToolBar>
</bottom>
</BorderPane>
The controller will load 2 videos and show them in the relative 2 AnchorPanes inside the SplitView, as shown in the following image:
ClickMe
The video exceeds from width bounds of the Parent AnchorPane, how can i set them properly?
Thanks all.
I've solved this with the following line of codes in the Controller class:
mediaViewL.fitWidthProperty().bind(mediaPaneL.widthProperty());
The left (same for the right one) mediaView width has been fitted to his first Parent width (AnchorPaneL / AnchorPaneR).

Resources