访问TableView中的按钮

时间:2018-02-10 19:52:56

标签: java sqlite button javafx tableview

使用Scenebuilder我创建了一个TableView,并从本地数据库中插入了几个项目。这些项目是我创建的类症状的类型。

package javafxapplication4;

import javafx.scene.control.Button;


public class Symptom {

private String name,category,symptomId;
private Button symptom;

public Symptom(String name,String category,String symptomId){
    this.name = name;
    this.category = category;
    this.symptomId = symptomId;
    this.symptom = new Button("Select Symptom");
    //setGraphic(add_symptom);
}

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

public String getCategory(){
    return this.category;
}


public void setName(String name){
    this.name = name;
}

public void setCategory(String category){
    this.category = category;
}

public void setSymptom(Button button){
    symptom = button;
}

public Button getSymptom(){
    return symptom;
}

public void setSymptomId(String symptomId){
    this.symptomId = symptomId;
}

public String getSymptomId(){
    return this.symptomId;
}

}

我已经为TableView提供了3列。名称,类别和操作列,其中症状按钮显示为执行特定操作。

TableView

这是我的FXML控制器。

package javafxapplication4;

import java.net.URL;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class Symptom_DataController implements Initializable {

/**
 * Initializes the controller class.
 */
@FXML 
private TableView<Symptom> symptomsTable;

@FXML
private TableColumn<Symptom,String> nameColumn;

@FXML
private TableColumn<Symptom,String> categoryColumn;

@FXML
private TableColumn<Symptom,String> actionColumn;

@FXML
private Button cancel;

@FXML
private Button diagnose;

public LoginModel loginModelSymptomsTable = new LoginModel();

@FXML
private void cancelAction(ActionEvent e) throws Exception{
    Stage stage;
    Scene scene;
    Parent root;
    if ( e.getSource() == cancel ) {
        stage = (Stage) cancel.getScene().getWindow();
        root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
        scene = new Scene(root);    

        stage.setX(0);
        stage.setY(0);
        stage.setMinWidth(800);
        stage.setMinHeight(600);
        stage.setWidth(1024);
        stage.setHeight(768);
        stage.setScene(scene);
        stage.show();
    }


}


@Override
public void initialize(URL url, ResourceBundle rb) {
    nameColumn.setCellValueFactory(new PropertyValueFactory<>("Name"));
    categoryColumn.setCellValueFactory(new PropertyValueFactory<>("Category"));
    actionColumn.setCellValueFactory(new PropertyValueFactory<Symptom,String>("symptom"));
    symptomsTable.setItems(loginModelSymptomsTable.selectSymptomValue());

}    

}

使用ObservableList我填写TableView。现在我想根据它放在TableView中的行为每个按钮创建一个动作。只要我在TableView中选择了一行(因为这使我能够访问Symptom对象),我就可以对Button执行操作。如何通过单击按钮而不选择行来执行按钮操作?

P.S:抱歉我的英语不好。如果这是一个重复的帖子,请指导我这样做的正确方法。

1 个答案:

答案 0 :(得分:0)

该按钮不应该是模型类Symptom的一部分:相反,您应该创建一个显示按钮的TableCell

所以表设置应该是这样的:

@FXML 
private TableView<Symptom> symptomsTable;

@FXML
private TableColumn<Symptom,String> nameColumn;

@FXML
private TableColumn<Symptom,String> categoryColumn;

// value for the action column is just going to be the entire symptom,
// so the type of the column is TableColumn<Symptom, Symptom>
@FXML
private TableColumn<Symptom,Symptom> actionColumn;

@Override
public void initialize(URL url, ResourceBundle rb) {
    nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
    categoryColumn.setCellValueFactory(new PropertyValueFactory<>("category"));

    // just provide the entire row as the value for cells in the actionColumn:
    actionColumn.setCellValueFactory(cellData -> new SimpleObjectProperty<>(cellData.getValue()));

    // cell factory which provides cell which display a button:
    actionColumn.setCellFactory(column -> new TableCell<Symptom, Symptom>() {
        private final Button button = new Button("Select Symptom");

        {
            button.setOnAction(e -> {
                Symptom symptom = getItem();
                // do whatever you need with symptom..
            });
        }

        @Override
        protected void updateItem(Symptom item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setGraphic(null);
            } else {
                setGraphic(button);
            }
        }
    });

    symptomsTable.setItems(loginModelSymptomsTable.selectSymptomValue());

}  

然后只需从Symptom类中完全删除该按钮。