Remove the space between the bars in a bar chart? - java

Is it possible to remove the space between the bars in a bar chart in JavaFX? Where is the option?

barGap and/ or categoryGap does this:
<BarChart fx:id="chart" barGap="0" categoryGap="0" animated="false" maxWidth="Infinity"
HBox.hgrow="ALWAYS" maxHeight="100" prefHeight="60" minHeight="40">
<xAxis>
<CategoryAxis fx:id="xAxis" tickLabelsVisible="false" />
</xAxis>
<yAxis>
<NumberAxis fx:id="yAxis" tickLabelsVisible="false" />
</yAxis>
</BarChart>

Related

LoadException on adding a controller to fxml file in Java 8 [duplicate]

This question already has answers here:
Why I'm getting javafx.fxml.LoadException even the path of the fxml file is correct
(2 answers)
Closed 4 years ago.
Every time i add a controller to the fxml file, i get an exception: javafx.fxml.LoadException, and when i print the exception message it shows the right directory to the file
I am using netbeans 8 IDE, Java 8 and Scene builder for the GUI
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Text?>
<AnchorPane id="AnchorPane" prefHeight="419.0" prefWidth="875.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="organizer.Controller.DeleteCategoryController">
<children>
<Pane layoutX="9.0" layoutY="108.0" prefHeight="283.0" prefWidth="550.0" style="-fx-border-width: 1pt; -fx-border-color: black;">
<children>
<Text fx:id="textDeleteThen" layoutX="195.0" layoutY="27.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Delete ... category then" />
<Button layoutX="14.0" layoutY="166.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 3
Create a new Relationship
and move contacts having .....
relationship to this relationship" />
<Button layoutX="266.0" layoutY="49.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 2
Keep all contacts
with the .... relationship" />
</children></Pane>
<Pane layoutX="569.0" layoutY="19.0" prefHeight="372.0" prefWidth="272.0" style="-fx-border-width: 1pt; -fx-border-color: black;" />
<Text layoutX="41.0" layoutY="73.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Choose one of the following options" wrappingWidth="447.681640625" />
<Text fx:id="textDeletingCategory" layoutX="42.0" layoutY="24.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Deleting ..." />
<Text fx:id="textMessage" layoutX="42.0" layoutY="48.0" strokeType="OUTSIDE" strokeWidth="0.0" text="After deleting .... do the following:" />
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="279.0" layoutY="271.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 4
Change Contacts having
.... relationship
to the following
existing relationship" />
<Button layoutX="23.0" layoutY="153.0" mnemonicParsing="false" prefHeight="103.0" prefWidth="241.0" text="Option 1
Delete all contacts
with the ..... relationship" />
<TextField layoutX="585.0" layoutY="56.0" prefHeight="25.0" prefWidth="184.0" />
<ListView fx:id="listViewRemainingCategories" layoutX="580.0" layoutY="138.0" prefHeight="236.0" prefWidth="241.0" />
<Text layoutX="580.0" layoutY="115.0" strokeType="OUTSIDE" strokeWidth="0.0" text="List of other categories (for option 4)" />
<Text layoutX="585.0" layoutY="39.0" strokeType="OUTSIDE" strokeWidth="0.0" text="New Category Name (for option 3)" />
</children>
</AnchorPane>
And Controller:
package organizer.Controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.text.Text;
import organizer.Model.SQL_Objects;
public class DeleteCategoryController extends SQL_Objects implements Initializable{
#FXML private ListView listViewRemainingCategories;
#FXML private Text textDeletingCategory;
#FXML private Text textMessage;
#FXML private Text textDeleteThen;
private int toRemoveId;
private String toRemoveName;
private int associatedRecordsCount;
public DeleteCategoryController(String toRemoveName, int toRemoveId, int associatedRecordCount) {
this.toRemoveName = toRemoveName;
this.toRemoveId = toRemoveId;
this.associatedRecordsCount = associatedRecordCount;
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
all other FXML files in my program work well, it is just this FXML file, and no matter which controller i add to it, it prevents it from loading, when i remove the controller attribute it opens normally.
Full stack trace:
javafx.fxml.LoadException:
file:/D:/Personal/Projects/Tools/Java/Organizer/dist/run798015614/Organizer.jar!/organizer/View/DeleteCategory.fxml:10
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at organizer.Controller.Main.switchWindow(Main.java:32)
at organizer.Controller.Main.start(Main.java:21)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.InstantiationException: organizer.Controller.DeleteCategoryController
at java.lang.Class.newInstance(Class.java:427)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
... 23 more
Caused by: java.lang.NoSuchMethodException: organizer.Controller.DeleteCategoryController.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.newInstance(Class.java:412)
... 25 more
Any idea why ?
I appreciate your help
So i found the solution, i will post here so someone might make use of that
When i removed the constructor the program worked, looks like in the JavaFx applications you can only have the default constructor, however if you need to add parameters to the constructor you then need to use what is known as ControllerFactory.

How to change space between two buttons as the window expands? [duplicate]

I am building a UI using Java FX scene builder and I want a button in a toolbar to float towards the right side of the toolbar. I have tried changing the node orientation of the parent(toolbar) and also the button but both seem to be ignored.
Add a pane with no content which always grows to fit available space between the left aligned tools in the bar and right aligned ones.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<ToolBar prefHeight="40.0" prefWidth="318.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<Button text="Apples" />
<Button text="Oranges" />
<Pane HBox.hgrow="ALWAYS" />
<Button text="Help" />
</ToolBar>
(Verified in Scene Builder 11.0.0)
One option: Use two ToolBar containers wrapped with an HBox container. Put the Help button in the right ToolBar. Put the left-aligned buttons in the left toolbar. For the left ToolBar, set the HGrow to ALWAYS. For the Right ToolBar, set HGrow to NEVER. For each ToolBar, set all Sizes to USE_COMPUTED_SIZE.
Here is the relevant fxml:
<HBox VBox.vgrow="NEVER">
<children>
<ToolBar HBox.hgrow="ALWAYS">
<items>
<Button text="Apples" />
<Button text="Oranges" />
</items>
</ToolBar>
<ToolBar HBox.hgrow="NEVER">
<items>
<Button text="Help" />
</items>
</ToolBar>
</children>
</HBox>
This is the hierarchy as displayed in Scene Builder:
This is the display within Scene Builder:
If you could place ur button inside a stack pane then u could make use of Stackpane's alignment property that takes javafx.geometry.Pos - The alignment of the child within the stackpane.For example in ur case:
<StackPane >
<Button translateY="-15" translateX="15" StackPane.alignment="TOP_RIGHT"/>
</StackPane>
BorderPane mainBorderPane = new BorderPane();
BorderPane ToolBorderPane = new BorderPane();
ToolBar tBarLeft=new ToolBar();
ToolBar tBarRight=new ToolBar();
ToolBorderPane.setLeft(tBarLeft);
ToolBorderPane.setRight(tBarRight);
mainBorderPane.setTop(ToolBorderPane);
...
...
for your left aligment items add tBarLeft and
your right aligment items add tBarRight
It can be done with some hack: apply 180 degree rotation around Y-axis for the toolbar and all its buttons.
<ToolBar rotate="-180.0" HBox.hgrow="ALWAYS">
<items>
<Button mnemonicParsing="false" rotate="180.0" text="Button">
<rotationAxis>
<Point3D y="1.0" />
</rotationAxis>
</Button>
</items>
<rotationAxis>
<Point3D y="1.0" />
</rotationAxis>
</ToolBar>

How do I initialize a double array of Circle Objects in a JavaFXML controller with two scenes?

I’m coding with JavaFXML and Gluon Scene Builder 8.0.0 to create a pixel editor app. I have created two .fxml files, one for drawing tools (sample.fxml) and the other for an array of Circle Objects (32 x 32) that represents a pixel array of LEDs (PixelEditor.fxml). Both share the same controller (Controller.java).
I can’t initialize my Circle[][] array in Controller.java when the user clicks on a menu item like 32h x 32w. I used a 4 x4 array to test my code:
public void handleMenuAction(ActionEvent event) throws IOException {
if(event.getSource() == menu32hx32w) {
Stage pixelStage = new Stage();
Parent pixelRoot = FXMLLoader.load(getClass().getResource("PixelEditor.fxml"));
Scene pixelScene = new Scene(pixelRoot);
pixelStage.setTitle("Pixel Array: 32h X 32w");
pixelStage.setScene(pixelScene);
pixelStage.setX(0.0);
pixelStage.setY(0.0);
pixelStage.show();
Circle[][] pixelArray = {
{R0C0, R0C1, R0C2, R0C3},
{R1C0, R1C1, R1C2, R1C3},
{R2C0, R2C1, R2C2, R2C3},
{R3C0, R3C1, R3C2, R3C3},
};
}
}
If I print out the array I get:
pixelArray:
null null null null
null null null null
null null null null
null null null null
When I had only one .fxml containing all the Objects I could initialize the pixelArray. I use fx:id to reference the Circle Objects but placing them in a separate Stage and Scene seems to de-reference them and create a null elements.
What am I not doing?
Previously, with one .fxml file, all I needed to assign values to the Circle Objects was to reference their fx:id in the Controller.java as follows:
#FXML
private Circle
R0C0, R0C1, R0C2, R0C3,
R1C0, R1C1, R1C2, R1C3,
R2C0, R2C1, R2C2, R2C3,
R3C0, R3C1, R3C2, R3C3;
This is what I'm still doing but the assigned properties via the fx:id reference don't seem to connect?
The PixelEditor.fxml is quite large because I have 32x32 = 1024 Circle Objects even though I'm just testing with the first 4x4. The code for the first row looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane fx:id="panePixelLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="776.0" prefWidth="776.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<VBox prefHeight="776.0" prefWidth="776.0" style="-fx-background-color: #000000;">
<children>
<HBox prefHeight="24.0" prefWidth="776.0" style="-fx-background-color: #000000;">
<children>
<Circle fx:id="R0C0" fill="DODGERBLUE" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
<HBox.margin>
<Insets left="8.0" top="8.0" />
</HBox.margin>
</Circle>
<Circle fx:id="R0C1" fill="DODGERBLUE" layoutX="22.0" layoutY="22.0" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
<HBox.margin>
<Insets left="8.0" top="8.0" />
</HBox.margin>
</Circle>
<Circle fx:id="R0C2" fill="DODGERBLUE" layoutX="22.0" layoutY="22.0" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
<HBox.margin>
<Insets left="8.0" top="8.0" />
</HBox.margin>
</Circle>
<Circle fx:id="R0C3" fill="DODGERBLUE" layoutX="42.0" layoutY="22.0" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
<HBox.margin>
<Insets left="8.0" top="8.0" />
</HBox.margin>
</Circle>
It looks like you haven't actually set values to any of: R0C0, R0C1, etc. If these variables are set to null when you create the array, they will still be null even if you set them later.
You haven't shown the part of your code where you assign R0C0, but most likely, that is where the problem is.
This question was answered in a related question by jewelsea who provided a most excellent answer at: Have multiple FXML files (created in SceneBuilder), but only one controller. Does each scene load it's own copy of the controller?.
By creating two controllers, I was able to initialize my Circle[][] array but now I will have to pass parameters between two controllers as jewelsea describes via links provided.

JavaFX SegmentedButton - Buttons full width

I got a SegmentedButton which contains 3 "glyph only" ToggleButtons like this:
<SegmentedButton maxWidth="Infinity" prefWidth="Infinity">
<buttons>
<fx:define>
<ToggleGroup fx:id="albumViewToggleGroup"/>
</fx:define>
<ToggleButton maxWidth="Infinity" fx:id="tagCloudToggle" mnemonicParsing="false" selected="true" toggleGroup="$albumViewToggleGroup">
<graphic>
<Glyph fontFamily="FontAwesome" icon="TAGS"></Glyph>
</graphic>
</ToggleButton>
<ToggleButton maxWidth="Infinity" fx:id="gridFlowToggle" mnemonicParsing="false" toggleGroup="$albumViewToggleGroup" >
<graphic>
<Glyph fontFamily="FontAwesome" icon="TH"></Glyph>
</graphic>
</ToggleButton>
<ToggleButton maxWidth="Infinity" fx:id="coverFlowToggle" mnemonicParsing="false" toggleGroup="$albumViewToggleGroup">
<graphic>
<Glyph fontFamily="FontAwesome" icon="ELLIPSIS_H"></Glyph>
</graphic>
<VBox.margin>
<Insets top="10.0"/>
</VBox.margin>
</ToggleButton>
</buttons>
</SegmentedButton>
The SegmenedtButton consumes the full width (represented by the red line), though the ToggleButtons are not. I checked this by setting a background color.
I would like that the ToggleButtons are stretched so that they are each 1/3 of the width of the SegmentedButton. How can i achive this?
According to the documentatiom mathematical operators can be used in fxml bindings.
So something like this can be used:
<SegmentedButton fx:id="segButton" maxWidth="1.7976931348623157E308" >
<buttons>
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K3" />
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K2" />
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K1" />
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K1,1" />
</buttons>
</SegmentedButton>
Probably no longer needed, but should hopefully be useful for anyone who winds up googling this.
I doubt that you still need help with this problem, but for anyone like me, who googled the problem and got here, here's how I solved it:
button1.prefWidthProperty().bind(segmentedButtons.widthProperty().divide(2));
button2.prefWidthProperty().bind(segmentedButtons.widthProperty().divide(2));
segmentedButtons.getButtons().addAll(button1, button2);
segmentedButtons.setMaxWidth(Double.MAX_VALUE);
So basically, for every ToggleButton that you add to SegmentedButton you bind preferred width to the actual width of SegmentedButton divided by the number of buttons. Since it's binding, the width will adjust on resizing the window, so you need to do it only once on creation. And if you want, you can divide width in some other way to make some buttons bigger and some smaller.

How to right align a button in Java FX toolbar

I am building a UI using Java FX scene builder and I want a button in a toolbar to float towards the right side of the toolbar. I have tried changing the node orientation of the parent(toolbar) and also the button but both seem to be ignored.
Add a pane with no content which always grows to fit available space between the left aligned tools in the bar and right aligned ones.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<ToolBar prefHeight="40.0" prefWidth="318.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<Button text="Apples" />
<Button text="Oranges" />
<Pane HBox.hgrow="ALWAYS" />
<Button text="Help" />
</ToolBar>
(Verified in Scene Builder 11.0.0)
One option: Use two ToolBar containers wrapped with an HBox container. Put the Help button in the right ToolBar. Put the left-aligned buttons in the left toolbar. For the left ToolBar, set the HGrow to ALWAYS. For the Right ToolBar, set HGrow to NEVER. For each ToolBar, set all Sizes to USE_COMPUTED_SIZE.
Here is the relevant fxml:
<HBox VBox.vgrow="NEVER">
<children>
<ToolBar HBox.hgrow="ALWAYS">
<items>
<Button text="Apples" />
<Button text="Oranges" />
</items>
</ToolBar>
<ToolBar HBox.hgrow="NEVER">
<items>
<Button text="Help" />
</items>
</ToolBar>
</children>
</HBox>
This is the hierarchy as displayed in Scene Builder:
This is the display within Scene Builder:
If you could place ur button inside a stack pane then u could make use of Stackpane's alignment property that takes javafx.geometry.Pos - The alignment of the child within the stackpane.For example in ur case:
<StackPane >
<Button translateY="-15" translateX="15" StackPane.alignment="TOP_RIGHT"/>
</StackPane>
BorderPane mainBorderPane = new BorderPane();
BorderPane ToolBorderPane = new BorderPane();
ToolBar tBarLeft=new ToolBar();
ToolBar tBarRight=new ToolBar();
ToolBorderPane.setLeft(tBarLeft);
ToolBorderPane.setRight(tBarRight);
mainBorderPane.setTop(ToolBorderPane);
...
...
for your left aligment items add tBarLeft and
your right aligment items add tBarRight
It can be done with some hack: apply 180 degree rotation around Y-axis for the toolbar and all its buttons.
<ToolBar rotate="-180.0" HBox.hgrow="ALWAYS">
<items>
<Button mnemonicParsing="false" rotate="180.0" text="Button">
<rotationAxis>
<Point3D y="1.0" />
</rotationAxis>
</Button>
</items>
<rotationAxis>
<Point3D y="1.0" />
</rotationAxis>
</ToolBar>

Categories