java - Javafx右键编辑listView项及类似方法
问题描述
我正在构建一个 JavaFX 待办事项列表,但不确定如何继续。右键单击弹出菜单工作正常,但我不知道如何编辑/更改其他内容,ListView
而不仅仅是删除它们。
LocalEvent e = a string somehow?
我试图在 Javafx 的右键弹出菜单中做 4 件事:
- 完成的是在项目旁边放置一个复选标记并删除该项目。
- Nest 是从列表项创建嵌套列表(完全不知道如何)。
- 编辑是使列表项可编辑并保存更改。
- 删除作品:)
我通过将以下内容添加到 fxml 文件来完成此操作:
<JFXListView fx:id="eventList" editable="true" layoutX="24.0" layoutY="106.0" prefHeight="354.0" prefWidth="939.0">
<contextMenu>
<ContextMenu>
<items>
<MenuItem fx:id="popUp" mnemonicParsing="false" onAction="#Done" text="Done" />
<MenuItem fx:id="popUp3" mnemonicParsing="false" onAction="#Remove" text="Remove" />
<MenuItem fx:id="popUp1" mnemonicParsing="false" onAction="#Nest" text="Nest" />
<MenuItem fx:id="popUp2" mnemonicParsing="false" onAction="#Edit" text="Edit" />
</items>
</ContextMenu>
</contextMenu></JFXListView>`
这是我的 Controller.java 文件:
package application;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXListView;
import com.jfoenix.controls.JFXTextField;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.DatePicker;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseEvent;
public class Controller implements Initializable{
@Override
public void initialize(URL url, ResourceBundle rb) {
datePicker.setValue(LocalDate.now());
eventList.setExpanded(true);
eventList.depthProperty().set(1);
}
@FXML
private MenuItem popUp;
@FXML
private JFXTextField textBox;
@FXML
private JFXListView<LocalEvent> eventList;
ObservableList<LocalEvent> list = FXCollections.observableArrayList();
@FXML
private JFXButton AddButton;
@FXML
private DatePicker datePicker;
@FXML
void Submit(ActionEvent event) {
list.add(new LocalEvent(datePicker.getValue(), textBox.getText()));
eventList.setItems(list);
datePicker.setValue(LocalDate.now());
textBox.setText("");
}
@FXML
public void onEnter(ActionEvent event){
list.add(new LocalEvent(datePicker.getValue(), textBox.getText()));
eventList.setItems(list);
datePicker.setValue(LocalDate.now());
textBox.setText("");
}
@FXML
void Done(ActionEvent event) {
int index = eventList.getSelectionModel().getSelectedIndex();
String str = list.get(index).toString();
str = "[✓] " + str;
LocalEvent e = null; // <- how to put a string in here?
list.set(index, e);
eventList.setItems(list);
//eventList.setItems(list.set(index, element));
}
@FXML
void Remove(ActionEvent event) {
// remove selected task
list.remove(eventList.getSelectionModel().getSelectedIndex());
}
@FXML
void Nest(ActionEvent event) {
System.out.println("How the hell do I do that? lol");
// check for nested level
// create a nested list item
}
@FXML
void Edit(ActionEvent e) {
System.out.println("Edit selection");
eventList.setEditable(true);
int index = eventList.getSelectionModel().getSelectedIndex();
eventList.scrollTo(index);
eventList.layout();
eventList.edit(index);
eventList.layout();
}
}
LocalEvent
是一个java类文件如下:
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
}
}
任何帮助都表示赞赏。
解决方案
评论中提到的第一件事是:你LocalEvent
不应该实现ObservableList
,它只是一个数据持有者。您可能还希望它还持有一个boolean
以查看它是否已完成:
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 标准控件足够接近。ListView
应该绑定到事件列表:
ObservableList<LocalEvent> list = FXCollections.observableArrayList();
ListView<LocalEvent> eventList = new ListView<>(list);
(我还将列表重命名为更有意义的名称。)
现在任何更改list
都将反映在中,eventList
因此您无需触摸eventList
。这使您的done
方法看起来像:
@FXML
void done(ActionEvent event) {
int index = eventList.getSelectionModel().getSelectedIndex();
LocalEvent localEvent = list.get(index);
localEvent.setCompleted(true);
list.set(index, localEvent);
}
请注意,我们需要重置列表中的项目,因为它不知道LocalEvent
实例的字段已更改。您可以使用提取器使字段也报告更改,请参见此处以及此处的许多其他答案。
编辑取决于 使用的单元格ListView
,我建议在此处阅读文档和其他问题。请注意,您可能希望能够单独编辑描述和日期,而不是整个toString
值,因此您必须为此提供一种机制,例如恢复DatePicker
.
嵌套取决于您希望它的外观。您可能需要定义自己的单元工厂,但考虑只添加一个缩进,通过"\t"
s,这将使它看起来嵌套。如果你真的需要在数据模型中嵌套,那么每个LocalEvent
都必须拥有自己的ObservableList<LocalEvent> nestedEvents
.
此外,方法名称应以小写字母开头。
推荐阅读
- java - 保持加载 keras 以进行评估
- java - 有没有办法在 Java 中将布尔类型赋予泛型?
- amcharts - 传说断了
- atmega - 我无法对我的 uC 进行编程。Avrdude 抛出我不明白的错误
- android - 错误:无法解决:com.android.support:support-v4:23.3.0
- vba - 有没有办法使用组合框数据 (VBA) 更改 Word 文档的文件名和电子邮件主题?
- google-app-engine - 使用 gcloud CLI 将 java 11 应用程序部署到 GAE 标准
- ios - 覆盖 UISearchController 中的搜索栏
- javascript - 使用对象作为查找表:第一次调用后值不更新
- php - CodeIgniter Active Records 插入中的数组索引更改