I am implementing a custom cellFactory on a TreeView component and I would like to call the startEdit() function on a certain tree item. According to the javaFX documentation, the following method should work:
// "Item Title" is the title of the item to be edited
tree.edit(new TreeItem<String>("Item Title"));
But nothing happens when I use it. I am only able to start the editing mode by double clicking the tree item itself, and I would like to be able to call this function automatically when the user creates a new treeItem:
// Create a new TreeItem
TreeItem<String> item = new TreeItem<String> ("New Item");
// Insert it to the current tree
tree.getRoot().getChildren().add(item);
// Enter edit mode
tree.edit(item);
Related
I have a project with a TreeView and I want that if I select a TreeItem and that I type DELETE the file in relation with it is automatically delete on the disk. But it doesn't work and I don't find my answer just by searching on Internet. This is my function (listeArbres is a TreeView) :
private void recupereFichierProjet(File repertoire, FileFilter filtre) {
File[] fichiers = repertoire.listFiles(filtre);
TreeItem<String> rootItem = new TreeItem<String>("Workspace");
rootItem.setExpanded(true);
for (File fichier : fichiers) {
Projet projet = Projet.charge(fichier);
TreeItem<String> item = new TreeItem<String>(fichier.getName());
item.addEventHandler(KeyEvent.KEY_TYPED, event -> {
if (event.getCode() == KeyCode.DELETE) {
System.out.println("la");
Projet.supprime(
new File("./workspace/" + listeArbres.getSelectionModel().getSelectedItem().getValue()));
initialiseTreeView();
}
});
rootItem.getChildren().add(item);
for (Arbre arbre : projet.getArbreDuProjet()) {
TreeItem<String> itemBis = new TreeItem<String>(arbre.getEntete().getNomFonction());
item.getChildren().add(itemBis);
}
}
listeArbres.setRoot(rootItem);
listeArbres.setVisible(true);
}
I think I understand that addEventHandler is for distinct Event so I don't understand how to use a KeyListener on the object 'Item'.
The static methode 'supprime' on 'Projet' is use to deleted my file.
Thank you beforehand.(And sorry for my bad english).
As stated in the TreeItem documentation (under "TreeItem Events"):
It is important to note however that a TreeItem is not a Node, which
means that only the event types defined in TreeItem will be delivered.
To listen to general events (for example mouse interactions), it is
necessary to add the necessary listeners to the cells contained within
the TreeView (by providing a cell factory).
For key presses, however, the actual cells do not get keyboard focus, and so they do not receive key events. So what you really want here is that when the TreeView has focus and the delete key is pressed, then delete the selected item in the tree. So you need
TreeView<String> tree = ... ;
tree.setOnKeyPressed(e -> {
TreeItem<String> selected = tree.getSelectionModel().getSelectedItem();
if (selected != null && e.getCode() == KeyCode.DELETE) {
System.out.println("Delete pressed on "+selected.getValue());
// delete file associated with selected.getValue()...
}
});
A couple of other notes:
Key typed events do not have a code associated with them (see docs). You need a key pressed event here, not a key typed event.
Since your tree view seems to be displaying files, it might make (a lot more) sense to have a TreeView<File> and to modify the updateItem method in the cell implementation to display the name of the file. Then you can get the file directly with getItem() in the listener, and the code to delete it will be much easier.
I am using this link for creating a ContextMenu for each table row. Right now I'm running into problems because I'm not sure how to attach a ContextMenu after the 'type' has been inserted into a row.
Lets say I'm using a .zip editor program, and it lists the contents. I have an Image, and a text file, and some other stuff, all of them are under a class called Entry. My table's generic type is 'Entry', and I'd like to be able to create a context menu for each entry based on it's underlying subclass type (like an ImageEntry might return a menu item to open it up in an image editor...etc).
Right now I have a generic context menu for everything, but it's not great displaying a menu item about opening a text file with an image editor...
Is this possible to do? If so, what is the proper way to go about doing it?
Add a listener to the row's itemProperty (which represents the item displayed in the row) and update the context menu when it changes:
table.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
#Override
public TableRow<Person> call(TableView<Person> tableView) {
final TableRow<Person> row = new TableRow<>();
final ContextMenu contextMenu = new ContextMenu();
row.itemProperty().addListener((obs, oldPerson, newPerson) -> {
contextMenu.getItems().clear();
// add items to context menu depending on value of newPerson
// ...
});
// Set context menu on row, but use a binding to make it only show for non-empty rows:
row.contextMenuProperty().bind(
Bindings.when(row.emptyProperty())
.then((ContextMenu)null)
.otherwise(contextMenu)
);
return row ;
}
});
Well it appears I have been stumped by one of the simplest ListView implementations out there. For a few days I have found it impossible to properly reallocate a JavaFX ListView. I am working on making an EntityView completely dynamic, being able to remove elements whenever needed through a ContextMenu. So I have a ListView that is populated by an ArrayList, which we will refer to as "renderable". When you select "Remove" in the context menu, it removes the Entity from the renderable list, which also happens to be the "value" of the List Cell on whom I right clicked. Afterwards, I wish to refresh the ListView and remove now the nonexistent cell. So by creating a new ObservableList with the new renderable list (which removes the correct entity and that works fine), I set the items in the ListView, which does jack shit. I can set the list to null in this case, which removes all the elements. But I cannot reset the list with the new array and it remove the now missing entity. Somebody point me in the right direction please!
When I use the method I stated above, it removes it from the list, but not visually. There becomes an unusable cell at the bottom of the list, which has its name.
public void createContextMenu(final Entity curr, MouseEvent me){
MenuItem[] items = {new MenuItem("EDIT TYPE"), new MenuItem("REMOVE")};
ContextMenu menu = new ContextMenu(items);
menu.show(list, me.getScreenX(), me.getScreenY());
items[1].setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent arg0) {
CanvasTab tab = (CanvasTab) core.canvasTabbedPane.getSelectionModel().getSelectedItem();
Kanvas k = tab.canvas;
k.renderable.remove(curr);
System.out.println(k.renderable);
k.redraw();
EntityView.this.list.getItems().remove(curr);
ObservableList<Entity> temp = FXCollections.observableList(k.renderable);
EntityView.this.list.setItems(temp);
}
});
}
This is the context menu:
how do i go do this, based on input in textfield, you get some results inside jlist, after you select option in jlist you then get an action, code examples would be appreciated... this is what i got so far:
final DefaultListModel<String> locations = new DefaultListModel<String>();
getTextFieldSearch().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int i=0;i<10;++i) {
locations.add(i, "blah");
}
}
});
JList<String> list_racentRaces = new JList<String>(locations);
Start by taking a look at How to Use Lists, which has lots of awesome code examples.
The basic idea would be to...
When your actionPerformed method is triggered, create a new DefaultListModel, assuming you don't have your own implementation, fill it with all the new items you need and apply it to the instance of list_racentRaces
If you want to maintain what was previously in the list, you should consider starting with a DefaultListModel and simply add the new items to it as you need to...
Then, attach a ListSelectionListener to list_racentRaces and when the valueChanged event is triggered, find the selected item(s) and do what ever you need to based on these result(s)
You can find more details and examples through How to Write a List Selection Listener
Requirement:
I have a list of strings displayed in the ComboBox. Each of these Strings can have some properties. These properties are displayed in PropertyTable. ComboBox's selected Item's properties are displayed in the table. In addition, we use PropertyTable for editing or setting property values to the selected item in the comboBox.
Problem:
The moment I de-select the comboBox Item,say item1, all the existing property values in the PropertyTable are set as new property values to item1. Again, when I select this item1 back, I should get above property values(i.e values before item1 is Deselected) back in to the PropertyTable?
Current Implementation Logic:
I am having TableCellListner for each PropertyTableCell, whenever cell content is changed, it takes the cell's new value and assigns this as new property value to the combo box's selected item. whenever new item is selected, table is refreshed with the selected Item's property values.
//before is Table initialization code
Action action = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
TableCellListener table = (TableCellListener)e.getSource();
String selectedItem=(String)ComponentPropComboBox.getSelectedItem();
if(table.getColumn()==1 && selectedItem!=null)
{
Property property=propertyMap.get(selectedItem);
else if(table.getRow()==0)
{
property.setProperty("MIN_LENGTH", (String)table.getNewValue());
propertyMap.put(selectedItem, property);
}
else if(table.getRow()==1)
{
property.setProperty("STARTS_WITH_STRING", (String)table.getNewValue());
propertyMap.put(selectedItem, property);
}
}
}
};
TableCellListener tcl = new TableCellListener(PropertiesTable, action);
How do i implement this requirement by overcoming the above challenge?
PS:
TableCellListner is a Not a java generic library. You can view code and its explanation at the following links:
http://www.camick.com/java/source/TableCellListener.java
http://tips4java.wordpress.com/2009/06/07/table-cell-listener/
I believe the question is obvious! Pls do let me know if question is not clear.Thanks in advance for your help & donating the knowledge!
In the code that listens for JComboBox selections. At its start have it set a boolean that the item is being changed. Then have your table refresh code ignore events that come while the boolean is set. After you are finished refreshing then set the boolean back.