0

I am trying to do is get the data from the current selected row on a button click, and return that. However what it returns now is something that seems to be in the format of packagename+ classname+ randomnumbers and letters.

simple.UserDetails@3f59ec1e
simple.UserDetails@210337a8

Random numbers and letters do change when another row is selected. I tried to use thread as example.
Getting selected item from a JavaFX TableView. I would like an answer on how I can achieve the result that I desire. And, if possible be able to save the data in variables too.

Method I use:

  @FXML
    public void getRow() {
        UserDetails row = tableview.getSelectionModel().getSelectedItem();
        System.out.println(row);
    }

The controller class.

package simple;

import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
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.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 *
 * @author admin
 */
public class FXMLUserController implements Initializable {

    @FXML
    private TableView<UserDetails> tableview;
    @FXML
    private TableColumn<UserDetails, String> columnId;
    @FXML
    private TableColumn<UserDetails, String> columnType;
    @FXML
    private TableColumn<UserDetails, String> columnKleur;
    @FXML
    private TableColumn<UserDetails, String> columnLuchthaven;
    @FXML
    private TableColumn<UserDetails, String> columnKenmerken;
    @FXML
    private TableColumn<UserDetails, String> columnStatus;
    @FXML
    private TableColumn<UserDetails, String> columnDatum;
    @FXML
    private Button btnLoad;
    //declare observable list for database data
    private ObservableList<UserDetails> data;
    private DbConnection dc;
    @FXML
    private Button editRow;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        dc = new DbConnection();
        loadDataFromDatabase();
    }

    @FXML
    public void loadDataFromDatabase() {
        try {
            Connection conn = dc.Connect();
            data = FXCollections.observableArrayList();
            // Execute query and store result in a resultset
            ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM gevonden_bagage");
            while (rs.next()) {
                //get strings
                data.add(new UserDetails(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),
                        rs.getString(6), rs.getString(7)));
            }

        } catch (SQLException ex) {
            System.err.println("Error" + ex);
        }

        //Set cell values to tableview.
        tableview.setEditable(true);
        columnId.setCellValueFactory(new PropertyValueFactory<>("id"));
        columnType.setCellValueFactory(new PropertyValueFactory<>("type"));
        columnKleur.setCellValueFactory(new PropertyValueFactory<>("kleur"));
        columnLuchthaven.setCellValueFactory(new PropertyValueFactory<>("luchthaven"));
        columnKenmerken.setCellValueFactory(new PropertyValueFactory<>("kenmerken"));
        columnStatus.setCellValueFactory(new PropertyValueFactory<>("status"));
        columnDatum.setCellValueFactory(new PropertyValueFactory<>("datum"));

        tableview.setItems(null);
        tableview.setItems(data);

    }

    @FXML
    public void getRow() {
        UserDetails row = tableview.getSelectionModel().getSelectedItem();
        System.out.println(row);
    }
}

Model class

package simple;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/**
 *
 * @author admin
 */
public class UserDetails {

    private final StringProperty id;
    private final StringProperty type;
    private final StringProperty kleur;
    private final StringProperty luchthaven;
    private final StringProperty kenmerken;
    private final StringProperty status;
    private final StringProperty datum;

    //Default constructor
    public UserDetails(String id, String type, String kleur, String luchthaven, String kenmerken, String status, String datum) {
        this.id = new SimpleStringProperty(id);
        this.type = new SimpleStringProperty(type);
        this.kleur = new SimpleStringProperty(kleur);
        this.luchthaven = new SimpleStringProperty(luchthaven);
        this.kenmerken = new SimpleStringProperty(kenmerken);
        this.status = new SimpleStringProperty(status);
        this.datum = new SimpleStringProperty(datum);

    }

    //getters
    public String getId() {
        return id.get();
    }

    public String getType() {
        return type.get();
    }

    public String getKleur() {
        return kleur.get();
    }

    public String getLuchthaven() {
        return luchthaven.get();
    }

    public String getKenmerken() {
        return kenmerken.get();
    }

    public String getStatus() {
        return status.get();
    }

    public String getDatum() {
        return datum.get();
    }

    //setters
    public void setId(String value) {
        id.set(value);
    }

    public void setType(String value) {
        type.set(value);
    }

    public void setKleur(String value) {
        kleur.set(value);
    }

    public void setLuchthaven(String value) {
        luchthaven.set(value);
    }

    public void setKenmerken(String value) {
        kenmerken.set(value);
    }

    public void setStatus(String value) {
        status.set(value);
    }

    public void setDatum(String value) {
        datum.set(value);
    }

    //property values
    public StringProperty idProperty() {
        return id;
    }

    public StringProperty typeProperty() {
        return type;
    }

    public StringProperty kleurProperty() {
        return kleur;
    }

    public StringProperty luchthavenProperty() {
        return luchthaven;
    }

    public StringProperty kenmerkenProperty() {
        return kenmerken;
    }

    public StringProperty statusProperty() {
        return status;
    }

    public StringProperty datumProperty() {
        return datum;
    }
}
Community
  • 1
  • 1
M.verdegaal
  • 362
  • 1
  • 4
  • 14

1 Answers1

0

You need to say something like row.someMethodorVariableInUserDetails What ever you are trying to return is in your UserDetails class

Look at this example from here

Person person = taview.getSelectionModel().getSelectedItem();
System.out.println(person.getName()); 

In this example they have to say person.somemethod(). In this case person.getName();

Community
  • 1
  • 1
SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • Another thing that you probably could do is add at toString method to your UserDetails class. Then you could say System.out.println(row.toString()); I haven't tested this, but it's worth trying. – SedJ601 Dec 06 '16 at 14:43
  • 1
    Thank you for the answer. I was able to make it work by using all of the getters from my UserDetails class. As mentioned in your example above.. – M.verdegaal Dec 06 '16 at 16:33