I want to make a border pane with two HBoxes on top and bottom and a GridPane in the center... I wrote what I needed, attached the labels but I can't run the code
Exception in Application start method
java.lang.reflect.InvocationTargetException is what I get as an error... the code is below, any help is welcome :) thanks
public class labelBorder extends Application {
#Override
public void start(Stage primaryStage) {
BorderPane bp = new BorderPane();
bp.setPrefSize(400, 400);
HBox hb1 = new HBox();
Label lb1 = new Label("");
lb1.setPrefWidth(200);
lb1.setBorder(new Border(new BorderStroke(Color.AQUAMARINE,BorderStrokeStyle.SOLID,null,new BorderWidths(5))));
Label lb2 = new Label("");
lb2.setPrefWidth(200);
lb2.setBorder(new Border(new BorderStroke(Color.BLUEVIOLET,BorderStrokeStyle.SOLID,null,new BorderWidths(5))));
HBox hb2 = new HBox();
URI foto = Paths.get("D:\\Barca.jpg").toUri();
Label lb3 = new Label();
lb3.setGraphic(new ImageView(foto.toString()));
lb3.autosize();
GridPane gp = new GridPane();
Label lb4 = new Label("");
Label lb5 = new Label("");
Label lb6 = new Label("");
Label lb7 = new Label("");
gp.add(lb4, 0, 0);
gp.add(lb5, 0, 1);
gp.add(lb6, 1, 0);
gp.add(lb7, 1, 1);
gp.getChildren().addAll(lb4,lb5,lb6,lb7);
hb1.getChildren().addAll(lb1,lb2);
hb2.getChildren().addAll(lb3);
bp.setTop(hb1);
bp.setCenter(gp);
bp.setBottom(hb2);
bp.getChildren().addAll(hb1,hb2,gp);
Scene scene = new Scene(bp);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
You should NOT add the duplicate children controls to your parent layout(pane). Your code must be throwing,
java.lang.IllegalArgumentException: Children: duplicate children added
To overcome your issue remove these lines,
gp.getChildren().addAll(lb4,lb5,lb6,lb7);
And,
bp.getChildren().addAll(hb1,hb2,gp);
As those controls have already been added to your corresponding layout.
Note: Adding children with pane.getChildren().addAll(...) on BorderPane is irrelevant and will be ignored while rendering the added controls.
Related
I need two grids in a BorderPane which is placed in a tab. When I add them both everything seems fine, but on launch only the first grid is visible.
This is my code.
public void start(Stage stage) throws Exception {
BorderPane bp = new BorderPane();
GridPane g1 = new GridPane();
g1.add(new Button("b1"), 0, 0);
GridPane g2 = new GridPane();
g2.add(new Button("b2"), 0, 0);
bp.setCenter(g1);
bp.setBottom(g2);
TabPane p = new TabPane();
Tab tab = new Tab("Test tab");
tab.setContent(bp);
p.getTabs().add(tab);
Scene s = new Scene(p);
stage.setScene(s);
stage.show();
}
g2 is not showing up at all. What am I missing?
Sorry, I have just begun learning javaFX and cannot figure out how to get all of my icons on buttons the same size. I tried a couple things but could not get it working, all help is appreciated. Thank you!
Wont let me post my question unless I add more details but I cant think of anything else to put so just ignore this whole paragraph as I ramble on so I can post my question and continue coding my game.
public class Main extends Application {
Stage window;
Button button;
Scene scene1, scene2;
public static final int ROCK = 0;
public static final int PAPER = 1;
public static final int SCISSORS = 2;
public static int userChoice;
public static void main(String [] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception
{
window = primaryStage;
//Layout 1
VBox layout = new VBox(20);
Label label = new Label("Rock Paper Scissors");
Button myButton = new Button("Start");
myButton.setOnAction(e -> window.setScene(scene2));
Button exit = new Button("Exit");
exit.setOnAction(e -> System.exit(0));
layout.getChildren().addAll(label, myButton, exit);
layout.setAlignment(Pos.CENTER);
scene1 = new Scene(layout, 300, 300);
//Layout 2
BorderPane border = new BorderPane();
VBox layout1 = new VBox(10);
Label label1 = new Label("Choose One");
layout1.setAlignment(Pos.CENTER);
//Layout 3
HBox layout2 = new HBox(10);
//Rock Image Button
Image rockIm = new Image(getClass().getResourceAsStream("Rock2.png"));
Button rock = new Button();
rock.setGraphic(new ImageView(rockIm));
rock.setOnAction(e -> userChoice = ROCK);
//Paper Image Button
Image paperIm = new
Image(getClass().getResourceAsStream("Paper2.png"));
Button paper = new Button();
paper.setGraphic(new ImageView(paperIm));
paper.setOnAction(e -> userChoice = PAPER);
//Scissor Image Button
Image scissorIm = new
Image(getClass().getResourceAsStream("Scissor2.png"));
Button scissors = new Button();
scissors.setGraphic(new ImageView(scissorIm));
scissors.setOnAction(e -> userChoice = SCISSORS);
Button quit = new Button("Return");
quit.setOnAction(e -> window.setScene(scene1));
layout2.getChildren().addAll(rock, paper, scissors, quit);
layout2.setAlignment(Pos.CENTER);
scene2 = new Scene(layout2, 300, 300);
window.setTitle("Rock Paper Scissors");
window.setScene(scene1);
window.show();
}
}
I'm using an animation in javafx so when I click on button it move with an animation but I want to also resize it (It should become smaller and smaller).
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
/*Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));*/
AnchorPane root=new AnchorPane();
Timeline timeline = new Timeline();
Button button = new Button();
timeline.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(button.translateXProperty(), 500),
new KeyValue(button.translateYProperty(), 500)),
new KeyFrame(Duration.seconds(.5), // set end position at 40s
new KeyValue(button.translateXProperty(), 200),
new KeyValue(button.translateYProperty(), 200)));
timeline.play();
root.getChildren().addAll(button);
primaryStage.show();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 800, 800));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
One possibility is to animate the node's scaleProperties:
timeline.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(button.translateXProperty(), 500),
new KeyValue(button.translateYProperty(), 500),
new KeyValue(button.scaleXProperty(), 1),
new KeyValue(button.scaleYProperty(), 1)),
new KeyFrame(Duration.seconds(.5), // set end position at 40s
new KeyValue(button.translateXProperty(), 200),
new KeyValue(button.translateYProperty(), 200),
new KeyValue(button.scaleXProperty(), .4),
new KeyValue(button.scaleYProperty(), .4)));
timeline.play();
I created this very simple example for JavaFX alert dialog for JavaFX8u40.
public class MainApp extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
private Stage stage;
#Override
public void start(Stage primaryStage) throws Exception
{
Button create = new Button("Create Alert");
create.setTooltip(new Tooltip("Create an Alert Dialog"));
create.setOnAction(e ->
{
createAlert();
});
primaryStage.setScene(new Scene(create));
primaryStage.show();
stage = primaryStage;
}
protected Alert createAlert()
{
Alert alert = new Alert(AlertType.WARNING);
Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png");
ImageView imageView = new ImageView(image1);
alert.setGraphic(imageView);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(stage);
alert.getDialogPane().setContentText("Some text");
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> System.out.println("The alert was approved"));
return alert;
}
}
I'm interested how I can set the image on the left side of the dialog.
Did someone manage to change the side of the image?
If you have a look at how the header is constructed, you'll find a GridPane node to layout a Label on the left and a StackPane for the icon.
If you want to reverse the cells order by code, you can do it, but it will be overriden every time updateHeaderArea() is called.
My suggestion is using this public API:
dialogPane.setHeader(Node header);
dialogPane.setGraphic(Node graphic);
providing a header with an icon on the left and a label, and a null graphic.
Using the same approach as DialogPane, we could add another GridPane as header:
protected Alert createAlert(){
Alert alert = new Alert(AlertType.WARNING);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(stage);
alert.getDialogPane().setContentText("Some text");
DialogPane dialogPane = alert.getDialogPane();
GridPane grid = new GridPane();
ColumnConstraints graphicColumn = new ColumnConstraints();
graphicColumn.setFillWidth(false);
graphicColumn.setHgrow(Priority.NEVER);
ColumnConstraints textColumn = new ColumnConstraints();
textColumn.setFillWidth(true);
textColumn.setHgrow(Priority.ALWAYS);
grid.getColumnConstraints().setAll(graphicColumn, textColumn);
grid.setPadding(new Insets(5));
Image image1 = new Image("http://www.mcaprojecttraining.com/images/java-big-icon.png");
ImageView imageView = new ImageView(image1);
imageView.setFitWidth(64);
imageView.setFitHeight(64);
StackPane stackPane = new StackPane(imageView);
stackPane.setAlignment(Pos.CENTER);
grid.add(stackPane, 0, 0);
Label headerLabel = new Label("Warning");
headerLabel.setWrapText(true);
headerLabel.setAlignment(Pos.CENTER_RIGHT);
headerLabel.setMaxWidth(Double.MAX_VALUE);
headerLabel.setMaxHeight(Double.MAX_VALUE);
grid.add(headerLabel, 1, 0);
dialogPane.setHeader(grid);
dialogPane.setGraphic(null);
alert.showAndWait()
.filter(response -> response == ButtonType.OK)
.ifPresent(response -> System.out.println("The alert was approved"));
return alert;
}
And this is what you will see:
Ok so I am in a basic programming class, and we have to make a borderpane that has all the different panes inside of it. When i try to put just a regular pane in the center with a "hello" button inside it the button gets put on the top the borderpane. All of the other panes I use a method that makes 5 buttons and randomly inserts a word and randomly rotates it. How do i get the pane to be in the center and not the top
StackPane root = new StackPane();
Button[] buttons = new Button[5];
buttons = createButtons();
root.getChildren().addAll(buttons);
FlowPane flow = new FlowPane();
Button[] buttons1 = new Button[5];
buttons1 = createButtons();
flow.getChildren().addAll(buttons1);
HBox hbox= new HBox();
Button[] buttons2 = new Button[5];
buttons2 = createButtons();
hbox.getChildren().addAll(buttons2);
VBox vbox = new VBox();
Button[] buttons3 = new Button[5];
buttons3 = createButtons();
vbox.getChildren().addAll(buttons3);
Pane pane = new Pane();
Button btn = new Button();
btn.setText("Hello");
pane.getChildren().add(btn);
BorderPane border = new BorderPane();
border.setBottom(hbox);
border.setLeft(root);
border.setRight(vbox);
border.setTop(flow);
border.setCenter(pane);
Scene scene = new Scene(border, 500, 500);
primaryStage.setTitle("Buttons");
primaryStage.setScene(scene);
primaryStage.show();
}
You should add a background color for identification, e. g. via setStyle("-fx-background-color:black). This way you see how each of your panes are laid out.
Example:
public class BindIt extends Application {
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
root.setStyle("-fx-background-color:red");
Button[] buttons = new Button[5];
buttons = createButtons();
root.getChildren().addAll(buttons);
FlowPane flow = new FlowPane();
flow.setStyle("-fx-background-color:blue");
Button[] buttons1 = new Button[5];
buttons1 = createButtons();
flow.getChildren().addAll(buttons1);
HBox hbox= new HBox();
hbox.setStyle("-fx-background-color:green");
Button[] buttons2 = new Button[5];
buttons2 = createButtons();
hbox.getChildren().addAll(buttons2);
VBox vbox = new VBox();
vbox.setStyle("-fx-background-color:yellow");
Button[] buttons3 = new Button[5];
buttons3 = createButtons();
vbox.getChildren().addAll(buttons3);
Pane pane = new Pane();
pane.setStyle("-fx-background-color:cyan");
Button btn = new Button();
btn.setText("Hello");
pane.getChildren().add(btn);
BorderPane border = new BorderPane();
border.setStyle("-fx-background-color:black");
border.setBottom(hbox);
border.setLeft(root);
border.setRight(vbox);
border.setTop(flow);
border.setCenter(pane);
Scene scene = new Scene(border, 500, 500);
primaryStage.setTitle("Buttons");
primaryStage.setScene(scene);
primaryStage.show();
}
private Button[] createButtons() {
return new Button[] { new Button( "Button") };
}
public static void main(String[] args) {
launch(args);
}
}
It gives you this:
Now you see that the center pane is in the center and fills the center. The problem is that the button isn't in the center. The easy solution is to just use a StackPane for the center pane:
StackPane pane = new StackPane();
Another solution would be to relocate the button to the center of the center pane. But then you'd have to consider resizing of the parent.