简体   繁体   中英

JavaFX pulling value from nth column of every row in a TableView

Sorry about the title, I couldn't think of anything better for it.

The following table is what I'm working with currently 在此处输入图片说明

Below is my code for creating the columns and and populating three of the 4 columns automatically from an ObservableList. The 4th column, # ordered, is editable by the user. My goal is to iterate through every row and update the item of that row with the value that the user has entered for it. I have had success iterating through the rows and getting the Item objects from the row but I have not been able to find a proper way to get the order value for that row.

TableView tableView = new TableView();
TableColumn itemCodeColumn = new TableColumn("Item Code");
TableColumn itemDescriptionColumn = new TableColumn("Description");
TableColumn numAvailableColumn = new TableColumn("# in Stock");
TableColumn numOrderColumn = new TableColumn("# Ordered");

itemCodeColumn.setCellValueFactory(new PropertyValueFactory<Item,Integer>("itemCode"));
itemDescriptionColumn.setCellValueFactory(new PropertyValueFactory<Item,String>("itemDesc"));
numAvailableColumn.setCellValueFactory(new PropertyValueFactory<Item,Integer>("quantity"));

I have been able to access all items found in my tableView using the following

for(Object it : tableView.getItems()){
    Item it2 = (Item) it;
}

however this only gives me a reference to the object to update the ordered value, I still need a way to pull the actual order number from the table. To do this I used the following piece of code I found online. Note that itemGUIList is an ObservableList that is used to populate the table.

for(int c=0;c<itemGUIList.size();c++){
     Object o;
     o = tableView.getColumns().get(c).getCellObservableValue(0).getValue();
}

The problem that I ran into here was that the method getCellObservableValue() gave me the error "cannot find symbol". Searching for this did not give me any results and this piece of code seemed to be working for everyone else.

If anyone is able to point out either what I am missing, or if there is better way to solve this problem I would greatly appreciate it. If you need me to post anymore of my code then please let me know and I will. Thank you ahead of time.

In your declaration of the TableView , you're using the raw type.

This results in the type of tableView.getColumns() being ObservableList and therefore the type of tableView.getColumns().get(c) being Object , which does not contain a getCellObservableValue method.

You should add a type parameter to the declaration, even if you're using ? to fix this issue:

TableView<Item> tableView = new TableView<>();

or

TableView<?> tableView = new TableView<>();

Note:

It's preferable not to use the raw type, if possible, since this allows the compiler to do some type checking and also you avoid having to write some casts in your code, eg

for(Object it : tableView.getItems()) {
    Item it2 = (Item) it;
}

could simply be changed to

for(Item it2 : tableView.getItems()) {
}

Actually using the TableColumn to retrieve the number of orders shouldn't be necessary though. You could simply retrieve it from the item itself (or the other location where this data is stored), eg:

Item item = tableView.getItems().get(0);
int ordered = item.getOrdered();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM