2

i hava a big problem in JavaFX. I created a TableView an i added some CheckBoxes to the TableView. I want to trigger an event if someone check the Checkbox in the TableView. I tried some different ways but I always have the same problem. I start the program and the "CheckBox trigger event" runs five times before showing the GUI. After that I can click on the Checkboxes but nothing happened. This is my code and I hope u can help me. Thx!

Controller.class

package GUI;

import java.io.IOException;
import java.util.ArrayList;

import javafx.beans.value.ObservableValue;
import ExternalRessources.TrafficVolume;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.CheckBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TVIDSelectionPanelController {


    @FXML
    private Button BACKBUTTON;
    @FXML
    private Button TEST;
    @FXML
    private MenuItem MENUITEMSETTINGS;
    @FXML
    private MenuBar MENUBAR;
    @FXML
    private GridPane GRIDPANETVID;
    @FXML
    private TableView<TrafficVolume> TABLETVID;
    @FXML
    private TableColumn<TrafficVolume, String> TABLECOLTVID;
    @FXML
    private TableColumn<TrafficVolume, String> TABLECOLFLIGHTLVL;
    @FXML
    private TableColumn<TrafficVolume, CheckBox> TABLECOLCHECKBOX;
    @FXML
    private AnchorPane TABLEPANE;

    private ExchangeController exchange;
    public ObservableList<TrafficVolume> list = FXCollections.observableArrayList();

    @FXML
    private void handleBACKBUTTON(ActionEvent event) throws IOException
    {         


    }

    public void init(ExchangeController ex)
    {
        this.exchange =ex;
    }

    @FXML   
    public void initalize() throws IOException
    {
        this.ChooseData();
    }

    @FXML
    private void ChooseData()
    {
        switch(exchange.getSelectedEBG())
        {
            case "Central":
            {
                this.createTable(exchange.getCentralTVID());
            }
            case "West":
            {
                this.createTable(exchange.getWestTVID());
            }
            case "East":
            {
                this.createTable(exchange.getEastTVID());
            }
            case "North":
            {
                this.createTable(exchange.getNorthTVID());
            }
            case "South":
            {
                this.createTable(exchange.getSouthTVID());
            }
        }
    }


    private void createTable(ArrayList<ArrayList<String>> ListTVID)
    {
        for(int i=0;i<ListTVID.size();i++)
        {
            list.add(new TrafficVolume(ListTVID.get(i).get(0),ListTVID.get(i).get(1)));
        }
        TableColumn<TrafficVolume, String> TVIDs = new TableColumn<TrafficVolume, String>("TV-ID");
        TableColumn<TrafficVolume, String> FLVL = new TableColumn<TrafficVolume, String>("Flight Level");   
        TableColumn<TrafficVolume, Boolean> checkedCol = new TableColumn<TrafficVolume, Boolean>("Active");
        TABLETVID.setItems(list);
        TABLETVID.getColumns().addAll(TVIDs,FLVL,checkedCol);
        TVIDs.setCellValueFactory(new PropertyValueFactory<TrafficVolume, String>("name"));
        FLVL.setCellValueFactory(new PropertyValueFactory<TrafficVolume, String>("flightLVL"));
        checkedCol.setCellValueFactory(new PropertyValueFactory<TrafficVolume, Boolean>("check"));
        checkedCol.setCellFactory(CheckBoxTableCell.forTableColumn(checkedCol));
        checkedCol.setEditable(true);
        TABLETVID.setEditable(true);

        checkedCol.setCellFactory(CheckBoxTableCell.forTableColumn(new Callback<Integer, ObservableValue<Boolean>>()
        {
            @Override
            public ObservableValue<Boolean> call(Integer param)
            {

                return list.get(param).checkedProperty();
            }
        }));


        for (TrafficVolume trafficVolume : list) {
            trafficVolume.checkedProperty().addListener((obs, wasChecked,isNowChecked) -> {
                  System.out.println("Checked property for " + trafficVolume.getName() +
                            " changed from "+wasChecked + " to " + isNowChecked);
            });
        }

    }




    //Switch the Scene
    @FXML
    private void handleSettings(ActionEvent event) throws IOException
    {       
        exchange.setTVIDSelectionPanelScene(MENUBAR.getParent().getScene());
        exchange.setTVIDSelectionPanelStage((Stage) MENUBAR.getParent().getScene().getWindow());
        exchange.setLastScene(exchange.getTVIDSelectionPanelScene());
        exchange.setLastStage(exchange.getTVIDSelectionPanelStage());
        exchange.initalizeStageOptions(event, MENUBAR);  

    }


}

TrafficVolume.class

    package ExternalRessources;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableBooleanValue;

