项目未显示在 JavaFX 的 TableView 中
Items not showing in TableView in JavaFX
我在 FXML
中有一个 Table View
pinTable 和两个 Table Columns
,即 latColumn、longColumn。我已按照以下方法填充 table :
https://docs.oracle.com/javafx/2/ui_controls/table-view.htm
片段如下:
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");
}
}
}
我有一个 PinList class 如下:
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;
}
}
所有这些似乎都很好。但是,当我点击 Add
按钮时,没有任何反应。 table 中没有创建任何行,addButtonClicked
事件处理程序中的 println
不执行,或者执行时数据源中没有任何数据。任何帮助将不胜感激。
问题是您将 Application
class 与您的 FXML 控制器 class 混淆了。即使此 FXML class 扩展了 Application
,重写的 start()
方法也不会在您的代码中的任何地方被调用。放一些 println
s 来验证这一点。 FXML 控制器 class 可以选择有一个 initialize()
(另外可以实现 Initializable 但不是强制的)。这个方法会在FXMLLoader
加载fxml文件后被它调用。所以正确的代码应该是:
public void initialize( URL url, ResourceBundle rb ) {
initTable();
}
并删除 start() 方法并从应用程序中删除扩展。
在你的initTable()
方法中你做
pinTable.setItems(dataSource);
所以现在 pinTable
在其 items
属性 内部持有的列表与 dataSource
完全相同(它们在引用上是相同的)。
现在在您的事件处理程序方法中执行
dataSource.add(new PinList(...));
向 dataSource
添加一个新项目(与 table 的 items
相同的列表)
然后
pinTable.getItems().clear();
从 table 的 items
列表中删除所有元素。所以 table 的 items
列表现在是空的(没有元素)。当然,由于这是与 dataSource
完全相同的列表,您还删除了 dataSource
中的所有项目:它与 pinTable.getItems()
.[=29= 相同(现在为空)列表]
现在你可以
pinTable.getItems().addAll(dataSource);
将 dataSource
中的所有项目(有 none)复制到 pinTable.getItems()
(这是同一个列表)中。所以这实际上复制了列表中的所有项目,但是由于你清空了列表,你仍然有一个空列表。
只需删除行
pinTable.getItems().clear();
pinTable.getItems().addAll(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()));
for(PinList p: dataSource)
{
System.out.print(p.getLatPin() + " " + p.getLongPin() + " " + p.getDescriptionPin() + "\n");
}
}
}
我在 FXML
中有一个 Table View
pinTable 和两个 Table Columns
,即 latColumn、longColumn。我已按照以下方法填充 table :
https://docs.oracle.com/javafx/2/ui_controls/table-view.htm
片段如下:
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");
}
}
}
我有一个 PinList class 如下:
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;
}
}
所有这些似乎都很好。但是,当我点击 Add
按钮时,没有任何反应。 table 中没有创建任何行,addButtonClicked
事件处理程序中的 println
不执行,或者执行时数据源中没有任何数据。任何帮助将不胜感激。
问题是您将 Application
class 与您的 FXML 控制器 class 混淆了。即使此 FXML class 扩展了 Application
,重写的 start()
方法也不会在您的代码中的任何地方被调用。放一些 println
s 来验证这一点。 FXML 控制器 class 可以选择有一个 initialize()
(另外可以实现 Initializable 但不是强制的)。这个方法会在FXMLLoader
加载fxml文件后被它调用。所以正确的代码应该是:
public void initialize( URL url, ResourceBundle rb ) {
initTable();
}
并删除 start() 方法并从应用程序中删除扩展。
在你的initTable()
方法中你做
pinTable.setItems(dataSource);
所以现在 pinTable
在其 items
属性 内部持有的列表与 dataSource
完全相同(它们在引用上是相同的)。
现在在您的事件处理程序方法中执行
dataSource.add(new PinList(...));
向 dataSource
添加一个新项目(与 table 的 items
相同的列表)
然后
pinTable.getItems().clear();
从 table 的 items
列表中删除所有元素。所以 table 的 items
列表现在是空的(没有元素)。当然,由于这是与 dataSource
完全相同的列表,您还删除了 dataSource
中的所有项目:它与 pinTable.getItems()
.[=29= 相同(现在为空)列表]
现在你可以
pinTable.getItems().addAll(dataSource);
将 dataSource
中的所有项目(有 none)复制到 pinTable.getItems()
(这是同一个列表)中。所以这实际上复制了列表中的所有项目,但是由于你清空了列表,你仍然有一个空列表。
只需删除行
pinTable.getItems().clear();
pinTable.getItems().addAll(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()));
for(PinList p: dataSource)
{
System.out.print(p.getLatPin() + " " + p.getLongPin() + " " + p.getDescriptionPin() + "\n");
}
}
}