如何在javaFX中一次更改对象的所有实例中的Boolean(flag)[完整示例]

时间:2018-10-29 07:15:23

标签: java javafx fxml

我们有几个按钮。当我第一次单击一个按钮时,它被选中。当我单击它后退按钮时,取消选中它。我们有一种可以立即取消选择所有选定按钮的方法。

我尝试将Boolean(object)存储在ArrayList中。我以为我可以接受对象引用并更改对象。 (换句话说,重置标志) 但这根本不起作用。

Controller.java

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;

public class Controller implements Initializable
{

    @FXML
    private FlowPane flowPane;

    //String 
    private ArrayList<String> list ;

    @Override
    public void initialize(URL location, ResourceBundle resources)
    {

        //Init String Array list
                list = new ArrayList<>();
                list.add("RED");
                list.add("GREEN");
                list.add("BLUE");
                list.add("YELLOW");
                list.add("WHITE");      
                list.add("DARK RED");
                list.add("DARK GREEN");
                list.add("DARK BLUE");
                list.add("DARK YELLOW");
                list.add("DARK WHITE");
    }

    //Add button
    @FXML
    private  void Add_btn(ActionEvent event)
    {   
        //Reading String Create nodes on flowPane
        for(String name : list) 
        {
            flowPane.getChildren().add(getBtn(name));
        }

    }
//Selected Button Array list
    private ArrayList<Button> selected_buttons = new ArrayList<>();

//Add Object to Button Array List
    private void add_to_list(Button btn)
    {
        selected_buttons.add(btn);
    }

//Remove Object from Button Array list
    private void remove_from_list(Button btn)
    {
        selected_buttons.remove(btn);
    }

//Reset click boolean
    private ArrayList<Boolean> reset_boolean = new ArrayList<>();

//Add bool to Arrylist
    private void add_to_reset_boolean(boolean bool) {
        reset_boolean.add(bool);
    }
//Remove bool in ArrayList
    private void remove_from_reset_boolean(boolean bool) {
        reset_boolean.remove(bool);
    }




//Returns java FX Button object
    private Button getBtn(String name)
    {
        Button btn = new Button(name);
        btn.setOnAction(new EventHandler<ActionEvent>()
        {   
            //Flag to check whether i click it first time or second time

            //private boolean first_click  = true;

            private Boolean first_click = new Boolean(true);
            @Override
            public void handle(ActionEvent event)
            {   
                //First time click,
                if(first_click)     
                { 
                    first_click = false;
                    //Show Selected
                    btn.setText(btn.getText()+" [Selected]");
                    //Add to selected Button Array list
                    add_to_list(btn);

                    add_to_reset_boolean(first_click);



                } else if(!first_click)     
                {
                    first_click = true;
                    //Remove Selected
                    btn.setText(btn.getText().replace(" [Selected]", ""));
                    //Remove from selected Button Array list
                    remove_from_list(btn);

                    remove_from_reset_boolean(first_click);
                }
            }


        });
        return btn;
    }
    //Reset Button
    @FXML
    private void reset_btn(ActionEvent event) 
    {   
        //On whatever remain in array list
        //On ever button in selected array list
        for(Button btn : selected_buttons) {

            //Remove Selected from Button
            btn.setText(btn.getText().replace(" [Selected]", ""));

        }
        //Every bool value
        for(Boolean bool : reset_boolean) { //I think im doing it wrong here
            bool = false;
        }
    }


}

Main.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application
{
    @Override
    public void start(Stage primaryStage) {
        try {


            Parent root = FXMLLoader.load(getClass().getResource("Window.fxml"));
            Scene scene = new Scene(root);          
            primaryStage.setScene(scene);           
            primaryStage.setMinWidth(350);
            primaryStage.show();

        } catch(Exception e) {
            System.err.println("Luncher Failed" +e);
        }
    }

    public static void main(String[] args) 
    {

        launch(args);

    }
}

Window.fxmxl

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="booleanFix.Controller">
   <children>
      <FlowPane fx:id="flowPane" alignment="CENTER" columnHalignment="CENTER" hgap="10.0" layoutX="69.0" layoutY="60.0" prefHeight="61.0" prefWidth="482.0" vgap="20.0" />
      <Button layoutX="351.0" layoutY="330.0" mnemonicParsing="false" onAction="#reset_btn" prefHeight="47.0" prefWidth="117.0" text="Reset">
         <font>
            <Font size="20.0" />
         </font>
      </Button>
      <Button layoutX="164.0" layoutY="330.0" mnemonicParsing="false" onAction="#Add_btn" prefHeight="47.0" prefWidth="117.0" text="Add">
         <font>
            <Font size="20.0" />
         </font>
      </Button>
   </children>
</AnchorPane>

添加到窗格的按钮

Nodes Added to flow pane

选择了两个按钮

Node Selected

重置

enter image description here

无法在首次点击时选择 enter image description here

双击

enter image description here

  • 注意:我不想再次将所有Button加载到流窗格,因为在我的实际应用程序中,我有数百个Button。请不要建议我。

1 个答案:

答案 0 :(得分:0)

解决方案非常简单。

Controller.java

package booleanFix;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.FlowPane;

public class Controller implements Initializable
{

    @FXML
    private FlowPane flowPane;

    //String 
    private ArrayList<String> list ;

    @Override
    public void initialize(URL location, ResourceBundle resources)
    {

        //Init String Array list
                list = new ArrayList<>();
                list.add("RED");
                list.add("GREEN");
                list.add("BLUE");
                list.add("YELLOW");
                list.add("WHITE");      
                list.add("DARK RED");
                list.add("DARK GREEN");
                list.add("DARK BLUE");
                list.add("DARK YELLOW");
                list.add("DARK WHITE");
    }

    //Add button
    @FXML
    private  void Add_btn(ActionEvent event)
    {   
        //Reading String Create nodes on flowPane
        for(String name : list) 
        {
            flowPane.getChildren().add(getBtn(name));
        }

    }
//Selected Button Array list
    private ArrayList<MyButton> selected_buttons = new ArrayList<>();

//Add Object to Button Array List
    private void add_to_list(MyButton btn)
    {
        selected_buttons.add(btn);
    }

//Remove Object from Button Array list
    private void remove_from_list(MyButton btn)
    {
        selected_buttons.remove(btn);
    }





//Returns java FX Button object
    private MyButton getBtn(String name)
    {
        MyButton btn = new MyButton(name);


        btn.setOnAction(new EventHandler<ActionEvent>()
        {   
            //Flag to check whether i click it first time or second time


            @Override
            public void handle(ActionEvent event)
            {   
                //First time click,
                if(btn.click)   
                { 
                    btn.click = false;
                    //Show Selected
                    btn.setText(btn.getText()+" [Selected]");

                    //Add to selected Button Array list
                    add_to_list(btn);





                } else if(!btn.click)   
                {   
                    btn.click = true;
                    //Remove Selected
                    btn.setText(btn.getText().replace(" [Selected]", ""));

                    //Remove from selected Button Array list
                    remove_from_list(btn);

                }
            }


        });
        return btn;
    }
    //Reset Button
    @FXML
    private void reset_btn(ActionEvent event) 
    {   
        //On whatever remain in array list
        //On ever button in selected array list
        for(MyButton btn : selected_buttons) {

            btn.click = true;

            btn.setText(btn.getText().replace(" [Selected]", ""));


        }

    }


}

MyButton 只需创建扩展Button的新类

package booleanFix;

import javafx.scene.control.Button;

public class MyButton extends Button
{
    public boolean click = true;

    //super(name); mean's Button.setText(name)
    public MyButton(String name)
    {
        super(name);

    }

}