This question already has answers here:
How to insert an object in an ArrayList at a specific position
(6 answers)
Closed 7 years ago.
In a VBox I already have two Grid Panes. Now I want to insert a new anchor pane between them. If I use the below code,
vBoxPane.getChildren().add(anchorPane);
it will insert anchor pane at last, but I want it inbetween the gridpanes. Is there any way?
Since you're using a VBox as main container, the index of its children determine their vertical position.
So, if you want to place a child node in the middle, just insert it in the middle of the list returned by the getChildren() method.
Here is a complete runnable example:
public class Example extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
GridPane gridTop = new GridPane();
GridPane gridBottom = new GridPane();
VBox mainPanel = new VBox(gridTop, gridBottom);
Label topLabel = new Label("Top");
gridTop.add(topLabel, 0, 0);
Button createAnchorPane = new Button("Create AnchorPane");
gridBottom.add(createAnchorPane, 0, 0);
createAnchorPane.setOnAction(event -> {
Label centerLabel = new Label("Center");
AnchorPane newPane = new AnchorPane();
newPane.getChildren().add(centerLabel);
// add the anchor pane in the middle
mainPanel.getChildren().add(1, newPane);
});
Scene scene = new Scene(mainPanel, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
}
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 am new to JavaFX and I'm trying to create a "dropdown list" of check items. I am trying to make the drop down scrollable. I can easily do this with a ComboBox (using setVisibleowCount(int)), but ComboBox only allows for 1 item to be chosen before closing the dialogue and doesn't seem to be the right object to use.
I am currently using a menu button with CheckMenuItems. ListView seems like it could be useful, but I'm not quite sure how to integrate that. If anyone can help that'd be great. Thanks.
Current Status
Since you cannot use a CheckComboBox I would see if an Accordion + TitledPane fits with what your doing.
Here is an example:
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception{
VBox root = new VBox();
root.getChildren().add(new Label("Select Number of Checkboxes you feel like clicking"));
VBox vBox = new VBox();
for (int i = 0; i < 5; i++)
vBox.getChildren().add(new CheckBox("i:" + i));
ScrollPane scrollPane = new ScrollPane(vBox);
//Easily changeable Max Height
scrollPane.setMaxHeight(10);
// Create TitledPane.
TitledPane titledPane = new TitledPane("Check Boxes", scrollPane);
//Add to Accordion
Accordion accordion = new Accordion(titledPane);
//Add to root VBox
root.getChildren().add(accordion);
root.getChildren().add(new Label("Some Other Content"));
stage = new Stage();
stage.setHeight(200);
stage.setScene(new Scene(root));
stage.setAlwaysOnTop(true);
stage.show();
}
public static void main(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 basically new to Java FX 2.
Scenario:
I have 3 Scenes and I want a way to add menu-bar such that I don't i don't want to explicitly remove the menu bar from previous scene and add it to new one. Like Some thing a Parent Scene or some way menu-bar is attached to Stage. I mean menu-bar is added just one time and always be present whatever scene is in front or not.
If This is Possible How Can I do this.
Here is the Default Example Provided by Oracle Docs of JavaFX http://docs.oracle.com/javafx/2/ui_controls/MenuSample.java.html
public class Main extends Application {
final ImageView pic = new ImageView();
final Label name = new Label();
final Label binName = new Label();
final Label description = new Label();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
stage.setTitle("Menu Sample");
Scene scene = new Scene(new VBox(), 400, 350);
scene.setFill(Color.OLDLACE);
MenuBar menuBar = new MenuBar();
// --- Graphical elements
final VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.setPadding(new Insets(0, 10, 0, 10));
makeContentsForVBox();// in this vBox area will be fill with name pic desrciption
vbox.getChildren().addAll(name, binName, pic, description); // name is lable
// --- Menu File
Menu menuFile = new Menu("File");
MenuItem add = new MenuItem("Shuffle",
new ImageView(new Image(getClass().getResourceAsStream("new.png"))));
add.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
shuffle();
vbox.setVisible(true);
}
});
MenuItem clear = new MenuItem("Clear");
clear.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
clear.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
vbox.setVisible(false);
}
});
MenuItem exit = new MenuItem("Exit");
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
System.exit(0);
}
});
menuFile.getItems().addAll(add, clear, new SeparatorMenuItem(), exit);
((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);
stage.setScene(scene);
stage.show();
}
}
So Here menuBar is added to a scene. if i swap the scene and bring an other scene in front ... What will i do. i think I remove menuBar from this scene and add to other or simply add to new one. so every time i have to do this when i change. Is there any way to avoid this??
The approach I would prefer is to use a Scene with BorderPane as its root
scene.setRoot(borderPane);
You can add the MenuBar to the top of the BorderPane and at its Center you can place SplitPane
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(splitPane);
Whenever you need to switch to WebView just replace it with SplitPane :
borderPane.setCenter(webView);
Following this approach, your MenuBar will always remain on TOP and you can switch between SplitPane and WebView
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 ;)