Reputation: 5
Using Scenebuilder I created a TableView and I insert several items in to it from a local Database. The items are type of a Class Symptom I've created.
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;
}
}
I've given 3 columns to the TableView. Name,Category and an action column where the symptom button appears to perform a certain action.
This is my FXML Controller.
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());
}
}
Using an ObservableList
I fill the TableView. Now i want to create an action for every button according to the row it's placed in the TableView. I can perform an action to the Button as long as I've selected a row in the TableView (cause that gives me access to the Symptom object). How can I perform an action with the button just by clicking on it and without selecting a row?
P.S: Sorry for my bad English. If this is a duplicate post, please direct me to the right way of doing this.
Upvotes: 0
Views: 384
Reputation: 209225
The button should not be part of the model class Symptom
: instead you should create a TableCell
that displays the button.
So the table setup should be something like:
@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());
}
And then just remove the button entirely from the Symptom
class.
Upvotes: 2