Add Scrollbar in javafx - javafx

I want to add Scrollbar to this code. How can it possible- If possible explain it . i tried but not succeed .
public void start(Stage primaryStage) {
BorderPane border=new BorderPane();
GridPane gridpane=new GridPane();
HBox hbox=new HBox();
Button btn=new Button("btn");
hbox.getChildren().add(btn);
btn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
Label text=new Label("hello");
gridpane.add(text, 0, b);
b++;
}
}
);
border.setTop(hbox);
border.setLeft(gridpane);
Scene scene=new Scene(border,460,250);
primaryStage.setScene(scene);
primaryStage.show();
}

Instead of
border.setLeft(gridPane);
do
border.setLeft(new ScrollPane(gridPane));

Related

How to call the public void start(Stage primaryStage) Method from a different class

I'am new to JavaFX, so my coding is not the best. My programme has one Stage with 2 different Scenes. For a better overview I have created a new class for the second Scene. From this second Scene I want to go back to first one, using a button. The method: primaryStage.setScene(scene) is not working and I dont know why. Can someone please help me? :)
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
GridPane primarygridpane = new GridPane();
Scene scene = new Scene(primarygridpane,400,400);
...
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
public class Harnwegsinfektion {
public static void create (Stage primaryStage){
GridPane secondarygridpane = new GridPane();
Scene scene2 = new Scene(secondarygridpane,400,400);
buttonBack.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(scene);
}
});
primaryStage.setScene(scene2);
primaryStage.show();
}
}
The problem is that your scene variable is named scene2 and you are calling setScene() with scene.
I don't really know when and where create() is called, but if you want to go back to the first scene from the button, you have to save the original one before switching :
public static void create (Stage primaryStage){
GridPane secondarygridpane = new GridPane();
Scene scene2 = new Scene(secondarygridpane,400,400);
Scene originalScene=primaryStage.getScene(); //save the previous scene
buttonBack.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent e) {
primaryStage.setScene(originalScene); //go back to the previous scene
}
});
primaryStage.setScene(scene2);
primaryStage.show();
}

How to let a child control receive mouse events?

When a parent component has transparent mouse enabled all child controls are no longer able to receive mouse events. Is it possible exclude a child from this rule.
public class TransparentMouseTest extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
#Override
public void start(Stage stage)
{
Pane root = new StackPane();
root.getChildren().add(new TextField());
Pane p = new StackPane();
p.setMouseTransparent(true);
p.setStyle("-fx-background-color:#F001;");
p.getChildren().add(new Button("Button should be able \nto receive mouse events."));
root.getChildren().add(p);
Scene scene = new Scene(root, 400, 300);
stage.setScene(scene);
stage.show();
}
}

Show Colorpicker from Contextmenu

I want to show Colorpicker from Context Menu:
ColorPicker colorssPicker = new ColorPicker();
final MenuItem resizeItem = new MenuItem("Option 1");
resizeItem.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
}
});
final MenuItem resizesItem = new MenuItem();
resizesItem.setGraphic(colorssPicker);
resizesItem.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
}
});
final ContextMenu menu = new ContextMenu();
menu.getItems().addAll(resizeItem, resizesItem);
sc.setOnMouseClicked(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
if (MouseButton.SECONDARY.equals(event.getButton()))
{
menu.show(primaryStage, event.getScreenX(), event.getScreenY());
}
}
});
This code is not working, I can't see Colorpicker when I click on the Contextmenu "Choose Color". What is the proper way to implement this?
I get this result:
This snippet will let you show a ColorPicker control embedded into a ContextMenu.
You can style it so it doesn't look like a button, by setting its backgound color.
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
final ColorPicker colorssPicker = new ColorPicker();
colorssPicker.setStyle("-fx-background-color: white;");
final MenuItem otherItem = new MenuItem(null, new Label("Other item"));
final MenuItem resizeItem = new MenuItem(null,colorssPicker);
resizeItem.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event)
{
root.setBackground(new Background(new BackgroundFill(colorssPicker.getValue(),null,null)));
}
});
final ContextMenu menu = new ContextMenu(otherItem,resizeItem);
Scene scene = new Scene(root, 300, 250);
scene.setOnMouseClicked(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event){
if (MouseButton.SECONDARY.equals(event.getButton())){
menu.show(primaryStage, event.getScreenX(), event.getScreenY());
}
}
});
primaryStage.setScene(scene);
primaryStage.show();
}

javafx To have a scroll method of Panel in the scrollPanel setPannable (true) does not work

I put a scalable pane in the scrollpanel then scroll way after his bigger scrollpanel setPannable (true) method does not work, I don't know how to solve, hope to get help
public class CreateAdvancedStage extends Application {
private void init(Stage primaryStage) {
BorderPane root = new BorderPane();
root.setPrefSize(200, 220);
primaryStage.setScene(new Scene(root));
ScrollPane scrolPanel = new ScrollPane();
scrolPanel.setPannable(true);//it do not work
scrolPanel.setContent(getCenterPane());
root.setCenter(scrolPanel);
}
Pane panel;
int tag = 10;
public Pane getCenterPane() {
panel = new Pane();
panel.setStyle("-fx-background-color:red");
panel.setPrefSize(100, 100);
panel.setOnScroll(new EventHandler<ScrollEvent>() {
#Override
public void handle(ScrollEvent t) {
System.out.println("OnScroll");
//this to set panSize
if (panel != null) {
panel.setPrefSize(100 + tag, 100 + tag);
tag++;
}
}
});
return panel;
}
#Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Minimize panel button

I'm working on this example of simple panel.
public class MainApp extends Application {
private static BorderPane bp;
#Override
public void start(Stage stage) throws Exception {
FlowPane flow = new FlowPane();
flow.setPadding(new Insets(50, 5, 5, 5));
flow.setVgap(15);
flow.setHgap(15);
flow.setAlignment(Pos.CENTER);
HBox thb = new HBox();
thb.setPadding(new Insets(13, 13, 13, 13));
thb.setStyle("-fx-background-color: gray");
DropShadow ds = new DropShadow();
ds.setOffsetY(3.0);
ds.setOffsetX(3.0);
ds.setColor(Color.GRAY);
bp = new BorderPane();
bp.setEffect(ds);
bp.setPrefSize(600, 500);
bp.setMaxSize(600, 500);
bp.setStyle("-fx-background-color: white;");
bp.setTop(thb);
flow.getChildren().add(bp);
Scene scene = new Scene(flow, 1200, 800);
scene.getStylesheets().add("/styles/Styles.css");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Can you help me to create button which minimizes the panel. I want when I click the button to shrink the size of the panel.
I don't understand basically what you trying to do. I think this might help you.
final Button btnmin = new Button();
flow.getChildren().add(btnmini)
btnmin.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("mini.gif"))));
btnmin.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
Stage stage = (Stage)btnmin.getScene().getWindow();
stage.setIconified(true);
}
});

Resources