javafx - Dynamically resize node from within - java

I want to create simple gridPane, which would change its size after clicking a button. I have this code:
public class NewSimScene extends GridPane{
Button testButton;
public NewSimScene(){
setPrefSize(500, 500);
testButton = new Button("TEST");
testButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
Node source = (Node) t.getSource();
NewSimScene pane = (NewSimScene) source.getParent();
pane.setPrefSize(100, 100);
pane.setMaxSize(100, 100);
}
});
getChildren().add(testButton);
}
}
When I'm debugging, I can see that prefHeight and prefWidth values are changed, but there isn't any change in pane appearance. What could be a problem?
I don't know if it's relevant, but I'm running this pane in a separate scene, triggered from MenuBar.

there is no issue in your code, the Grid Pane's getting re-sized. Please have a look at the following code and feel free to comment if you are looking for something else !
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class TableViewSample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
GridPane grid = new NewSimScene();
((Group) scene.getRoot()).getChildren().addAll(grid);
stage.setScene(scene);
stage.show();
}
public class NewSimScene extends GridPane {
Button testButton;
public NewSimScene() {
setPrefSize(500, 500);
setStyle("-fx-background-color: palegreen;");
testButton = new Button("TEST");
testButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
Node source = (Node) t.getSource();
NewSimScene pane = (NewSimScene) source.getParent();
pane.setPrefSize(100, 100);
}
});
getChildren().add(testButton);
}
}
}

Related

JavaFX FileDialog focus

