我使用JavaFX的工作,并试图解决以下问题:JavaFX的,如何与多个tableviews和他们的项目选择工作
我得到了2个TableViews(A和B)(每个只有一个列)。如果我选择一个tableview项目,应该有信息在其他地方收听,关于选定的对象。
当我选择tableview A并单击一个项目时,显示信息并选择该项目。没关系。 现在当我尝试选择tableview B项目时,信息不会显示。它只有当我第一次点击tableview A区域(不是点击A的直接项目)然后点击tableview B项目时才起作用。这可能是什么原因?
我尝试使用tableview.getSelectionModel().clearSelection()
并添加鼠标点击侦听器以取消选择其他tableview。没有工作。
也许有一种简单的方法来说“以前忘了所有选择”?谢谢!
下面的代码显示信息时,点击是:
....
initTableConfiguration();
categoryTableView.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showBoardObjectDetails(newValue));
noteTableView.getSelectionModel().selectedItemProperty().addListener(
(observable, oldValue, newValue) -> showBoardObjectDetails(newValue));}
private void initTableConfiguration() {
categoryColumn.setCellValueFactory(
cellData -> cellData.getValue().titleProperty());
noteColumn.setCellValueFactory(
cellData -> cellData.getValue().titleProperty());
}
而且对于点击监听器:
categoryTableView.setOnMousePressed(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent me){
noteTableView.getSelectionModel().clearSelection();
}
});
noteTableView.setOnMousePressed(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent me){
categoryTableView.getSelectionModel().clearSelection();
}
});
显示的信息:
public void showBoardObjectDetails(Object object){
if(object != null){
Category category;
Note note;
if(object.getClass()==Category.class){
category = (Category) object;
titleLabel.setText(category.getTitle());
dateLabel.setVisible(false);
staticDateLabel.setVisible(false);
contentTextArea.setVisible(false);
staticContentLabel.setVisible(false);
}else if(object.getClass()==Note.class){
note = (Note) object;
staticContentLabel.setVisible(true);
staticDateLabel.setVisible(true);
dateLabel.setVisible(true);
contentTextArea.setVisible(true);
titleLabel.setText(note.getTitle());
dateLabel.setText(note.getDate().toString());
contentTextArea.setText(note.getText());
}
}else{
titleLabel.setText("");
dateLabel.setText("");
contentTextArea.setText("");
}
}
您可以创建并发布[MCVE](http://stackoverflow.com/help/mcve)吗?在没有看到导致它的代码的情况下,知道什么可能会出错是几乎不可能的。 –
@James_D好吧,现在它应该足够的上下文 – qecce
他们是tableviews而不是listviews对不对?纠正你的问题。 –