If I show a circle with specific x and y coordinates it works fine:
public class FxApplication extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Group group = new Group();
Circle circle = new Circle(100, 100, 2);
group.getChildren().add(circle);
Pane pane = new Pane(group);
ScrollPane scrollPane = new ScrollPane(pane);
BorderPane borderPane = new BorderPane(scrollPane);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
But if I add a label to the circle the position of the circle is ignored.
public class FxApplication extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Group group = new Group();
Circle circle = new Circle(100, 100, 2);
Label label = new Label("test", circle);
group.getChildren().add(label);
Pane pane = new Pane(group);
ScrollPane scrollPane = new ScrollPane(pane);
BorderPane borderPane = new BorderPane(scrollPane);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
How to keep the position of the circle and only add the label or how to set the correct circle position inclusive label?
For example what works is:
Circle circle = new Circle(circle_center_x, circle_center_y, 3);
Text label = new Text("test");
double halfLabelHeight = label.getLayoutBounds().getHeight() / 2;
label.relocate(circle_center_x + 10, circle_center_y - halfLabelHeight);
this.getChildren().addAll(circle , label);
But I'm looking for a more integrated solution. I thought the Label object could be somewhat smart and do this on it's own but instead it's taking the circle x and y position and applies that to it's own space and not the parent space.
You actually need to relocate the label now instead of just telling the circle where to be displayed. When you specify the new Circle(100,100,2) you telling the Circle Object to be located at the x=100 and y=100 of its parent. In the first case its parent is the group but in the second case, its parent is now the Label. In order to locate the Label to x,y = 100,100 inside the Group you will need to call :
label.relocate(100, 100);
The Circle initialization is now not necessary. Even if you put the Circle at 0,0 it's still going to be displayed next to the Label because the label will manage the Node location.
PS. You can either change the NodeOrientation from LEFT_TO_RIGHT to RIGHT_TO_LEFT by label.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT); or in case you want to change the "shape" location you can do label.setContentDisplay(ContentDisplay.TOP); ( or BOTTOM etc )
I am not sure I understand correct what are you trying to achieve here but I guess you want to have the Circle and the Label next to each other. In addition you want to label to be centered on height depending the circle location. If the previous assumption is correct then here is the code to achieve that :
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class FxApplication extends Application {
private Group group;
#Override
public void start(Stage primaryStage) throws Exception {
group = new Group();
addCustomNode(100, 100, new Circle(2), new Label("Test"));
Pane pane = new Pane(group);
ScrollPane scrollPane = new ScrollPane(pane);
BorderPane borderPane = new BorderPane(scrollPane);
Scene scene = new Scene(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}
private void addCustomNode(int x, int y, Circle circle, Label label) {
double labelDimensions[] = getLabelDimensions(label);
circle.setCenterX(100);
circle.setCenterY(100);
label.relocate(circle.getCenterX() + labelDimensions[0] / 2.0, circle.getCenterY() - labelDimensions[1] / 2.0);
group.getChildren().addAll(circle, label);
}
// find the height and width before we
// add the label to the stage
private double[] getLabelDimensions(Label label) {
HBox h = new HBox();
Label l = new Label("Hello");
h.getChildren().add(l);
Scene s = new Scene(h);
l.impl_processCSS(true);
return new double[] { l.prefWidth(-1), l.prefHeight(-1) };
}
public static void main(String[] args) {
launch(args);
}
}
Related
Say I have a scene. I have a grid pane in it which contains 2 x 2 buttons. I can align this whole grid pane exactly in the center by simply doing gridPane.setAlignment(Pos.CENTER); however I want it the whole grid pane to be positioned on the position of one node.
Here is an illustration:
The whole grid pane is aligned in the center. What I want is to set the position of r2 c1 exactly in the center and I want the other three nodes positioned above and besides it respectively.
I can position a single button but I do not know how to make the whole grid pane positioned based on the position of one node.
Here is the code I wrote for the illustration:
private BorderPane root = new BorderPane();
private Scene scene = new Scene(root, 1366, 768);
#Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
gridPane.addRow(0, new Button("r1 c1"), new Button("r1 c2"));
gridPane.addRow(1, new Button("r2 c1"), new Button("r2 c2"));
gridPane.setVgap(20);
gridPane.setHgap(30);
gridPane.setAlignment(Pos.CENTER);
root.setCenter(gridPane);
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.show();
}
The r2c1 node should exactly be in the middle and the rest of the buttons positioned based on its position. Here is the desired view:
r2c1 starts exactly from the center of the screen and the other nodes are moved corresponding to it.
Any help would be greatly appreciated.
"moving the whole GridPane as a whole" requires changing the way GridPane is laid out with in it's parent (and not the internal layout of the GridPane itself).
There are a few alternatives to achieve it. Manipulating the parent layout is one.
Setting translation to the GridPane is another:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class FxMain extends Application {
private static final int VGAP = 20, HGAP = 30;
#Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
Button button1 = new Button("r1 c1");
gridPane.addRow(1, button1, new Button("r1 c2"));
gridPane.addRow(2, new Button("r2 c1"), new Button("r2 c2"));
gridPane.setVgap(VGAP); gridPane.setHgap(HGAP);
gridPane.setAlignment(Pos.CENTER);
//apply row-height + vgap down translate (row height represented by
//button height)
gridPane.translateYProperty().bind(button1.heightProperty().add(VGAP));
BorderPane root = new BorderPane();
Scene scene = new Scene(root, 200, 200);
root.setCenter(gridPane);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(final String[] args) {
launch(args);
}
}
EDIT: after clarifying the desired layout:
The technique is similar. Enclose the GridPane in an AnchorPane and apply the desired translation:
public class FxMain extends Application {
private static final int VGAP = 20, HGAP = 30;
#Override
public void start(Stage primaryStage) {
GridPane gridPane = new GridPane();
AnchorPane root = new AnchorPane(gridPane); //enclose grid in an AnchorPane
gridPane.addRow(1, new Button("r1 c1"), new Button("r1 c2"));
gridPane.addRow(2, new Button("r2 c1"), new Button("r2 c2"));
gridPane.setVgap(VGAP); gridPane.setHgap(HGAP);
//apply y translation: (root height/2) minus grid pane height
gridPane.translateYProperty().bind(root.heightProperty().divide(2).subtract(gridPane.heightProperty()));
//apply x translation of root widt / 2
gridPane.translateXProperty().bind(root.widthProperty().divide(2));
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(final String[] args) {
launch(args);
}
}
I want to render an array of buttons and then a piechart to the screen. I've tried almost every method I could but something doesn't seems to work. either alone array of buttons(usercontrol()) or pie(the graph) can be render but when I try to do both it only render the array of buttons.plz don't worry about return types of function. any help will be really appreciated.
public class Layout {
// returns Windows height and width
private final double width = 600;
private final double height = 400;
private Button[] userControl() { // navigation bar buttons
Button[] buttons = new Button[3];
buttons[0] = new Button("BUY Share!"); // Buy shares buttons
buttons[0].setLayoutX(width - 100);
buttons[0].setLayoutY(10);
buttons[1] = new Button("Sell Shares!"); // Sell shares buttons
buttons[1].setLayoutX(width - 200);
buttons[1].setLayoutY(10);
buttons[2] = new Button("Show Share"); // Show shares buttons
buttons[2].setLayoutX(width - 300);
buttons[2].setLayoutY(10);
return buttons;
}
public void pie() {
ObservableList<PieChart.Data> shareHolders
= FXCollections.observableArrayList(
new PieChart.Data("user1", 13),
new PieChart.Data("user2", 25),
new PieChart.Data("user3", 10),
new PieChart.Data("user4", 22),
new PieChart.Data("user5", 30));
PieChart chart = new PieChart(shareHolders);
chart.setTitle("Share Holders Shares");
VBox pie = new VBox();
pie.setLayoutY(100);
pie.getChildren().addAll(chart);
pane().getChildren().add(pie);
// return pie;
}
private Pane pane() {
Pane pane = new Pane();
pane.getChildren().addAll(userControl());
return pane;
}
public Stage window() {
//pane().getChildren().add();
pie();
Scene scene = new Scene(pane(), 600, 400);
Stage primaryStage = new Stage();
primaryStage.setScene(scene);
primaryStage.setTitle("ShareHolders!");
primaryStage.show();
return primaryStage;
}
}
Your problem is that you are creating a new Pane every time you call the pane method. You need to change it, perhaps by using a global Pane object.
//First, declare a global Pane.
static Pane pane = new Pane();
//Make your pie() method return the pie VBox.
public VBox pie() {
/*Blah blah blah, making the pie...*/
return pie//Remember, pie is a VBox, which is why we are returning the VBox.
}
//Later, when you build your window, add the pie and the buttons to the GLOBAL PANE...
public Stage window() {
pane.getChildren().add(pie()); //...right here.
pane.getChildren().addAll(userControl());
/*Build the primary stage...*/
return primaryStage;
}
This should get you your desired result.
I am trying to set up a UI with three split panes. The first two are vertical panes, on the left and right side of the screen. One side of each split has a title pane. The user can select items from these panes to include in fields in the central pane. There is also a horizontal pane at the bottom that is not relevant to this question.
The user can open these side panes either by dragging the vertical dividers, or by clicking on the relevant toggle button (Films, Books etc.) to show that pane.
The issue I have is that I want to make it so that dragging one vertical divider does not move the other. However, since I cannot find a way to set this up without putting one of the vertical split panes into the other vertical pane, this always results in a situation where moving one of the dividers also moves the other. In the case of the below code for instance, moving the vertical divider for the left-hand (Films) split pane moves the right-hand vertical divider.
Can anyone help with this?
package pane2;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.application.*;
import javafx.beans.property.DoubleProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TitledPane;
import javafx.scene.input.*;
import javafx.stage.Stage;
public class Pane2 extends Application {
SplitPane rightSplit;
DoubleProperty rightSplitDividerPos;
TitledPane books;
ToggleButton selectBooks;
VBox booksBox;
VBox centre;
SplitPane leftSplit;
DoubleProperty leftSplitDividerPos;
TitledPane films;
ToggleButton selectFilms;
VBox filmsBox;
VBox centreLeft;
SplitPane mainSplit;
DoubleProperty mainSplitDividerPos;
TitledPane arts;
ToggleButton selectArts;
VBox artsBox;
BorderPane root;
public static void main(String[] args)
{
launch( args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Test");
//Create right-hand titled pane for the books list and centre it in Vbox
books = new TitledPane();
books.setText("Books");
books.setMinWidth(0);
booksBox = new VBox(0,books);
//Create central pane and add toggle buttons to open hidden panes on the
//left, right, and bottom (films, books, and arts respectively)
selectBooks = new ToggleButton("Books");
selectFilms = new ToggleButton("Films");
selectArts = new ToggleButton("Arts");
centre = new VBox(100,selectBooks,selectFilms,selectArts);
centre.setPrefWidth(1300);
centre.setPrefHeight(750);
//Create split pane to divide the central pane and books list
rightSplit = new SplitPane();
rightSplit.getItems().addAll(centre,booksBox);
//Create left-hand titled pane for the films list and centre it in VBox
films = new TitledPane();
films.setText("Films");
films.setMinWidth(0);
filmsBox = new VBox(0,films);
//Create split pane to divide the films list and the central pane
leftSplit = new SplitPane();
leftSplit.getItems().addAll(filmsBox,rightSplit);
//Create mainSplit pane
arts = new TitledPane();
arts.setText("arts");
arts.setMinHeight(0);
artsBox = new VBox(0,arts);
mainSplit = new SplitPane();
mainSplit.setOrientation(Orientation.VERTICAL);
mainSplit.getItems().addAll(leftSplit,artsBox);
root = new BorderPane();
root.setCenter(mainSplit);
//Set divider positions for the three dividers
rightSplitDividerPos = rightSplit.getDividers().get(0).positionProperty();
rightSplitDividerPos.set(1.0);
leftSplitDividerPos = leftSplit.getDividers().get(0).positionProperty();
leftSplitDividerPos.set(0.0);
mainSplitDividerPos = mainSplit.getDividers().get(0).positionProperty();
mainSplitDividerPos.set(1.0);
//Start up scene and stage
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
//Event - if the books toggle button is selected, the left divider will
//move to the right to show the books selection pane
selectBooks.setOnAction(event -> {
if(selectBooks.isSelected()){
leftSplitDividerPos.set(0.15);
}
if(!selectBooks.isSelected()){
leftSplitDividerPos.set(0.0);
}else{
}
});
//Event - if the films toggle button is selected, the right divider will
//move to the left to show the films selection pane
selectFilms.setOnAction(event -> {
if(selectFilms.isSelected()){
rightSplitDividerPos.set(0.8);
}
if(!selectFilms.isSelected()){
rightSplitDividerPos.set(1.0);
}else{
}
});
//Event - if the arts toggle button is selected, the bottom divider will
//move up to show the arts selection pane
selectArts.setOnAction(event -> {
if(selectArts.isSelected()){
mainSplitDividerPos.set(0.75);
}
if(!selectArts.isSelected()){
mainSplitDividerPos.set(1.0);
}else{
}
});
}
}
do you really need 3 SplitPane in your layout? because i think you can achieve pretty much the same result with just 1 pane:
SplitPane split = new SplitPane();
VBox left = new VBox(new Label("left"));
left.setStyle("-fx-background-color: cadetblue");
VBox right = new VBox(new Label("right"));
right.setStyle("-fx-background-color: darkorange");
VBox center = new VBox(new Label("center"));
center.setStyle("-fx-background-color: darkgreen");
split.getItems().addAll(left, center, right);
split.setDividerPosition(0,1/(double)3);
split.setDividerPosition(1,2/(double)3);
Scene scene = new Scene(split, 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
Here is your code realated Example:
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Test");
//Create central pane and add toggle buttons to open hidden panes on the
//left, right, and bottom (films, books, and arts respectively)
ToggleButton selectBooks = new ToggleButton("Books");
ToggleButton selectFilms = new ToggleButton("Films");
ToggleButton selectArts = new ToggleButton("Arts");
VBox centre = new VBox(100,selectBooks,selectFilms,selectArts);
//Create left-hand titled pane for the films list and centre it in VBox
TitledPane films = new TitledPane();
films.setText("Films");
VBox filmsBox = new VBox(films);
//Create right-hand titled pane for the books list and centre it in Vbox
TitledPane books = new TitledPane();
books.setText("Books");
VBox booksBox = new VBox(books);
//Create mainSplit pane
TitledPane arts = new TitledPane();
arts.setText("arts");
VBox artsBox = new VBox(arts);
SplitPane mainSplit = new SplitPane();
mainSplit.getItems().addAll(filmsBox, centre, booksBox);
mainSplit.setDividerPosition(0,1/(double)12);
mainSplit.setDividerPosition(1,11/(double)12);
SplitPane root = new SplitPane();
root.setOrientation(Orientation.VERTICAL);
root.getItems().addAll(mainSplit, artsBox);
root.setDividerPosition(0,0.9);
root.setPrefWidth(1300);
root.setPrefHeight(750);
//Start up scene and stage
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setMaximized(true);
primaryStage.show();
}
I'm working on this example which is not working properly:
public class test extends Application
{
private void init(Stage primaryStage)
{
Group root = new Group();
primaryStage.setScene(new Scene(root));
String pillButtonCss = DX57DC.class.getResource("PillButton.css").toExternalForm();
// create 3 toggle buttons and a toogle group for them
ToggleButton tb1 = new ToggleButton("Left Button");
tb1.setId("pill-left");
ToggleButton tb2 = new ToggleButton("Center Button");
tb2.setId("pill-center");
ToggleButton tb3 = new ToggleButton("Right Button");
tb3.setId("pill-right");
final ToggleGroup group = new ToggleGroup();
tb1.setToggleGroup(group);
tb2.setToggleGroup(group);
tb3.setToggleGroup(group);
// select the first button to start with
group.selectToggle(tb1);
//////////////////////////////////////////
final VBox vbox = new VBox();
final Rectangle rect1 = new Rectangle(300, 300);
rect1.setFill(Color.ALICEBLUE);
final Rectangle rect2 = new Rectangle(300, 300);
rect2.setFill(Color.AQUA);
final Rectangle rect3 = new Rectangle(300, 300);
rect3.setFill(Color.AZURE);
tb1.setUserData(rect1);
tb2.setUserData(rect2);
tb3.setUserData(rect3);
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
{
#Override
public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle)
{
if (new_toggle == null)
{
//rect.setFill(Color.WHITE);
}
else
{
vbox.getChildren().addAll((Node[]) group.getSelectedToggle().getUserData());
//rect.setFill((Color) group.getSelectedToggle().getUserData());
}
}
});
///////////////////////////////////////////
HBox hBox = new HBox();
hBox.getChildren().addAll(tb1, tb2, tb3);
hBox.setPadding(new Insets(20, 20, 260, 20));
hBox.getStylesheets().add(pillButtonCss);
vbox.getChildren().add(hBox);
//vbox.getChildren().add(rect);
root.getChildren().add(vbox);
}
#Override
public void start(Stage primaryStage) throws Exception
{
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
I want to create several Rectangles(or object in which or object) in which I want to store data. I want to switch the Rectangles(objects) which are displayed in front of the user using the buttons. The example which I implemented is not working properly. Can you tell me what is the proper way to implement this?
You could create a Stackpane with the Rectangle and a Label with Text on top of it (if thats the data you want to store). Alternatively you can also set the Background of any Pane to have a colored Rectangle.
Than add this Pane as Userdata to the corresponding button and add the buttons userdata to your VBox on toggle.
final StackPane rect1pane = new StackPane();
final Rectangle rect1 = new Rectangle(300, 300);
rect1pane.getChildren().add(rect1);
rect1pane.getChildren().add(new Label("Some text"));
tb1.setUserData(rect1pane);
togglePropertyListener:
...
else{
//Delete rectangles added before ( or check if this one isnt already dispayed)
if(group.getSelectedToggle().getUserData() instanceof Node)
vbox.getChildren().add((Node)group.getSelectedToggle().getUserData());
}
If you just want your example code to work change:
vbox.getChildren().addAll((Node[]) group.getSelectedToggle().getUserData());
to
vbox.getChildren().addAll((Node) group.getSelectedToggle().getUserData());
Because your just adding the Rectangle of the Selected ToggleButton it is only one and not an array.
Make your window bigger after a click to see the rectanlge (the 260px bottom padding doesn't help because even if the space is empty below hbox, it still is part of the hbox and cant get used by your added rectangle) or just move
group.selectToggle(tb1);
to the last line of your init method ;)
Since updating to JavaFX 2.0 b36 (SDK for Windows (32Bit) + Netbeans Plugin) from a previous JavaFX 2.0 version the SplitPane control does not work as expected any longer.
The divider can't be moved
The divider position is not as expected
The sizing of the contained sides is not as expected
Here my example code for a SplitPane .
public class FxTest extends Application {
public static void main(String[] args) {
Application.launch(FxTest.class, args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SplitPane Test");
Group root = new Group();
Scene scene = new Scene(root, 200, 200, Color.WHITE);
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
SplitPane splitPane = new SplitPane();
splitPane.setPrefSize(200, 200);
splitPane.setOrientation(Orientation.HORIZONTAL);
splitPane.setDividerPosition(0, 0.7);
splitPane.getItems().addAll(button1, button2);
root.getChildren().add(splitPane);
primaryStage.setScene(scene);
primaryStage.setVisible(true);
}
}
As you can (hopefully) see the left side is clearly smaller than the right side.
Another funny fact is, when you change orientation to VERTICAL
splitPane.setOrientation(Orientation.VERTICAL);
and try to move the divider up or down you get some console output saying 'HERE'.
Looks like some test output.
What's the issue with this?
To get the SplitPane working as expected add a layout (e.g. BorderPane) to each side. Add the controls to display to each of these layouts. I think this should be made more clear in API documentation!
public class FxTest extends Application {
public static void main(String[] args) {
Application.launch(FxTest.class, args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SplitPane Test");
Group root = new Group();
Scene scene = new Scene(root, 200, 200, Color.WHITE);
//CREATE THE SPLITPANE
SplitPane splitPane = new SplitPane();
splitPane.setPrefSize(200, 200);
splitPane.setOrientation(Orientation.HORIZONTAL);
splitPane.setDividerPosition(0, 0.7);
//ADD LAYOUTS AND ASSIGN CONTAINED CONTROLS
Button button1 = new Button("Button 1");
Button button2 = new Button("Button 2");
BorderPane leftPane = new BorderPane();
leftPane.getChildren().add(button1);
BorderPane rightPane = new BorderPane();
rightPane.getChildren().add(button2);
splitPane.getItems().addAll(leftPane, rightPane);
//ADD SPLITPANE TO ROOT
root.getChildren().add(splitPane);
primaryStage.setScene(scene);
primaryStage.setVisible(true);
}
}