我想使用在表视图中加载的数据填充一些文本字段。 有没有办法创建一个OnMouseClick动作事件,该事件将用表视图中的数据填充一堆文本字段?
我想要这个是因为我想创建一个更新方法,在该方法中,您不必再次更改整个服务详细信息(例如价格,部门,可用性和名称),而只需更改一件特定的事情,但这是这不是问题,我知道该怎么做,只需要使用tableview提供的数据自动为其填充文本字段即可。
在代码中提供了一些注释。
该表格视图的代码:
public class ProgramariDBController implements Initializable {
@FXML
private TextField Serviciu1;
@FXML
private TextField Departament1;
@FXML
private TextField Pret1;
@FXML
private TextField Disponibilitate1;
@FXML
private Label rows;
@FXML
private Label rows;
@FXML
private TextField cpn;
@FXML
private TextField nume1;
@FXML
private TableView<DetaliiProgramari> ProgrDB;
@FXML
private TableColumn<DetaliiProgramari, String> Nume;
@FXML
private TableColumn<DetaliiProgramari, String> Prenume;
@FXML
private TableColumn<DetaliiProgramari, String> Data;
@FXML
private TableColumn<DetaliiProgramari, String> Ora;
@FXML
private TableColumn<DetaliiProgramari, String> Departament;
@FXML
private TableColumn<DetaliiProgramari, String> Doctor;
@FXML
private TableColumn<DetaliiProgramari, String> Nr_telefon;
public LogareController Numeutilzator = new LogareController();
private ObservableList<DetaliiProgramari> Info;
@Override
public void initialize(URL url, ResourceBundle ResurcesFORDAYS) {
// TODO
}
@FXML
private void AfiseazaProgramari(ActionEvent event) {
try {
ConectaredB ConectaredB=new ConectaredB();
Connection conectare=ConectaredB.logareDB();
Info = FXCollections.observableArrayList();
String CNP=cpn.getText();
ResultSet IncDate = conectare.createStatement().executeQuery("SELECT * FROM programari where CNP=" + CNP);
while (IncDate.next()) {
Info.add(new DetaliiProgramari(IncDate.getString(2), IncDate.getString(3), IncDate.getString(4),
IncDate.getString(5), IncDate.getString(6), IncDate.getString(7), IncDate.getString(8)));
//I have something like this
Serviciu1.setText(IncDate.getString(2)); it did work, but it loaded the first thing that was created in TipServiciu, which is a column in the table, so that doesn't really help, and I won't even paste the whole thing, because it will just be confusing for no reason.
}
} catch (SQLException ex) {
System.err.println("Error"+ex);
}
Nume.setCellValueFactory(new PropertyValueFactory<>("Nume"));
Prenume.setCellValueFactory(new PropertyValueFactory<>("Prenume"));
Data.setCellValueFactory(new PropertyValueFactory<>("Data"));
Ora.setCellValueFactory(new PropertyValueFactory<>("Ora"));
Departament.setCellValueFactory(new PropertyValueFactory<>("Departament"));
Doctor.setCellValueFactory(new PropertyValueFactory<>("Doctor"));
Nr_telefon.setCellValueFactory(new PropertyValueFactory<>("Nr_telefon"));
ProgrDB.setItems(null);
ProgrDB.setItems(Info);
}
@FXML
private void delete(ActionEvent event) throws IOException {
try {
String sql = "delete FROM programari where CNP=? ";
ConectaredB ConectaredB=new ConectaredB();
Connection conexiune=ConectaredB.logareDB();
PreparedStatement ps = conexiune.prepareStatement(sql);
String CNP=cpn.getText();
ps.setString(1, CNP);
int count = ps.executeUpdate();
rows.setText("rows" +count);
} catch (Exception e) {
// TODO: handle exception
}
}
}
setter和getter,如果它们有任何可能的帮助:
public class ServiciiSittersGettersController {
private final StringProperty Serviciu;
private final StringProperty Disponibilitate;
private final StringProperty Pret;
private final StringProperty Departament;
//Default constructor
public ServiciiSittersGettersController(String Serviciu, String Disponibilitate, String Pret, String Departament) {
this.Serviciu = new SimpleStringProperty(Serviciu);
this.Disponibilitate = new SimpleStringProperty(Disponibilitate);
this.Pret = new SimpleStringProperty(Pret);
this.Departament = new SimpleStringProperty(Departament);
}
public String getDepartament() {
return Departament.get();
}
public StringProperty departamentProperty() {
return Departament;
}
public void setDepartament(String departament) {
this.Departament.set(departament);
}
public String getServiciu() {
return Serviciu.get();
}
public StringProperty serviciuProperty() {
return Serviciu;
}
public void setServiciu(String serviciu) {
this.Serviciu.set(serviciu);
}
public String getDisponibilitate() {
return Disponibilitate.get();
}
public StringProperty disponibilitateProperty() {
return Disponibilitate;
}
public void setDisponibilitate(String disponibilitate) {
this.Disponibilitate.set(disponibilitate);
}
public String getPret() {
return Pret.get();
}
public StringProperty pretProperty() {
return Pret;
}
public void setPret(String pret) {
this.Pret.set(pret);
}
}
答案 0 :(得分:1)
您必须将监听器添加到TableView's
getSelectionModel().selectedItemProperty()
中。
关键代码:
table.getSelectionModel().selectedItemProperty().addListener((obs, oldVal, newVal) ->
{
if (newVal != null) {
tfFirstName.setText(newVal.getFirstName());
tfLastName.setText(newVal.getLastName());
tfEmail.setText(newVal.getEmail());
}
});
完整代码:
主班
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class TableViewSample extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage stage)
{
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
new Person("Ethan", "Williams", "ethan.williams@example.com"),
new Person("Emma", "Jones", "emma.jones@example.com"),
new Person("Michael", "Brown", "michael.brown@example.com")
);
TableView<Person> table = new TableView();
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(500);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
TableColumn firstNameCol = new TableColumn("First Name");
TableColumn lastNameCol = new TableColumn("Last Name");
TableColumn emailCol = new TableColumn("Email");
firstNameCol.setCellValueFactory(
new PropertyValueFactory("firstName")
);
lastNameCol.setCellValueFactory(
new PropertyValueFactory("lastName")
);
emailCol.setCellValueFactory(
new PropertyValueFactory("email")
);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
table.setItems(data);
TextField tfFirstName = new TextField();
TextField tfLastName = new TextField();
TextField tfEmail = new TextField();
table.getSelectionModel().selectedItemProperty().addListener((obs, oldVal, newVal) -> {
if (newVal != null) {
tfFirstName.setText(newVal.getFirstName());
tfLastName.setText(newVal.getLastName());
tfEmail.setText(newVal.getEmail());
}
});
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, new HBox(new Label("First Name: "), tfFirstName), new HBox(new Label("Last Name: "), tfLastName), new HBox(new Label("Email: "), tfEmail));
scene.setRoot(vbox);
stage.setScene(scene);
stage.show();
}
}
人员班
import javafx.beans.property.SimpleStringProperty;
public class Person
{
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
public Person(String fName, String lName, String email)
{
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}
public String getFirstName()
{
return firstName.get();
}
public void setFirstName(String fName)
{
firstName.set(fName);
}
public String getLastName()
{
return lastName.get();
}
public void setLastName(String fName)
{
lastName.set(fName);
}
public String getEmail()
{
return email.get();
}
public void setEmail(String fName)
{
email.set(fName);
}
}