-2

Ok, so I know this is a common problem that has been posted about a lot but as much as I try to follow the advice given, my TableView till displays no data... I'll reduce my object a bit to keep things as short as possible. Here is my Object:

public SimpleStringProperty itemCode, itemName; 

public ResourceItem(String code, String name) {
    this.itemCode = new SimpleStringProperty(code);
    this.itemName = new SimpleStringProperty(name);
}

public String getItemCode() {
    return itemCode.get();
}

public void setItemCode(String code) {
    itemCode.set(code);
}

public SimpleStringProperty itemCodeProperty() {
    return itemCode;
}

public SimpleStringProperty itemNameProperty() {
    return itemName;
}

public String getItemName() {
    return itemName.get();
}

public void setItemName(String name) {
    itemName.set(name);
}

And here is where I create the TableColumns:

TableColumn<ResourceItem, String> code = new TableColumn("Item Code"); 
code.setCellValueFactory(new PropertyValueFactory("itemCode"));
TableColumn<ResourceItem, String> code = new TableColumn("Item Name"); 
name.setCellValueFactory(new PropertyValueFactory("itemName"));

I add Resource Items to the ObservableList through a for loop and set my items of the TableView to that list:

ObservableList<ResourceItem> data = FXCollections.observableArrayList();
....
itemsInDB.setItems(data);
itemsInDB.getColumns().addAll(code, name);

And then nothing is added. Can someone help me out please?

EDIT: Here is a testable version. It does require you set up a database called ims, a table called im_resoureitem_br with two columns: IMItemCode Varchar(4) and IMItemName Varchar(30).

