1

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;
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
John Terry JQ
  • 41
  • 2
  • 5

1 Answers1

1

The methods in the ShowData class do not meet the naming conventions as required by PropertyValueFactory.

  • Getters include the name of the property with a uppercase first letter.
  • property methods are named <property>Property(), i.e. in your case:
public String getUname() {
    return username.get();
}

public String getPass() {
    return msg.get();
}

public StringProperty idProperty(){
    return id;
}

public StringProperty unameProperty(){
    return username;
}

public StringProperty passProperty(){
    return msg;
}

This is why PropertyValueFactory cannot identify the correct methods to use and this is why the cells remain empty.

Graham
  • 7,431
  • 18
  • 59
  • 84
fabian
  • 80,457
  • 12
  • 86
  • 114