0

I have a issue with disabling rows in a TableView. I have a TableView which contains three columns. A name column, a value column and a checkbox column. If the user selects a checkbox all rows should be disabled with the same value like the value in the checked row. I tried to use the ReactFX2 framework to create a binding between a disabled property and a cell but it didnt work. Is there an easy way to handle my problem. Here is my code:

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;
    private  BooleanProperty disabled;

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

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

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

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

    public BooleanProperty checkedProperty()
    {
        return check;
    }

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

    public BooleanProperty disabledProperty()
    {
        return disabled;
    }

    public Boolean getDisabled() {
        return disabled.get();
    }




}

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.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableRow;
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()
    {
        String EBG = exchange.getSelectedEBG();
        switch(EBG)
        {
            case "Central":
            {
                this.createTable(exchange.getCentralTVID());
                break;
            }
            case "West":
            {
                this.createTable(exchange.getWestTVID());
                break;
            }
            case "East":
            {
                this.createTable(exchange.getEastTVID());
                break;
            }
            case "North":
            {
                this.createTable(exchange.getNorthTVID());
                break;
            }
            case "South":
            {
                this.createTable(exchange.getSouthTVID());
                break;
            }
        }
    }


    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);  

    }


}

I want to disable all rows which have the same flightlvl like the selected one. Example

name   lvl       checked
FFM14  100-300   x
FFM15  100-250
FFM24  300-400
FFM34  400-500

ffm15 should be disable because the lvl is a part of the selected lvl. Thanks for your help!!

Sirox
  • 21
  • 1
  • 4
  • Start [here](http://stackoverflow.com/questions/28671132/javafx-checkboxtablecell-get-actionevent-when-user-check-a-checkbox). – SedJ601 Apr 11 '17 at 13:35
  • the callback with checkbox is working, i'm talking about disabling a row like "gray out" – Sirox Apr 12 '17 at 11:35

1 Answers1

0

You can try the following. The example is for the last 3 digits only, so you'll need to amend it to suit your needs.

//add IntegerProperty as class field:

private IntegerProperty maxvalue = new SimpleIntegerProperty(999);

//change column type

@FXML

private TableColumn < TrafficVolume, Boolean > TABLECOLCHECKBOX;

// the initialize() method is labeled wrong. Should be :

@Override

public void initialize(URL url, ResourceBundle rb)

// the GUI design must already exist in a TVIDSelectionPanel.fxml file . No need to redeclare and add the columns

//Now the CellFactory part :

TABLECOLFLIGHTLVL.setCellValueFactory(new PropertyValueFactory<>("flightLVL"));
 TABLECOLFLIGHTLVL.setCellFactory(new Callback < TableColumn < TrafficVolume, String >, TableCell < TrafficVolume, String >>() {

        @Override
            public TableCell<TrafficVolume, String> call(TableColumn<TrafficVolume, String> param) {

                TableCell<TrafficVolume, String> cell = new TableCell<TrafficVolume, String>(){

                    @Override
                    protected void updateItem(String item, boolean empty) {
                        if(item != null){
                        super.updateItem(item, empty); 
                        setText(empty ? null : item);
                        Integer myVal1 = Integer.valueOf(item.substring(4));
                        TableRow<TrafficVolume> tr = getTableRow();
                             if(tr.getItem().getCheck()){
                                 maxvalue.set(myVal1);
                             }
                tr.disableProperty().bind(Bindings.greaterThan(myVal1, maxvalue));

                 DoubleBinding bind2 = new DoubleBinding() {

                                {super.bind(maxvalue);}
                            @Override
                            protected double computeValue() {
                                //you can put other statements here, e.g to change Style
                                if(myVal1 > maxvalue.get()){
                                    return 0.5;
                                }else{
                                    return 1.0;
                                }
                            }
                        };

               tr.opacityProperty().bind(bind2);
                        }
                    }

                };

                return cell;
            }
        });


TABLECOLCHECKBOX.setCellValueFactory(new PropertyValueFactory<>("check"));
TABLECOLCHECKBOX.setCellFactory(new Callback<TableColumn<TrafficVolume, Boolean>, TableCell<TrafficVolume, Boolean>>() {
 @Override
 public TableCell<TrafficVolume, Boolean> call(TableColumn<TrafficVolume, Boolean> param) {
 TableCell<TrafficVolume, Boolean> cell = new CheckBoxTableCell<TrafficVolume, Boolean>(){
 @Override
  public void updateItem(Boolean item, boolean empty) {
  if(item != null ){
  super.updateItem(item, empty); 
  TableRow<TrafficVolume> tr = getTableRow();
  if(tr.getItem().checkedProperty().get()){
  tr.setStyle("-fx-background-color:lightgreen");
  }
  }
  }
 };

  cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
 @Override
 public void handle(MouseEvent event) {
 TrafficVolume tv = (TrafficVolume) cell.getTableRow().getItem();
 String lvl = tv.getFlightLVL();
 if(tv.getCheck()){
 maxvalue.set(Integer.valueOf(ordNr.substring(4)));
 cell.getTableRow().setStyle("-fx-background-color:lightgreen");
 }else{
 maxvalue.set(999);
 cell.getTableRow().setStyle("");    
}
}
});
 return cell;
}
});

Make sure the checkboxes are mutually exclusive as radio buttons otherwise on startup only the last valid maxvalue is set (assuming all data is stored in a db)

John C
  • 1,795
  • 4
  • 27
  • 42
Helmwag
  • 460
  • 5
  • 7