我使用TableView开发JavaFX应用程序,我想在其中显示来自MySQL数据库的数据。当我单击按钮显示所有数据时,在表视图中只显示一行,其中包含MySQL的行ID,其他列仍为空。
控制器:
public class FXMLDocumentController implements Initializable {
private Label label;
@FXML
private TableView table;
@FXML
private TextField uname;
@FXML
private PasswordField pass;
@FXML
private Button add;
@FXML
private Button del;
@FXML
private TableColumn tc1;
@FXML
private TableColumn tc2;
@FXML
private TableColumn tc3;
private ObservableList<ShowData>data;
private Connection1 c;
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
c= new Connection1();
}
private void onClick(ActionEvent event) throws IOException {
Stage stage=new Stage();
Parent root = FXMLLoader.load(getClass().getResource("/Box/FXML.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
@FXML
private void onAdd(ActionEvent event) {
}
@FXML
private void onDelete(ActionEvent event) {
}
@FXML
private void onOpen(ActionEvent event) {
try {
Connection conn=c.Connect();
data=FXCollections.observableArrayList();
ResultSet rs=conn.createStatement().executeQuery("SELECT * FROM logs");
while (rs.next()) {
data.add(new ShowData(rs.getString(1), rs.getString(2), rs.getString(3)));
}
} catch (SQLException ex) {
System.err.println("Error"+ex);
}
tc1.setCellValueFactory(new PropertyValueFactory("id"));
tc2.setCellValueFactory(new PropertyValueFactory("username"));
tc3.setCellValueFactory(new PropertyValueFactory("msg"));
table.setItems(null);
table.setItems(data);
}
}
我没有收到任何错误。当我改为例如:
data.add(new ShowData(rs.getString(2), rs.getString(1), rs.getString(3)));
在第一列显示用户名,在其他两列中没有。 我的代码出了什么问题?
这是ShowData类 -
package javafxapplication7;
public class ShowData {
private final StringProperty id;
private final StringProperty username;
private final StringProperty msg;
public ShowData(String id, String username, String msg) {
this.id = new SimpleStringProperty(id);
this.username = new SimpleStringProperty(username);
this.msg = new SimpleStringProperty(msg);
}
public String getId() {
return id.get();
}
public String getuname() {
return username.get();
}
public String getpass() {
return msg.get();
}
public void setId(String value) {
id.setValue(value);
}
public void setUname(String value) {
username.setValue(value);
}
public void setPass(String value) {
msg.setValue(value);
}
public StringProperty idproper(){
return id;
}
public StringProperty unameproper(){
return username;
}
public StringProperty passproper(){
return msg;
}
}