I develop an JavaFX application with TableView, where I want to show data from MySQL database. When I click the button to show all the data, in the table view show only one column with id of the row in MySQL, and other columns still empty.
The controller:
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);
}
}
I don't get any errors. When I change for example to this:
data.add(new ShowData(rs.getString(2), rs.getString(1), rs.getString(3)));
In first column is show the username and in the other two columns nothing. What is wrong with my code ?
And this is ShowData class-
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;
}
}