Like the picture showed, the red box above is a GridBox and below is a VBox with Splitpane (ListView) and Gridpane (2 Buttons). What I want to implement is to hide the VBox below, when the Button "Hide <<<" is clicked.
But now, the red box below is removed by call root.getChildren().remove(child); How can the Window(Stage) or Scene automatically resizing.
In the controller:
public class FunctionOwnerFX extends Application{
public static String HIDE = "Hide Libraries <<<";
public static String SHOW = "Show Libraries >>>";
private boolean isHidden = false;
@FXML
private TextField textfield;
@FXML
private Button btn2;
@FXML
private VBox vbox;
@FXML
private VBox libraryVbox;
private Stage primaryStage;
@Override
public void start(Stage stage) throws Exception{
primaryStage = stage;
Parent root = FXMLLoader.load(getClass().getResource("new_function_owner.fxml"));
Scene scene = new Scene(root);
stage.setTitle("New Function Owner");
stage.setScene(scene);
stage.sizeToScene();
stage.show();
}
@FXML
protected void splitPaneControl(){
isHidden = !isHidden;
if (isHidden) {
vbox.getChildren().remove(libraryVbox);
primaryStage.sizeToScene();
btn2.setText(SHOW);
} else {
vbox.getChildren().add(libraryVbox);
primaryStage.sizeToScene();
btn2.setText(HIDE);
}
}
private ArrayList<String> getMatches(String str){
return null;
}
public static void main(String[] args) {
launch(args);
}
}
The Runtime error occured when I used the method sizeToScene():
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 49 more
Caused by: java.lang.NullPointerException
at FunctionOwnerFX.splitPaneControl(FunctionOwnerFX.java:65)
... 58 more
Resize the Scene and the Scene Builder Window Resize the scene's width and height in the Content panel to get a larger working area. In the Inspector panel, select the Layout section. In the Size section, change the Pref Width property value to 800 and the Pref Height property value to 600.
stage. Stage , represents a window in a JavaFX desktop application. Inside a JavaFX Stage you can insert a JavaFX Scene which represents the content displayed inside a window - inside a Stage .
I don't have enough reputation to make a comment (need 50, but I have 48) so I will make an answer. Now back on topic, try calling the method sizeToScene on your stage object after you remove the node.
https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Window.html#sizeToScene--
I think Dusko is right given your approach, but I am asking why recreate the wheel? In this example, I use a TitledPane
and adjust the Stage
as the Scene's
height gets larger or smaller. Thanks, @James_D for info on how to implement this.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* @author blj0011
*/
public class JavaFXApplication97 extends Application
{
@Override
public void start(Stage primaryStage)
{
Label tpLabel = new Label("text here!");
TextField tpTextField = new TextField();
Button tpButton = new Button("OK");
HBox tpHBox = new HBox(tpTextField, tpButton);
VBox topPanel = new VBox(tpLabel, tpHBox);
TextArea bpTextArea = new TextArea();
Button bpButton = new Button("Button");
VBox bpVBox = new VBox(bpTextArea, bpButton);
TextArea bpTextArea2 = new TextArea();
Button bpButton2 = new Button("Button2");
VBox bpVBox2 = new VBox(bpTextArea2, bpButton2);
HBox bpHBox = new HBox(bpVBox, bpVBox2);
TitledPane titledPane = new TitledPane();
titledPane.setText("");
titledPane.setContent(bpHBox);
titledPane.setAnimated(false);
Accordion bottomPanel = new Accordion(titledPane);
VBox root = new VBox(topPanel, bottomPanel);
//This adjust the Stage when the height of the Accordian change.
bottomPanel.expandedPaneProperty().addListener((obs, oldVal, newVal) -> {
System.out.println("hello");
Platform.runLater(() -> {
primaryStage.sizeToScene();
});
});
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With