I have got two questions. 1-) I have got a JavaFX tableView screen and need to add a cancel button there. I prepared my screen via fxml file, added columns in fxml. Four columns will be used to show data and last column (fifth column)will be used to delete this record. I need to add this button via fxml. How can I add button? My fxml file like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<TableView fx:id="appointmentsTable" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="appointmentList.AppointmentListController">
<columns>
<TableColumn prefWidth="105.0" text="Randevu Günü">
<cellValueFactory>
<PropertyValueFactory property="appointmentDay"/>
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="111.0" text="Randevu Saati" >
<cellValueFactory>
<PropertyValueFactory property="appointmentClock"/>
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="112.0" text="Randevu Sahibi">
<cellValueFactory>
<PropertyValueFactory property="appointmentOwner"/>
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="122.0" text="Randevu Doktoru" >
<cellValueFactory>
<PropertyValueFactory property="appointmentDoctor"/>
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="69.0" text="İptal Et" >
</TableColumn>
</columns>
</TableView>
2-) As I said that button will delete the record but I need the row id. How can I pass this id to the button action? My controller class is like this:
package appointmentList;
import connection.OpenConnection;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableView;
import pojos.Appointments;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
/**
* Created by ilkaygunel on 04/08/16.
*/
public class AppointmentListController implements Initializable{
@FXML
private TableView appointmentsTable;
@Override
public void initialize(URL location, ResourceBundle resources) {
ObservableList<Appointments> data =
FXCollections.observableArrayList(getTakenAppointments());
appointmentsTable.setItems(data);
}
public List<Appointments> getTakenAppointments(){
List<Appointments> appointmentsList = new ArrayList<>();
OpenConnection openConnection = new OpenConnection();
Connection connection = openConnection.getConnection();
PreparedStatement getTakenAppointmentsPreparedStatement=null;
try {
getTakenAppointmentsPreparedStatement = connection.prepareStatement("SELECT * FROM appointments");
ResultSet resultSet = getTakenAppointmentsPreparedStatement.executeQuery();
while (resultSet.next()){
appointmentsList.add(new Appointments(resultSet.getInt("appointmentId"), resultSet.getDate("appointmentDay"),resultSet.getString("appointmentClock"),
resultSet.getString("appointmentDoctor"),resultSet.getString("appointmentOwner")));
}
}
catch (Exception e){
System.out.println("An Error Occured! Error is:"+e);
}
return appointmentsList;
}
}
Appointments pojo class:
package pojos;
import java.util.Date;
/**
* Created by ilkaygunel on 04/08/16.
*/
public class Appointments {
int appointmentId;
Date appointmentDay;
String appointmentClock;
String appointmentDoctor;
String appointmentOwner;
public Appointments(int appointmentId,Date appointmentDay,String appointmentClock,String appointmentDoctor,String appointmentOwner){
this.appointmentId = appointmentId;
this.appointmentDay = appointmentDay;
this.appointmentClock = appointmentClock;
this.appointmentDoctor = appointmentDoctor;
this.appointmentOwner = appointmentOwner;
}
public int getAppointmentId() {
return appointmentId;
}
public void setAppointmentId(int appointmentId) {
this.appointmentId = appointmentId;
}
public Date getAppointmentDay() {
return appointmentDay;
}
public void setAppointmentDay(Date appointmentDay) {
this.appointmentDay = appointmentDay;
}
public String getAppointmentClock() {
return appointmentClock;
}
public void setAppointmentClock(String appointmentClock) {
this.appointmentClock = appointmentClock;
}
public String getAppointmentDoctor() {
return appointmentDoctor;
}
public void setAppointmentDoctor(String appointmentDoctor) {
this.appointmentDoctor = appointmentDoctor;
}
public String getAppointmentOwner() {
return appointmentOwner;
}
public void setAppointmentOwner(String appointmentOwner) {
this.appointmentOwner = appointmentOwner;
}
}