public class TableViewTest extends Application {

final String DRIVER = "com.mysql.jdbc.Driver";
String urlHead = "jdbc:mysql://localhost/ims";

final String USER = "root";
final String PASS = "";

Connection connection;
Statement statement;

private TableView<ResourceItem> table = new TableView<ResourceItem>(); //creates table to hold Course objects
private final ObservableList<ResourceItem> data
        = FXCollections.observableArrayList();

@Override
public void start(Stage stage) throws ClassNotFoundException {
    Scene scene = new Scene(new Group());
    stage.setTitle("Fall 2015 Schedule"); //title of stage, appears at top bar
    stage.setWidth(700);
    stage.setHeight(500);

    final Label label = new Label("Brenna Morss-Fish Fall Schedule 2015");

    table.setEditable(true);

    table.setItems(data); //sets rows of table as data from course arraylist
    TableColumn<ResourceItem, String> code = new TableColumn<ResourceItem, String>("Code:");//creates first column
    code.setMinWidth(100);
    code.setCellValueFactory(
            new PropertyValueFactory("itemCode"));
    TableColumn<ResourceItem, String> name = new TableColumn<ResourceItem, String>("Name:");//creates first column
    name.setMinWidth(100);
    name.setCellValueFactory(
            new PropertyValueFactory("itemName")); //defines what column holds according to name field of Course class

    String query = "select * from ims.im_resourceItem_br; ";
    ArrayList<String[]> items = new ArrayList<String[]>();

    TableView<ResourceItem> itemsInDB = new TableView();
    items = getQueryResult(query);
    //itemsInDB.setEditable(false);
    ResourceItem item = new ResourceItem("", "");
    ObservableList<ResourceItem> data = FXCollections.observableArrayList();
    data.removeAll(data);
    //System.out.println(items.get(0).toString()); 
    for (int i = 0; i < items.size(); i++) {
        item.setItemCode(items.get(i)[1]);
        item.setItemName(items.get(i)[2]);
        data.add(item);
    }

    code.setCellValueFactory(new PropertyValueFactory("itemCode"));
    name.setCellValueFactory(new PropertyValueFactory("itemName"));

    itemsInDB.setItems(data);

    System.out.println(itemsInDB.getItems());

    itemsInDB.getColumns().addAll(code, name);

    table.getColumns().addAll(code, name);
    //adds previously defined columns to the table in the order they will appear
    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.getChildren().addAll(label, table); //adds label and course table to VBox layout container

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    stage.setScene(scene); //adds scene to the stage
    stage.show(); //displays stage
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

public ArrayList getQueryResult(String stmt) throws ClassNotFoundException {
    String results = "";
    ResultSet resultSet = null;
    String row = "";
    ArrayList<String[]> list = new ArrayList<String[]>();
    try {

        Class.forName(DRIVER);
        connection = DriverManager.getConnection(urlHead, USER, PASS);
        statement = connection.createStatement();
        resultSet = statement.executeQuery(stmt);

        int columnCount = resultSet.getMetaData().getColumnCount();

        while (resultSet.next()) {
            String delims = "[%]";
            row = "";
            for (int i = 1; i <= columnCount; i++) {
                row += resultSet.getString(i) + "%";
            }
            String[] array = row.split(delims);

            list.add(array);
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return list;
}

public static class ResourceItem {

    public SimpleStringProperty itemCode, itemName;

    public ResourceItem(String code, String name) {
        this.itemCode = new SimpleStringProperty(code);
        this.itemName = new SimpleStringProperty(name);
    }

    public String getItemCode() {
        return itemCode.get();
    }

    public void setItemCode(String code) {
        itemCode.set(code);
    }

    public SimpleStringProperty itemCodeProperty() {
        return itemCode;
    }

    public SimpleStringProperty itemNameProperty() {

        return itemName;
    }

    public String getItemName() {
        return itemName.get();
    }

    public void setItemName(String name) {
        itemName.set(name);
    }

public String toString() {
    String print = itemCode + " " + itemName + " ";

    return print; 
}
}

}

  • I haven't used your syntax but I believe if you change this: this.itemCode = new SimpleStringProperty(code); to this: this.itemCode = new SimpleStringProperty(this, "itemCode", code, ); it might work. It's looking it up by the property name that you haven't set – purring pigeon Apr 05 '16 at 21:56
  • @purringpigeon No, it's looking it up by reflection, using the method name in the model class. – James_D Apr 05 '16 at 21:59
  • @Brennasyril I can't see anything wrong here. Can you expand the code to a [MCVE]. I think the problem lies elsewhere in your code. – James_D Apr 05 '16 at 22:00
  • Oh - ok thanks - didn't know it looked it up that way. Thanks. – purring pigeon Apr 05 '16 at 22:01
  • @James_D I have edited my post. It reads in the info from the database and stores it in the ObservableList fine but does not display the content. – Brennasyril Apr 05 '16 at 22:23
  • 1
    Didn't you ask this question two days ago and were given the same advice (from James)? I even answered that question with a minimal example.. but you deleted that question for some reason. – JohnRW Apr 05 '16 at 22:36
  • That's not really "minimal", is it? Can you get rid of all the (presumably completely irrelevant) database code, and create a minimal example? I'm quite happy to help, but I don't want to read through a ton of irrelevant code that I can't actually run to help you out. – James_D Apr 05 '16 at 22:36
  • @JohnRW no I did not. I didn't even start this project until yesterday morning. – Brennasyril Apr 05 '16 at 22:44
  • In that case I am sorry, its just very very very similar to a deleted question from two days ago :) – JohnRW Apr 05 '16 at 23:05
  • @JohnRW no problem! Like I said, I've found a bunch of similar questions but none were specific to my problem so they didn't help. I finally figured it out so all is good! – Brennasyril Apr 05 '16 at 23:56

1 Answers1

0

You have created two tables: one called table, which you display in the UI when you add it to vbox, and another called itemsInDB, which you populate but never display. Since the table you do display has no data in it, and the table you populate is never displayed, you never see the data.

You have another logical error when you populate the observable list: you add the same item over and over to the list, and update its properties each time. So if it were displayed you would see the same item repeatedly in the table.

There may be other errors I haven't noticed, but since you haven't created a MCVE, I can't actually run it and test it.

James_D
  • 201,275
  • 16
  • 291
  • 322