【发布时间】: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 列。名称、类别和一个操作列,其中出现症状按钮以执行特定操作。
这是我的 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