0

the code below creates a Grid which is populated with buttons. I would like to know how one can add a method that pops up another grid box with numbers on it. When the number is selected on the second pop up grid box it changes the label on the original button that was clicked. Take the example below, someone clicks on the button with the text "1". A grid pops up with buttons labelled 1 to 5. Button 5 is clicked. The pop up grid box disappears and the button with the text "1" on it is now changed to "5".

import javafx.application.*;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;

    public class GUI extends Application {
        public static void main(String[] args) {
            Application.launch(args);
    }


    @Override public void start(Stage primaryStage)
    {
        final int HGAP = 2;
        final int VGAP = 2;
        final int BUTTONSIZE = 50;
        final int INSET = 5;
        final int SIZE = 4;

        GridPane root = new GridPane();
        root.setPadding(new Insets(INSET));
        root.setHgap(HGAP);
        root.setVgap(VGAP);
        root.setAlignment(Pos.CENTER);

        final Button[][] btn = new Button[SIZE][SIZE];
        final Paint background = Color.TURQUOISE;

        int index = 0;
        for ( int theCol = 0; theCol < SIZE; theCol++) {
            for ( int theRow = 0; theRow < SIZE; theRow++) {

                btn[theRow][theCol] = new Button(""+ index);
                btn[theRow][theCol].setPrefSize(BUTTONSIZE, BUTTONSIZE);
                root.add(btn[theRow][theCol], theRow, theCol);
                index++;
                btn[theRow][theCol].setOnMouseClicked(new EventHandler<MouseEvent>()
                {
                    @Override
                    public void handle(MouseEvent arg0) 
                    {
                        Button b= (Button)arg0.getSource();
                        System.out.println(b.getText());
                    }
                });
            }
        }

        Scene scene = new Scene(root,background);
        primaryStage.setTitle("Grid");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
StonedRanger
  • 3
  • 1
  • 5

2 Answers2

0

Yoy could try in such a way also

        GridPane grid = new GridPane();
        grid.setHgap(10);
        grid.setVgap(10);
        final Text infoText = new Text();
        grid.setPadding(new Insets(10, 10, 10, 10));
        grid.add(infoText, 0, 4, 2, 1);
        final Dialog dlg = new Dialog(null, "dialog");
        dlg.setContent(grid);
        dlg.show();
manikant gautam
  • 3,521
  • 1
  • 17
  • 27
0

You can use a PopupControl:

  PopupControl popup = new PopupControl();
  popup.getScene().setRoot(yourGridPane);
  popup.show(yourGridPane.getScene().getWindow());

In your button listener you can call popup.hide() to close the popup and update the button text

jns
  • 6,017
  • 2
  • 23
  • 28