2017-04-07 58 views
1

我在JavaFX中遇到了一个大问题。我创建了一个TableView,并向TableView添加了一些CheckBox。如果有人检查TableView中的复选框,我想触发一个事件。我尝试了一些不同的方法,但我总是遇到同样的问题。在显示GUI之前,我启动程序并且“CheckBox触发器事件”运行五次。之后,我可以点击复选框,但没有任何发生。这是我的代码,我希望你能帮助我。谢谢!JavaFX,如果有人检查桌面视图中的复选框,我该如何触发事件

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

} 

控制台输出

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 

回答

0

添加听众项目的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); 
     } 
    } 

    // ... 
} 
+0

Thx为解决方案。但我仍然有问题,我得到的字符串“System.out.println(”Checked属性的“+ trafficVolume.getName()+ ”从“+ wasChecked +”更改为“+ isNowChecked);”五次在控制台中,我真的不知道为什么。 – Sirox

+0

嗯,因为你要求在你的'cellFactory'的'selectedStateCallback'''call()'方法的控制台中打印字符串。所以它每次在工厂创建一个新单元格时都会显示出来 –

+0

这部分来自controller.class“return list.get(param).checkedProperty();”尽管我点击了一次,但它会永久运行5次。 – Sirox

相关问题