JavaFX Unselect item on TreeView - java

I have a TreeView, when I select an item, a tab opens with the corresponding information.
But when I close the tab and try to open it, it does not appear because the item has already been selected. And I need to first select another, and then click on it again.
I use this.
fileView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == null)
return;
System.out.println("Selected File : " + newValue.getValue().getAbsolutePath());
if (newValue.getValue().isFile()) {
...
}
});
I see 2 solutions to the problem.
1) remove the selection label from the item
2) replace the listener
But I didn’t succeed.
I will be glad to your solutions.
Please provide a code snippet for example
P.s. please do not lower my reputation, I'm really interested in my question

You can clear the selection after opening a tab:
fileView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null) {
System.out.println("Selected File : " + newValue.getValue().getAbsolutePath());
if (newValue.getValue().isFile()) {
// Open a tab here...
/* Clear selection */
Platform.runLater(() -> fileView.getSelectionModel().clearSelection());
}
}
});

Related

Enable edit only for the selected row in the table

I am fairly new in JavaFX. I have a table with multiple columns and an Edit button in each row. Whenever I click on the Edit 1st cell is selected for edit of that row. Everything goes fine but the problem is whenever I select a row and click on a cell of it also gets in edit mode though I have not clicked Edit. I think it's because I have made tableView.setEditable(true) but I want to enable edit only when I click Edit button and only for that row, not others when I double click/ single click. I have seen this but they have solved it by a pop-up. I don't want that.
In the initialize() method I tried this way but it's not working exactly. Can anyone show me the correct way to do this, please?
tableBuilding.setRowFactory(tv -> {
TableRow<ModelBrBuilding> row = new TableRow<>();
row.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
if (e.getButton() == MouseButton.SECONDARY) {
e.consume();
}
});
row.addEventFilter(MouseEvent.MOUSE_PRESSED,event -> {
if ( event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2 ) {
event.consume();
}
});
return row ;
});
and my button action looks like
EditButton.setOnAction((ActionEvent event)->{
TableRow row = cell.getTableRow();
tableBuilding.requestFocus();
tableBuilding.getSelectionModel().select(row.getIndex());
int i=tableBuilding.getSelectionModel().getSelectedIndex();
tableBuilding.edit(i,colName);
});
There may be a more elegant solution to this, but you could simply set the TableView editable right before you call tableView.edit(int, TableColumn) and then set it back to not being editable when the edit is either committed or cancelled. Or you can do this set\unset editable thing on each specific TableColumn.
For instance, in your Button's action handler:
button.setOnAction(evt -> {
evt.consume();
tableView.requestFocus();
tableView.edit(-1, null); // cancel any current edit if there is one
tableView.getSelectionModel().select(tableCell.getIndex());
tableView.setEditable(true);
tableView.edit(tableCell.getIndex(), tableColumn);
});
And then for the TableColumn:
EventHandler<TableColumn.CellEditEvent<S, T>> handler = evt -> tableView.setEditable(false);
tableColumn.setOnEditCancel(handler);
tableColumn.setOnEditCommit(handler);
// or you can use the addEventHandler(EventType, EventHandler) method

JavaFX setOnKeyTyped event firing before text field updated

txfInput.setOnKeyTyped(
e -> {
if(group.getSelectedToggle() == rdEncrypt){
txfOutput.setText(en.encryptText(txfInput.getText()));
}else if(group.getSelectedToggle() == rdDecrypt){
txfOutput.setText(en.decryptText(txfInput.getText()));
}
}
);
In the following code, the text that gets passed to the encryptText method is the text before the key was pressed. I am curious how to update the txfInput before the code gets run.
Sorry - all I had to do was change onKeyTyped to onKeyReleased
txfInput.setOnKeyTyped -> txfInput.setOnKeyReleased
Thank You #sedrick-jefferson!

JFace TreeView remove item

