提问者:小点点

Gridpane javafx切换和取消切换的可点击单元格


我正在尝试创建一个在用户单击特定单元格时切换/取消切换的gridpane。例如,当用户单击内容为“*”的单元格时,我希望将该特定单元格的内容更改为空白单元格“”。类似地,如果用户单击内容为空白的单元格“”,我希望将该单元格的内容更改为内容为“*”的单元格。

基本上,我从一个部分输入的网格开始,如下所示:

我的格子最初输入如下,我正在跟踪一个布尔矩阵,它代表矩阵的一个单元格中是否应该有一个“*”。如果格子的单元格[i, j]中有一个“*”,那么矩阵[i,j]的值应该是true,否则应该是false

  boolean matrix[][] = new boolean[StateList.size()+1][RequirementList.size()+1];

    for( InstanceArtifact li: ListOfLinks) {
        int y=1; 
        for(InstanceArtifact re: RequirementList) {
            int x=1; 
            for(InstanceArtifact state: StateList) {
             if(li.getPropertyValue("linksource").equals(re) && li.getPropertyValue("linktarget").equals(state)) {

                     link= new Label("    *    "); 
                    //tick in cell (1,1)
                    grid.add(link, y, x); 
                    matrix[x][y]= true; 

                }
             else {
                   link= new Label("         "); 
                    //tick in cell (1,1)
                    grid.add(link, y, x); 

             }
             x++; 


        }
        y++; 
    }


    }




    }

我想做的是切换/取消切换星星,这是我试图用下面的代码做的,当我们点击一个包含“*”的单元格,意思是矩阵[i][j]=true时,我正在删除网格中相应的标签,并添加一个带有空文本的新标签。我也在相反的情况下做同样的事情,其中标签文本是空白的,我需要用包含星星的标签替换它。

         grid.getChildren().forEach(element -> {
                         element.setOnMouseClicked(new EventHandler<MouseEvent>() {

                    @Override
                    public void handle(MouseEvent event) {
                        int matrixX= GridPane.getRowIndex(element); 
                        int matrixY= GridPane.getColumnIndex(element); 
                        if(element!=null  && matrixX!=0 && matrixY!=0) {

                             System.out.println("matrixX: "+matrixX+"   matrixY: "+matrixY);
                             System.out.println("matrixY: "+matrixY+" "+ matrix[matrixX][matrixY]);
                            if(matrix[matrixX][matrixY]==true && matrixX!=0 && matrixY!=0) {

                                   System.out.println("HEY I AM HERE FIRSTTTTTTTTT");

                                  Node newnode= new Label("     ");
                                  GridPane.clearConstraints(element); 
                                 // grid.getChildren().remove(element); 

                                grid.add(newnode, matrixY, matrixX); 
                                matrix[matrixX][matrixY]=false; 
                                /*for(int l=0; l<RequirementList.size(); l++) {
                                    for(int p=0; p<StateList.size(); p++) {
                                        System.out.println(l + "  "+p +" "+matrix[l][p]);
                                    }
                                }*/


                                //grid.add(mynode, matrixY+1, matrixX+1, 1, 1); 


                            }
                            else if(matrix[matrixX][matrixY]==false && matrixX!=0 && matrixY!=0){

                                 System.out.println("HEY I AM HERE SECONDDDDDDD   ");

                                /* for(int l=0; l<RequirementList.size(); l++) {
                                        for(int p=0; p<StateList.size(); p++) {
                                            System.out.println(l + "  "+p +" "+matrix[l][p]);
                                        }
                                    }*/

                                    Node falsenode= new Label("   *      ");
                                    GridPane.clearConstraints(element); 
                                     // grid.getChildren().remove(element); 

                                    grid.add(falsenode, matrixY, matrixX); 
                                    matrix[matrixX][matrixY]=true; 


                            }


                         //   System.out.println("Row: " + GridPane.getRowIndex(element));
                           // System.out.println("Column: " + GridPane.getColumnIndex(element));
                        }   
                        }

                });
                     });

我的代码没有按预期运行,我希望每当用户单击单元格时单元格被切换/取消切换,代码仅在用户第一次单击给定单元格时执行,如果用户多次单击同一单元格(不止一次),则什么都不会发生。