I am now trying to make focus to FX FileDialog. When i click outside the dialog, dialog is outfocused. Its any way to make when i click outside, the dialog call any metod that make him visible (focused)? TY :)
I just tried any like this.
...focusedProperty().addListener((obs, oldVal, newVal) -> System.out.println(newVal ? "Focused" : "Unfocused"));
and maeby this way...
fileChooser.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>(){
#Override
public void handle(WindowEvent window)
{...
There is no way to handle focus using addEventHandler inside javafx.stage.FileChooser,
But you can simply use primaryStage.setAlwaysOnTop(true); to make your FileChooser always visible on top of other windows
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class JavaFXtest5 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Test");
FileChooser chooser = new FileChooser();
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
primaryStage.setAlwaysOnTop(true);
chooser.showOpenDialog(primaryStage);
primaryStage.setAlwaysOnTop(false);
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello Dialog!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

JFXtras MonologFX - How to detect which button was pressed

I was testing the MonologFX from JFXtras (v8.0-r5), but I got stuck with it!
Can anyone tell me how to check what was the button in the dialog that was pressed by the user? I tried in many ways, but no luck at all.
package javafx_jfxtras_monologfx;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import jfxtras.labs.dialogs.MonologFX;
import jfxtras.labs.dialogs.MonologFXButton;
import jfxtras.labs.dialogs.MonologFX.Type;
public class JavaFX_JFXtras_MonologFX extends Application
{
#Override
public void start(Stage stage)
{
MonologFX m = new MonologFX();
m.setModal(true);
m.setType(Type.QUESTION);
m.setTitleText("JFXtras MonologFX");
m.setMessage("Do you want to continue?");
m.setPos(698, 450);
MonologFXButton mb1 = new MonologFXButton();
mb1.setType(MonologFXButton.Type.YES);
mb1.setLabel("Continue");
m.addButton(mb1);
MonologFXButton mb2 = new MonologFXButton();
mb2.setType(MonologFXButton.Type.NO);
mb2.setLabel("Exit");
m.addButton(mb2);
Button btn = new Button();
btn.setText("Click the Button");
btn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
System.out.println("Hello :)");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
stage.setTitle("JavaFX - JFXtras MonologFX");
stage.setScene(scene);
stage.show();
m.show();
}
public static void main(String[] args)
{
launch(args);
}
}
But the controls in labs are experimental and Mark has not worked on this one for a long time. We don't take them out because someone may use them, but as of version 8u40 JavaFX has a dialog itself. https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html|

How to change CenterPane from LeftPane in javaFx borderPane?

I am trying to make a panel in javafx and i used to a border pane as a main scene. There are 4 windows (main1, main2, main3,main4) for center panel, and there is a navigation menu in left panel.
borderPane.setCenter(mainMenu1.getCenterMain1UI());
//borderPane.setCenter(mainMenu2.getCenterMain2UI());
//borderPane.setCenter(mainMenu3.getCenterMain3UI());
//borderPane.setCenter(mainMenu4.getCenterMain4UI());
public BorderPane getAppWindow(){
if (borderPane == null){
borderPane = new BorderPane();
borderPane.setTop(topPanel.getTopPanelUI());
borderPane.setBottom(bottomPanel.getBottomPanelUI());
borderPane.setLeft(leftPanel.getLeftPanelUI());
borderPane.setCenter(mainMenu.getCenterMainUI());
borderPane.setAlignment(borderPane.getCenter(), Pos.TOP_LEFT);
}
return borderPane;
}
in the left panel controller
public class LeftPanelController {
public VBox leftPanelPane;
public Button btnLeftPanelMainmenu;
public Button btnLeftPanelDb;
public Button btnLeftPanelOfficeInfo;
public Button btnLeftPanelConfiguration;
public void btnLeftPanelMainmenuOnClickAction(ActionEvent e){
change border pane center to main
}
public void btnLeftPanelDbOnClickAction(ActionEvent e){
change border pane center to DB
}
public void btnLeftPanelOfficeInfoOnClickAction(ActionEvent e){
change border pane center to DB
}
public void btnLeftPanelConfigurationOnClickAction(ActionEvent e){
change border pane center to configuration
}
}
You'll need to set the on action event for each of your menu buttons so that it changes what is displayed in the center of the BorderPane.
Here's an example:
import java.util.LinkedHashMap;
import java.util.Map;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class BorderPaneExample extends Application {
private Map<String, Node> menuOptions;
private Node defaultCenterNode = new Label("Example node 1");
#Override
public void start(Stage primaryStage) throws Exception {
menuOptions = new LinkedHashMap<>();
menuOptions.put("Main Menu", defaultCenterNode);
menuOptions.put("History", new Label("Example node 2"));
menuOptions.put("Office Info", new Label("Example node 3"));
menuOptions.put("Configuration", new Label("Example node 4"));
BorderPane root = new BorderPane();
root.setCenter(defaultCenterNode);
VBox menuLayout = new VBox();
for (String key : menuOptions.keySet()) {
Button button = new Button(key);
button.setMaxWidth(Double.MAX_VALUE);
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
root.setCenter(menuOptions.get(key));
}
});
menuLayout.getChildren().add(button);
}
root.setLeft(menuLayout);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.setTitle("BorderPane Example");
primaryStage.setResizable(false);
primaryStage.sizeToScene();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I changed my button click methods in the left panel like that;
public void btnLeftPanelMainmenuOnClickAction(ActionEvent e) {
AppWindow.borderPane.setCenter(AppWindow.mainMenu.getCenterMainUI());
}
public void btnLeftPanelDbOnClickAction(ActionEvent e) {
AppWindow.borderPane.setCenter(AppWindow.dbMenu.getCenterDbUI());
}
public void btnLeftPanelConfigurationOnClickAction(ActionEvent e) {
AppWindow.borderPane.setCenter(AppWindow.configMenu.getCenterConfigurationUI());
}

JavaFx - Method Undefined

I have to create a program using JavaFX that creates a pane with a ball in it. There are four buttons that move the ball either left, right, up, or down. I don't understand why when I go to define each method (like .left() shown) the method call above remains an error that tells me it is undefined.
Main class:
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class MoveTheBall extends Application {
#Override
public void start(Stage primaryStage) {
StackPane pane = new StackPane();
Circle circle = new Circle(20);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
pane.getChildren().add(circle);
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btLeft = new Button("Left");
Button btRight = new Button("Right");
Button btUp = new Button("Up");
Button btDown = new Button("Down");
hBox.getChildren().add(btLeft);
hBox.getChildren().add(btRight);
hBox.getChildren().add(btUp);
hBox.getChildren().add(btDown);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
Scene scene = new Scene(borderPane, 300, 200);
primaryStage.setTitle("Move The Ball");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Buttons class:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class ControlBall extends Application {
private MoveTheBall moveTheBall = new MoveTheBall();
#Override
public void start(Stage primaryStage) {
HBox hBox = new HBox();
hBox.setSpacing(5);
hBox.setAlignment(Pos.CENTER);
Button btLeft = new Button("Left");
Button btRight = new Button("Right");
Button btUp = new Button("Up");
Button btDown = new Button("Down");
hBox.getChildren().add(btLeft);
hBox.getChildren().add(btRight);
hBox.getChildren().add(btUp);
hBox.getChildren().add(btDown);
btLeft.setOnAction(new LeftHandler());
btRight.setOnAction(new RightHandler());
btUp.setOnAction(new UpHandler());
btDown.setOnAction(new DownHandler());
BorderPane borderPane = new BorderPane();
borderPane.setCenter(moveTheBall);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
Scene scene = new Scene(borderPane, 200, 150);
primaryStage.setTitle("Move The Ball");
primaryStage.setScene(scene);
primaryStage.show();
}
class LeftHandler implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent e) {
moveTheBall.left(); // error because no class yet
}
}
class RightHandler implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent e) {
moveTheBall.right(); // error because no class yet
}
}
class UpHandler implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent e) {
moveTheBall.up(); // error because no class yet
}
}
class DownHandler implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent e) {
moveTheBall.down(); // error because no class yet
}
}
}
class moveTheBall extends StackPane {
private Circle ball = new Circle(20);
public moveTheBall() {
getChildren().add(ball);
ball.setStroke(Color.BLACK);
ball.setFill(Color.WHITE);
}
public void left() {
ball.setCenterX(-2.0);
ball.setCenterY(-2.0);
}
}
The line borderPane.setCenter(moveTheBall); is giving me problems as well. "The method setCenter(Node) in the type BorderPane is not applicable for the arguments (MoveTheBall)"
I'm pretty new to JavaFX, but I'm gonna do the best I can here.
I would suggest instead of doing totally separate classes for controlling the ball, moving the ball, and handling each button, look into using lambda expressions to simplify the code and reduce your room for error. For example, you can set what the btRight button does by writing a line like this:
btRight.setOnAction(e -> {
//if the ball is inside the right limit
//Move the ball to the right
});
You can write these expressions directly into your start() method, and avoid defining inner classes, as well as a separate class for the control.
As far as why you are getting an error, my best guess (as i said, I'm new to this) is that moveTheBall() is a constructor for the MoveTheBall class, but it is being called as a method.
Hope this helps.

JavaFx removing from pane object when I am in this object's class

I don't know if anyone could understand my problem from the title, but here's more specific description. I have class, in which I created a FlowPane, where I added objects of another class(images packed inside VBoxes). Each VBox have ContextMenu, where is MenuItem "Remove File". My problem is, how to remove this object while beeing inside the VBox class. Here is a little part of my code:
//removed, entire code is below after edit
The code where I'm accessing my CustomPane (my class of FlowPane, with specified attributes) works, because I can remove object if I'm doing it by their indexes, but when I remove one of them, other's indexes changes, so I'm looking for another solution. I need to specifically remove the object of the class in the code.
Okay so here is so called sscce, which of I had no idea, since now:
package sscce;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Sscce extends Application {
#Override
public void start(Stage primaryStage) {
CustomPane root = new CustomPane();
root.setPadding(new Insets(20));
root.setHgap(10);
root.setVgap(10);
for (int i = 0; i < 10; i++) {
RectangleBox recB = new RectangleBox();
root.getChildren().add(recB);
}
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class RectangleBox extends VBox {
static int index = 0;
public RectangleBox() {
Rectangle rec = new Rectangle(150, 150);
rec.setFill(Color.GREEN);
StackPane sp = new StackPane();
sp.getChildren().add(rec);
Label label = new Label(Integer.toString(index));
index++;
sp.getChildren().add(label);
getChildren().add(sp);
final ContextMenu cm = new ContextMenu();
MenuItem removeRec = new MenuItem("Remove Rectangle");
removeRec.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
((CustomPane) getParent()).getPane().getChildren().remove(0); //here is the problem, I want this line to remove the rectangle I clicked on(now it's removing first element in the pane).
}
});
cm.getItems().add(removeRec);
createContextMenuEvent(cm, rec);
}
private void createContextMenuEvent(final ContextMenu cm, final Rectangle rec) {
addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
if (t.getButton() == MouseButton.SECONDARY) {
cm.show(rec, t.getScreenX(), t.getScreenY());
}
}
});
}
}
class CustomPane extends FlowPane {
public CustomPane() {
//setAlignment(Pos.CENTER);
setHgap(25);
setVgap(25);
setPadding(new Insets(20));
}
public CustomPane getPane() {
return this;
}
}
It should work after copy/paste this entire code to java project. So I removed everything that is not neccessary, and I have replaced images to rectangles, now this program looks kind of stupid;p
I added comment to a line I have problem with. I hope now it's a lot clearer than before.
try this:
((CustomPane) RectangleBox.this.getParent()).getChildren().remove(RectangleBox.this);
hope it helps.

Categories