Javafx右键单击编辑listView项和类似方法
我正在构建一个JavaFX待办事项列表,不知道如何继续。右键单击弹出菜单工作正常,但我不确定如何编辑/更改Javafx右键单击编辑listView项和类似方法,java,javafx,Java,Javafx,我正在构建一个JavaFX待办事项列表,不知道如何继续。右键单击弹出菜单工作正常,但我不确定如何编辑/更改列表视图的内容,而只是删除它们 LocalEvent e = a string somehow? 我尝试在Javafx中的右键单击弹出菜单中总共执行4项操作: 完成的步骤是在项目旁边放置复选标记并删除该项目 嵌套是从列表项创建嵌套列表(完全不知道如何创建) 编辑是使列表项可编辑并保存更改 移除工程:) 为此,我向fxml文件中添加了以下内容: 非常感谢您的帮助。突出的第一件事是:您的Lo
列表视图的内容,而只是删除它们
LocalEvent e = a string somehow?
我尝试在Javafx中的右键单击弹出菜单中总共执行4项操作:
完成的步骤是在项目旁边放置复选标记并删除该项目李>
嵌套是从列表项创建嵌套列表(完全不知道如何创建)
编辑是使列表项可编辑并保存更改李>
移除工程:)
为此,我向fxml文件中添加了以下内容:
非常感谢您的帮助。突出的第一件事是:您的LocalEvent
不应该实现ObservableList
,它只是一个数据持有者。您可能希望它还保存一个布尔值
,以查看它是否已完成:
public class LocalEvent {
private String description;
private LocalDate date;
private boolean completed = false;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public void setCompleted(boolean completed) {
this.completed= completed;
}
public LocalEvent(LocalDate date, String description) {
this.setDate(date);
this.setDescription(description);
}
@Override
public String toString() {
String base = "At " + this.getDate() + ": " + this.getDescription();
return completed ? "[✓] " + base : base;
}
}
我不知道什么是jfoenix
控件,但我假设它们与JavaFX标准控件足够接近。列表视图
应绑定到事件列表:
ObservableList<LocalEvent> list = FXCollections.observableArrayList();
ListView<LocalEvent> eventList = new ListView<>(list);
请注意,我们需要重置列表中的项,因为它不知道LocalEvent
实例的字段已更改。通过使用提取器,您可以使字段也报告更改,请参阅和这里的许多其他答案
编辑取决于列表视图所使用的单元格,我建议您在和其他问题中阅读。请注意,您可能希望能够分别编辑说明和日期,而不是整个toString
值,因此您必须提供一种机制,如返回日期选择器
嵌套取决于您希望它看起来像什么。您可能需要定义自己的单元格工厂,但只需添加一个缩进,通过<代码> \ \”\ /Cult> s,这将使它看起来嵌套。如果您真的需要在数据模型中嵌套,那么每个LocalEvent
都必须保存自己的observeListNesteEvents
此外,方法名称应以小写字母开头。什么是LocalEvent?我在API中没有看到它。更新了我的问题,使其包含LocalEvent。为什么要创建一个实现ObservableList
的类,然后创建该类的ObservableList
LocalEvent
不应该实现这一点,只要您在initialize()
方法中执行过一次,就可以删除所有的setItems()
,而您目前可能没有这样的方法。
package application;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javafx.beans.InvalidationListener;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
public class LocalEvent implements ObservableList<LocalEvent> {
private String description;
private LocalDate date;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public LocalEvent(LocalDate date, String description) {
this.setDate(date);
this.setDescription(description);
}
@Override
public String toString() {
return "At " + this.getDate() + ": " + this.getDescription();
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public Iterator<LocalEvent> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean add(LocalEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(Collection<? extends LocalEvent> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(int index, Collection<? extends LocalEvent> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
@Override
public LocalEvent get(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public LocalEvent set(int index, LocalEvent element) {
// TODO Auto-generated method stub
return null;
}
@Override
public void add(int index, LocalEvent element) {
// TODO Auto-generated method stub
}
@Override
public LocalEvent remove(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public int indexOf(Object o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int lastIndexOf(Object o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public ListIterator<LocalEvent> listIterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public ListIterator<LocalEvent> listIterator(int index) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<LocalEvent> subList(int fromIndex, int toIndex) {
// TODO Auto-generated method stub
return null;
}
@Override
public void addListener(InvalidationListener listener) {
// TODO Auto-generated method stub
}
@Override
public void removeListener(InvalidationListener listener) {
// TODO Auto-generated method stub
}
@Override
public void addListener(ListChangeListener<? super LocalEvent> listener) {
// TODO Auto-generated method stub
}
@Override
public void removeListener(ListChangeListener<? super LocalEvent> listener) {
// TODO Auto-generated method stub
}
@Override
public boolean addAll(LocalEvent... elements) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean setAll(LocalEvent... elements) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean setAll(Collection<? extends LocalEvent> col) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(LocalEvent... elements) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(LocalEvent... elements) {
// TODO Auto-generated method stub
return false;
}
@Override
public void remove(int from, int to) {
// TODO Auto-generated method stub
}
}
public class LocalEvent {
private String description;
private LocalDate date;
private boolean completed = false;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public void setCompleted(boolean completed) {
this.completed= completed;
}
public LocalEvent(LocalDate date, String description) {
this.setDate(date);
this.setDescription(description);
}
@Override
public String toString() {
String base = "At " + this.getDate() + ": " + this.getDescription();
return completed ? "[✓] " + base : base;
}
}
ObservableList<LocalEvent> list = FXCollections.observableArrayList();
ListView<LocalEvent> eventList = new ListView<>(list);
@FXML
void done(ActionEvent event) {
int index = eventList.getSelectionModel().getSelectedIndex();
LocalEvent localEvent = list.get(index);
localEvent.setCompleted(true);
list.set(index, localEvent);
}