Reputation: 547
I have a Table View
pinTable in FXML
and two Table Columns
i.e. latColumn, longColumn. I have followed the following method to populate the table :
https://docs.oracle.com/javafx/2/ui_controls/table-view.htm
Snippets are as follows:
FXML Controller Class:
@FXML
public TableView pinTable;
@FXML
public TableColumn latColumn, longColumn;
public final ObservableList<PinList> dataSource = new FXMLCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb)
{
initTable();
}
private void initTable()
{
latColumn.setCellValueFactory(new PropertyValueFactory<PinList,String>("latPin"));
longColumn.setCellValueFactory(new PropertyValueFactory<PinList,String>("longPin"));
pinTable.setItems(dataSource);
}
@FXML
private void addButtonClicked(MouseEvent event)
{
if(latText.getText().equals(""))
{
System.out.println("Lat Empty");
}
else if(longText.getText().equals(""))
{
System.out.println("Long Empty");
}
else
{
latVal = Double.parseDouble(latText.getText());
longVal = Double.parseDouble(longText.getText());
dataSource.add(new PinList(latText.getText(),longText.getText(),descriptionText.getText()));
pinTable.getItems().clear();
pinTable.getItems().addAll(dataSource);
for(PinList p: dataSource)
{
System.out.print(p.getLatPin() + " " + p.getLongPin() + " " + p.getDescriptionPin() + "\n");
}
}
}
I have a PinList class which is as follows:
public class PinList
{
private final SimpleStringProperty latPin;
private final SimpleStringProperty longPin;
private String descriptionPin;
public PinList(String _latPin, String _longPin, String _descriptionPin)
{
this.latPin = new SimpleStringProperty(_latPin);
this.longPin = new SimpleStringProperty(_longPin);
this.descriptionPin = _descriptionPin;
}
public String getLatPin()
{
return latPin.getValue();
}
public StringProperty latPinProperty()
{
return latPin;
}
public String getLongPin()
{
return longPin.getValue();
}
public StringProperty longPinProperty()
{
return longPin;
}
public String getDescriptionPin()
{
return descriptionPin;
}
}
All these seem to be fine. But, when I click Add
button, nothing happens. No row is created in the table and the println
inside the addButtonClicked
event handler doesn't execute, or it is executed with no data in the dataSource whatsoever. Any help will be appreciated.
Upvotes: 0
Views: 3185
Reputation: 209225
In your initTable()
method you do
pinTable.setItems(dataSource);
So now the list held internally by pinTable
in its items
property is the very same list as dataSource
(they are the identical by reference).
Now in your event handler method you do
dataSource.add(new PinList(...));
which adds a new item to dataSource
(which is the same list as the table's items
)
and then
pinTable.getItems().clear();
which removes all elements from the table's items
list. So the table's items
list is now empty (has no elements). Of course, since this is the very same list as dataSource
, you have also removed all items from dataSource
: it is the same (now empty) list as pinTable.getItems()
.
and now you do
pinTable.getItems().addAll(dataSource);
which copies all the items in dataSource
(there are none) into pinTable.getItems()
(which is the same list). So this actually duplicates all items in the list, but since you emptied the list, you still have an empty list.
Just remove the lines
pinTable.getItems().clear();
pinTable.getItems().addAll(dataSource);
All you want here is:
@FXML
private void addButtonClicked(MouseEvent event)
{
if(latText.getText().equals(""))
{
System.out.println("Lat Empty");
}
else if(longText.getText().equals(""))
{
System.out.println("Long Empty");
}
else
{
latVal = Double.parseDouble(latText.getText());
longVal = Double.parseDouble(longText.getText());
dataSource.add(new PinList(latText.getText(),longText.getText(),descriptionText.getText()));
for(PinList p: dataSource)
{
System.out.print(p.getLatPin() + " " + p.getLongPin() + " " + p.getDescriptionPin() + "\n");
}
}
}
Upvotes: 1
Reputation: 49185
The problem is you confused the Application
class with your FXML controller class. Even though this FXML class extends Application
the overridden start()
method is not being invoked anywhere in your code. Put some println
s to verify this. The FXML controller class can optionally have a initialize()
(and additionally can implement Initializable but not mandatory). This method will be invoked by FXMLLoader
after the fxml file is loaded by it. So the correct code should be:
public void initialize( URL url, ResourceBundle rb ) {
initTable();
}
and delete start() method and remove extending from Application.
Upvotes: 2