public class TrafficVolume {
    private  SimpleStringProperty name;
    private  SimpleStringProperty flightLVL;
    private  BooleanProperty check;

    public TrafficVolume(String name, String flightLVL) 
    {
        this.name = new SimpleStringProperty(name);
        this.flightLVL = new SimpleStringProperty(flightLVL);
        this.check = new SimpleBooleanProperty(false);
    }

    public String getName() {
        return name.get();
    }

    public String getFlightLVL() {
        return flightLVL.get();
    }

    public Boolean getCheck() {
        return check.get();
    }

    public BooleanProperty checkedProperty()
    {
        System.out.println("test");
        return check;
    }

    public void setCheck(Boolean checked)
    {
        this.check.set(checked);
    }

    public ObservableBooleanValue isChecked()
    {
        System.out.println("test");
        return check;
    }

}

Console Output

Checked property for EDUCNTR changed from false to true
Checked property for EDUCNTR changed from false to true
Checked property for EDUCNTR changed from false to true
Checked property for EDUCNTR changed from false to true
Checked property for EDUCNTR changed from false to true
Checked property for EDUFFM1F changed from false to true
Checked property for EDUFFM1F changed from false to true
Checked property for EDUFFM1F changed from false to true
Checked property for EDUFFM1F changed from false to true
Checked property for EDUFFM1F changed from false to true
Checked property for EDUFFM14 changed from false to true
Checked property for EDUFFM14 changed from false to true
Checked property for EDUFFM14 changed from false to true
Checked property for EDUFFM14 changed from false to true
Checked property for EDUFFM14 changed from false to true
Checked property for EDUFFM24 changed from false to true
Checked property for EDUFFM24 changed from false to true
Checked property for EDUFFM24 changed from false to true
Checked property for EDUFFM24 changed from false to true
Checked property for EDUFFM24 changed from false to true
Checked property for EDUFFM34 changed from false to true
Checked property for EDUFFM34 changed from false to true
Checked property for EDUFFM34 changed from false to true
Checked property for EDUFFM34 changed from false to true
Checked property for EDUFFM34 changed from false to true
Sirox
  • 21
  • 1
  • 4

1 Answers1

1

Add listeners to the item's checkedProperty()s:

@FXML   
public void initalize() throws IOException
    {

    TableColumn<TrafficVolume, String> TVIDs = new TableColumn<TrafficVolume, String>("TV-ID");
    TableColumn<TrafficVolume, String> FLVL = new TableColumn<TrafficVolume, String>("Flight Level");   
    TableColumn<TrafficVolume, Boolean> checkedCol = new TableColumn<TrafficVolume, Boolean>("Active");
    TABLETVID.setItems(list);

    for (TrafficVolume trafficVolume : list) {
        trafficVolume.checkedProperty().addListener((obs, wasChecked, isNowChecked) -> {
            System.out.println("Checked property for " + trafficVolume.getName() +
                " changed from "+wasChecked + " to " + isNowChecked);
        }
    }

    // ...
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thx for the solution. But i have still the problem that i get the String "System.out.println("Checked property for " + trafficVolume.getName() + " changed from "+wasChecked + " to " + isNowChecked);" five times in console and i really don't know why. – Sirox Apr 07 '17 at 12:00
  • 1
    Well, because you are asking for that string to be printed in the console in your `cellFactory`'s `selectedStateCallback`'s `call()` method. So it is being displayed every time the factory creates a new cell – James_D Apr 07 '17 at 12:03
  • This part from the controller.class "return list.get(param).checkedProperty();" runs permanently 5times although i clicked one time. – Sirox Apr 07 '17 at 12:04
  • I guess I'm confused. Did you try the code I posted, and remove all your other `System.out.println`? – James_D Apr 07 '17 at 12:06
  • I updated my code. I checked the program with debugger but i dont get why it call the print five times. – Sirox Apr 07 '17 at 12:13
  • Yeah, not sure. It looks like the listeners are getting added multiple times, and since we don't know where and how often your `createTable` method is called, that's not possible for anyone but you to fix. Just ensure the listeners are only added to each `TrafficVolume` object once. – James_D Apr 07 '17 at 12:22
  • Yeah the Problem is the switch case, i belive something is wrong with the Syntax. Switch is called with "Central" but it runs createTable for each case. Thx for your help. Have a nice weekend! – Sirox Apr 07 '17 at 12:25
  • @Sirox Correct: you are missing the `break;` statements from your cases. – James_D Apr 07 '17 at 12:26
  • Yeah missed the break :D Thx SystemVerilog switching between multiple languages is not a good idea :D – Sirox Apr 07 '17 at 12:29