We have a couple of buttons. When I first click a button it's get selected. When I click it back button get Deselected. we have a method that Deselects all selected button at once.
I try storing Boolean(object) in ArrayList. That I thought I might take object references and change object. ( in other words, reset flag) But it's not working at all.
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>
Buttons added to pane
Two button selected
Reset
Double clicked
- Note: I don't wanna want to load all Button to flow pane again because in my actual Application I have hundreds of button. Please don't suggest me that.