此外,我尝试删除标签的代码行不起作用:

       grid.getChildren().remove(element); 

共1个答案

匿名用户

这可以通过切换按钮更好地服务。代码中的注释。

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication60 extends Application {   

    @Override
    public void start(Stage primaryStage) {     
        //Start create GUI
        Label lblRequirement11 = new Label("Requirement1");
        Label lblRequirement12 = new Label("Requirement2");
        Label lblRequirement13 = new Label("Requirement3");
        GridPane.setConstraints(lblRequirement11, 1, 0);
        GridPane.setConstraints(lblRequirement12, 2, 0);
        GridPane.setConstraints(lblRequirement13, 3, 0);

        Label lblState1 = new Label("State1");
        ToggleButton toggleButton11 = new ToggleButton();
        toggleButton11.setMaxWidth(Double.MAX_VALUE);
        ToggleButton toggleButton12 = new ToggleButton();
        toggleButton12.setMaxWidth(Double.MAX_VALUE);
        ToggleButton toggleButton13 = new ToggleButton();
        toggleButton13.setMaxWidth(Double.MAX_VALUE);
        GridPane.setConstraints(lblState1, 0, 1);
        GridPane.setConstraints(toggleButton11, 1, 1);
        GridPane.setConstraints(toggleButton12, 2, 1);
        GridPane.setConstraints(toggleButton13, 3, 1);

        Label lblState2 = new Label("State2");
        ToggleButton toggleButton21 = new ToggleButton();
        toggleButton21.setMaxWidth(Double.MAX_VALUE);
        ToggleButton toggleButton22 = new ToggleButton();
        toggleButton22.setMaxWidth(Double.MAX_VALUE);
        ToggleButton toggleButton23 = new ToggleButton();
        toggleButton23.setMaxWidth(Double.MAX_VALUE);
        GridPane.setConstraints(lblState2, 0, 2);
        GridPane.setConstraints(toggleButton21, 1, 2);
        GridPane.setConstraints(toggleButton22, 2, 2);
        GridPane.setConstraints(toggleButton23, 3, 2);

        Label lblState3 = new Label("State3");
        ToggleButton toggleButton31 = new ToggleButton();
        toggleButton31.setMaxWidth(Double.MAX_VALUE);
        ToggleButton toggleButton32 = new ToggleButton();
        toggleButton32.setMaxWidth(Double.MAX_VALUE);
        ToggleButton toggleButton33 = new ToggleButton();
        toggleButton33.setMaxWidth(Double.MAX_VALUE);
        GridPane.setConstraints(lblState3, 0, 3);
        GridPane.setConstraints(toggleButton31, 1, 3);
        GridPane.setConstraints(toggleButton32, 2, 3);
        GridPane.setConstraints(toggleButton33, 3, 3);

        GridPane root = new GridPane(); 
        root.setVgap(5);
        root.setHgap(5);
        root.getChildren().addAll(lblRequirement11, lblRequirement12, lblRequirement13);
        root.getChildren().addAll(lblState1, toggleButton11, toggleButton12, toggleButton13);
        root.getChildren().addAll(lblState2, toggleButton21, toggleButton22, toggleButton23);
        root.getChildren().addAll(lblState3, toggleButton31, toggleButton32, toggleButton33);
        //End create GUI

        //Start create ToggleButtons' event handlers.
        List<ToggleButton> toggleButtonList = new ArrayList();
        toggleButtonList.add(toggleButton11);
        toggleButtonList.add(toggleButton12);
        toggleButtonList.add(toggleButton13);
        toggleButtonList.add(toggleButton21);
        toggleButtonList.add(toggleButton22);
        toggleButtonList.add(toggleButton23);
        toggleButtonList.add(toggleButton31);
        toggleButtonList.add(toggleButton32);
        toggleButtonList.add(toggleButton33);

        for(ToggleButton tempToggleButton : toggleButtonList)
        {
            tempToggleButton.setOnAction(actionEvent -> {
                if(tempToggleButton.isSelected())
                {
                    tempToggleButton.setText("*");
                }
                else
                {
                    tempToggleButton.setText("");
                }
            });
        }
        ////End create ToggleButtons' event handlers.



        Scene scene = new Scene(root, 400, 300);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}