I have a treeView in my wizard application. Whenever i add or remove an object from my model , calling the update or refresh methods works as expected. What i want is , when i press a certain check button, two things may happen: If the new selection is false(unchecked) , i want to remove the treeView items , so they wont show in my UI, and when the new selection is true(checked) , i want to add the previously removed items (i have them stored in my application), so they can show up again. So i added this listener to my button :
oIsAuthorizableResourceButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
bIsResourceAuthorizable[intSelectedResourceIndex] = oIsAuthorizableResourceButton.getSelection();
//recursiveSetEnabled(grpPolicySetConfiguration,oIsAuthorizableResourceButton.getSelection());
if(!bIsResourceAuthorizable[intSelectedResourceIndex]){
System.out.println("Remove : " + oSelectedPolicySet.getHasResourceAccessPolicy().size());
oTreeViewer.remove(oSelectedPolicySet.getHasResourceAccessPolicy().toArray());
oTreeViewer.refresh(oSelectedPolicySet);
oTreeViewer.refresh();
}else{
System.out.println("Add : " + oSelectedPolicySet.getHasResourceAccessPolicy().size());
oTreeViewer.add(oSelectedPolicySet, oSelectedPolicySet.getHasResourceAccessPolicy().toArray());
oTreeViewer.refresh(oSelectedPolicySet);
oTreeViewer.refresh();
}
}
Well this code does absolutely nothing.Any help appreciated.
Implement a Viewer Filter, then add and remove it from your viewer: http://www.eclipse.org/articles/Article-TreeViewer/TreeViewerArticle.htm . What it filters, and whether it even cares about a specific property, is your choice.

Eclipse E4 - Menu contribution and PersistedState

I have got ditched in a problem with Menu Contribution and PersistedState. I had no problem before removing the -clearPersistedState flag from the VM args.
Now, the app has a weird behaviour, the menu contribution starts to pile up a menu entry every time the code is executed.
Here it's the guilty snippet enclosed in a Processor:
MDirectMenuItem menuItem = MMenuFactory.INSTANCE.createDirectMenuItem();
menuItem.setLabel("Another Exit");
menuItem.setContributionURI("bundleclass://"
+ "com.telespazio.optsat.wizard/"
+ ExitHandlerWithCheck.class.getName());
if (!menu.getChildren().contains(menuItem))
menu.getChildren().add(menuItem);
The menu items you add to the application model will be persisted, so you need to check if they already exist in the menu. The contains check you currently have does not do this.
You need to check for a match of the label (or the contribution URI, or the id), something like:
List<MMenuElement> children = menu.getChildren();
boolean gotExisting = false;
for (MMenuElement child : children)
{
if ("Another Exit".equals(child.getLabel())
{
gotExisting = true;
break;
}
}
if (!gotExisting)
{
... add to menu
}

Java Swing drop down list

I have created a drop down list using JAVA Swing. When I select "Keep track of status of RCM:", I want to create another drop down list next to the option selected. should I use mouseactionlistener instead? I trying to accomplish something like in this, when I click menu options, there is another list which I can select under menu options category. Example : http://smoothjazztampabay.com/wp-content/rockettheme/rt_metropolis_wp/menu-options/dropdownmenu.jpg
I tried using this code but couldn't.
if (state == ItemEvent.SELECTED)
{
ItemSelectable itemS = itemEvent.getItemSelectable();
String cmd = selectedString(itemS);
if ( cmd.equals("Keep track of status of RCM:"))
{
RCMCombo2.addItem(RCMCombo);
selectionPanel.add(RCMCombo2);
}
The full version of the code is shown as below:
String [] RCM2 = {"Keep track of status of RCM:", "Add and activate RCM", "Remove RCM",
"Display the usage statistics for RCM",
"Update capabilities of RCMs", "Show RCM used most frequently in the last n days",
"Display number of times the RCM was emptied in n hours"};
RCMCombo2 = new JComboBox(RCM2);
RCMCombo2.addItemListener(itemListener);
RCMCombo2.setEditable(false);
RCMCombo2.setBounds(10,10,10,30);
//"Updates capabilities of RCM"); Get the location of RCM");
selectionPanel.add(RCMCombo2);
ItemListener itemListener = new ItemListener()
{
public void itemStateChanged(ItemEvent itemEvent)
{
int state = itemEvent.getStateChange();
//System.out.println((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
//System.out.println("Item: " + itemEvent.getItem());
if (state == ItemEvent.SELECTED)
{
ItemSelectable itemS = itemEvent.getItemSelectable();
String cmd = selectedString(itemS);
if ( cmd.equals("Keep track of status of RCM:"))
{
RCMCombo2.addItem(RCMCombo2);
selectionPanel.add(RCMCombo2);
}
Any help will be appreciated. Thank you.
It looks like you are on a completely wrong track. I think you are mixing the usage of ItemListener with the setup of the lists model.
Check out this guide to ComboBox - it should contain all the information you need.

Categories