I'am using javafx for building a GUI, including a GridPane with a MenuBar and another GridPane. I want that second GridPane to be scrollable, so I put it into a ScrollPane "new ScrollPane(grid2)", and added it to the first GridPane. Unfortunally, the content of the ScrollPane isn't streched horizontally anymore, if the window-size changes, what is needed.
Here is a pic, which should clearify the problem: http://img5.fotos-hochladen.net/uploads/scrollpaneemptwjpv2084xm.png (cant use sof-tool, because reputation<10)
I want to have a big number of TitledPages, with a button in front of them, and which can be scrollable (unlike the menu, which should stay on top).
If i just add the second GridPane to the first one, it is streched in correct way, so I wonder why the ScrollPane or its element cant be streched also, and how.
I post you my code, thanks for reading this, and thanks for your effort!
package main;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MainGUI6 extends JFrame {
private static final long serialVersionUID = -8824577253071530315L;
JFXPanel panel;
Scene scene;
boolean wait = true;
public MainGUI6() {
panel = new JFXPanel();
Platform.runLater(new Runnable() {
@Override
public void run() {
GridPane grid = new GridPane();
scene = new Scene(grid, 1000, 1000);
grid.setVgap(4);
grid.setPadding(new Insets(5, 5, 5, 5));
//Menu
final Menu menu1 = new Menu("Menu1");
final Menu menu2 = new Menu("Menu2");
final Menu menu3 = new Menu("Menu3");
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(menu1, menu2, menu3);
grid.add(menuBar, 0, 0);
grid.setHgrow(menuBar, Priority.ALWAYS);
//Second grid
GridPane grid2 = new GridPane();
//Buttons
Button b1 = new Button("ButtonI1");
Button b2 = new Button("ButtonI2");
Button b3 = new Button("ButtonI3");
grid2.add(b1, 0, 0);
grid2.add(b2, 0, 1);
grid2.add(b3, 0, 2);
//TitledPanes
TitledPane tp1 = new TitledPane("TitledPane1", new Button("Button1"));
TitledPane tp2 = new TitledPane("TitledPane2", new Button("Button2"));
TitledPane tp3 = new TitledPane("TitledPane3", new Button("Button3"));
grid2.add(tp1, 1, 0);
grid2.add(tp2, 1, 1);
grid2.add(tp3, 1, 2);
grid2.setHgrow(tp1, Priority.ALWAYS);
grid2.setHgrow(tp2, Priority.ALWAYS);
grid2.setHgrow(tp3, Priority.ALWAYS);
ScrollPane sp = new ScrollPane(grid2);
grid.add(sp, 0, 1);
grid.setHgrow(sp, Priority.ALWAYS);
panel.setScene(scene);
wait = false;
}
});
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainGUI6();
}
});
}
}
Your ScrollPane
lacks the following:
sp.setFitToWidth(true);
To quote the corresponding API docs:
If true and if the contained node is a Resizable, then the node will be kept resized to match the width of the ScrollPane's viewport. If the contained node is not a Resizable, this value is ignored.