I am building a JavaFX todo list and am not sure how to continue. The right click popup menu works fine but I am not sure how to edit/change the contents of the ListView
other than just removing them.
LocalEvent e = a string somehow?
I am trying to do 4 total things in a right click popup menu in Javafx:
- Done is to place a check mark next to and strikeout the item.
- Nest is to create a nested list from a list item (no idea at all how).
- Edit is to make the list item editable and save the chages.
- Remove works :)
I have done this by adding the following to the fxml file:
<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>`
Here is my Controller.java file:
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
is a java class file as follows:
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
}
}
Any help at all appreciated.