2016-04-09 14 views
0

以下のコードでは、ボタンが挿入されたグリッドが作成されます。私はそれに数字が付いた別のグリッドボックスをポップアップするメソッドをどのように追加できるのか知りたい。 2番目のポップアップグリッドボックスで番号が選択されると、クリックされた元のボタンのラベルが変更されます。以下の例を見て、誰かがテキスト "1"でボタンをクリックします。グリッドが1から5のボタンでポップアップします。ボタン5がクリックされます。ポップアップグリッドボックスが消え、テキスト "1"のボタンが "5"に変更されました。JavaFXを使用してポップアップボックスを作成する方法

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(); 
    } 
} 
+0

私はここに説明されていると思う:http://stackoverflow.com/questions/12717969/javafx-2-custom-popup-pane – DVarga

+0

リンクのいくつかに一時的なリンクがあり、そこにコード例があると思います – StonedRanger

答えて

0

あなたはPopupControl使用できるように試みることができる:ポップアップを閉じて、更新するあなたがpopup.hide()を呼び出すことができ、あなたのボタンリスナーで

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

をボタンのテキスト

0

前年比でも

 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(); 
関連する問題