2017-04-07 23 views
1

나는 JavaFX에서 큰 문제가 있습니다. 내가 TableView에 몇 가지 CheckBoxes를 추가 한 TableView를 만들었습니다. 누군가가 TableView의 체크 박스를 체크하면 이벤트를 트리거하고 싶습니다. 몇 가지 방법을 시도했지만 항상 같은 문제가 있습니다. 프로그램을 시작하고 GUI를 보여주기 전에 "CheckBox trigger event"가 다섯 번 실행됩니다. 그 후 나는 체크 박스를 클릭 할 수 있지만 아무 일도 일어나지 않았다. 이것은 내 코드이며, 내가 도와 줄 수 있기를 바랍니다. 고마워!JavaFX, 누군가가 Tableview의 CheckBox를 체크하면 이벤트를 트리거하는 방법

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()들에 청취자를 추가합니다

@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 ("+ trafficVolume.getName()"+"wasChecked + "isNowChecked 변경되었습니다"Checked 속성을 얻을 문제가 "; 콘솔에서 5 번, 정말 이유를 모르겠다. – Sirox

+0

글쎄, 그 문자열이'cellFactory'의'selectedStateCallback'의'call()'메소드에서 콘솔에 출력되기를 요구하기 때문입니다. 따라서 공장에서 새 셀을 만들 때마다 표시됩니다. –

+0

이 부분은 controller.class의 "return list.get (param) .checkedProperty();" 내가 한 번 클릭했지만 영구적으로 5 회 실행됩니다. – Sirox