【问题标题】:Accessing buttons in TableView访问 TableView 中的按钮
【发布时间】:2018-02-10 19:52:55
【问题描述】:

我使用 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 中选择了一行,我就可以对 Button 执行操作(因为这使我可以访问 Symptom 对象)。如何仅通过单击按钮而不选择行来执行操作?

P.S:对不起,我的英语不好。如果这是重复的帖子,请指导我正确的做法。

【问题讨论】:

    标签: java sqlite button javafx tableview


    【解决方案1】:

    按钮不应该是模型类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 类中完全删除按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多