Java Problem with the clearselection() method - java

I have 2 trees in my program. Iam using JTree's clearselection() method to clear the selection in 1 tree when something in the other tree is selected. The main code is something like this:(inside a valueChanged event listener and tree being the one on which the current selection has been triggered)
if ( tree == tree1 ){
tree2.clearSelection();
} else {
tree1.clearSelection();
}
When I select for the first time, it works fine. But when I try to select from a different tree after this, it appears the valueChanged method is getting called twice. Any solution?

Did you use the code I gave you in this question?
This included a flag to get round the problem of introducing an infinite loop, and should also ignore subsequent selection events when it is processing the current one.
BTW Given this relates directly to the previous question you may have been better off just commenting on the previous post. That way all the context is kept in one place.

clearSelection() triggers valueChanged as well, so you need a workaround, add some flag and do not clear selection when it's true.
EDIT. Seems like using some flag is tricky. Can you use MouseListener instead and run same code within mouseClicked event?

Related

SWT: prevent Tree from expanding by doubleclick?

I have a problem with SWT Tree.
My situation is like this: I have a SWT Tree, which contains many TreeItems (Log entries), which contain TreeItems too. Those log entries have really long messages, which could not be shown in the TreeColumns at all. So my idea was: adding a Listener to the tree, which opens a new Dialog by DoubleClick, which shows the entries' details. So far so good.
If I do a double click on a item, it works. BUT: If I do a double click on a parent Item, it will expand (and thats good), but my double click Listener is active then as well and the Dialog will open.
That's not, what I want.
So, there are two solutions to the problem:
1) prevent the Tree from expanding/collapsing by double click automatically and implement the method by myself or
2) recognize, that the item was expanded and the event has to be aborted.
I do not really know how to do 1 or 2. Do u guys know that?
Thanks in advance.
Other answers did not work for me. This worked:
treeViewer.getControl().addListener(SWT.MeasureItem, new Listener(){
#Override
public void handleEvent(Event event) {
}});
I found this in a discussion in the Eclipse Community Forums: Disabling Treeviewer doubleclick expand/collapse.
When you look at this code you might be tempted to believe that it pretends to the tree control that your tree items have a size of zero, and as a result the tree control fails to detect that the double-click happened within the item, so it does not perform the double-click action. However, this is not what is actually happening. Instead, what this snippet does is that it leverages some weird code in the implementation of the tree control, which checks whether a listener has been added for SWT.MeasureItem, and if so, it deliberately avoids handling a double-click. This piece of code is even prefixed with a lengthy comment which a) fails to make sense and b) does not agree with what the code does. (Whatever.) So, bottom line is that by simply adding a handler for SWT.MeasureItem, and regardless of what the handler does, we are preventing the tree control from handling double-clicks. This is a prime example of Programming by Coincidence1.
1 The term "Programming by coincidence" was coined in the book The Pragmatic Programmer by Andy Hunt and Dave Thomas. It refers to relying on luck and accidental successes rather than programming deliberately.
If you are using TreeViewer, you could make use of IOpenListener
treeViewer.addOpenListener(new IOpenListener() {
#Override
public void open(OpenEvent event) {
}
}
There is another solution which works much better.
The problem with the solution from 'sambi reddy' was, that the tree was prevented from expanding by doubleclick, but it was prevented from expanding by clickling on the left handside cross as well.
My solution (that works well), was easy: I added a TreeListener, which listens to expanding/collapsing the tree and removed the expanding/collpasing implementation from the MouseDoubleClick-Listener.
No JFace TreeViewer - it works fine.

Call itemStateChanged() only after user interaction

in my swing application I have a combo box with an ItemListener that does X if the user changes the value (via itemStateChanged()).
However I also have a different function that changes the value of that combo box. In this case I do not want X to be done.
Is there a way to find out if the state change was caused by user interaction or from a function?
Thank you!
Edit: I used the flag method. Thanks for the quick answers. I just want to add, that itemStatechanged is actually called twice, once for deselection and once for selection. This needs to be dealt with otherwise the flag won't have any effect. The problem is discussed here.
There are 2 ways to do the check:
Define a flag isUser. The flag is true by default. Before changing programmatically set it to false and reset after setting the combo-box value. In the listener just check the flag and skip the action if necessary.
Keep a reference to the listener and remove it before setting value, adding after.
From what I understand you could very easily sort out your problem using a flag.
Just make a boolean flag, e.g. isDoneByMethod which will be on entrance to a method set to true and at the end set to false and between these two do the operation on the combobox. Then inside the itemStateChanged() check for the value of the isDoneByMethodflag and act accordingly.
I have the similar kind of situation where I need to differentiate two cases. Both options discussed here (flag and removal/addition of listener) do not work all the time, as the item listener is asynchronously called on EDT thread. However remove and adding the particular item listener is more safe . To further ensure that all the previous actions will be dealt before you add the item listener, I use fireEvents() just before adding the listener again.

Saving value from JComboBox in custom TreeCellEditor

I've got a JTree with a custom TreeModel and a custom TreeCellEditor displaying (for now) a JComboBox through the getTreeCellEditorComponent() override. The tree is displayed properly, with the nodes going into edit mode and displaying the JComboBox when I click on them.
Whenever I edit a node, changing the value from the dropdown, and then proceed to select another node from the three, I can see the TreeCellEditor's cancelCellEditing() being triggered.
What's the "correct" way to stop editing in stead of cancelling it, thus (hopefully?) making sure the model's valueForPathChanged() get's triggered?
After further investigation in the source code I found the answer inside the JTree class:
Setting JTree#setInvokesStopCellEditing(true) means editing is stopped in stead of cancelled whenever I change focus from one node to another. This also means my TreeModel#valueForPathChanged() gets called.

TableModelListener

I am making a student record application... I want a column in it which is editable.
I have attached table model listener on table and on that column i m saving all the updating values
if(table.getSelectedColumn() == 3 && table.getSelectedRow() != -1 && tme.getType() == TableModelEvent.UPDATE)
{
// my code here
table.cellEditAt(row,column); // this is giving me error
}
table.cellEditAt automatically calls table model event and that is producing a infinite loop... Any other method to automatically select a cell for editing????
please post an SSCCE that demonstate your issues, for example based on my question about Infinite loop by implements TabelModelListener linked to my answer,
The answer was to post an SSCCE. We are not mind readers. We can't guess what your editCellAt(...) method does. If it causes a loop, then that would be because you are somehow changing the model and generating another TableModelEvent. Don't do this!
If the problem is somehow related to placing a cell in edit mode, then I would guess you need to wrap that code in a SwingUtilities.invokeLater() to make sure the processing of the original event is completed before you place another cell in edit mode.

JTable: double-click should keep prior selection

I have a JTable where I can select one or more cells. I also want to react on double-click for doing some extra action for the selected cells. But the problem is, when the user double-clicks, the selection changes to the clicked cell. But I want to keep the prior selection on double-click, so I can handle the double-click for all selected cells.
EDIT:
Related to this question:
Java : ignore single click on double click?
But I hope, there is a better/easier solution for my case.
The problem is, that on the first click the first event goes out. A bit later the second click might come or not. So the first click event does know nothing. As in the proposed solution a timer might do.
What also might do is on the first click to select nothing, but invoke a special selection event a bit later.
SwingUtilities.invokeLater(myRunnable);
and on handling the double click/myRunnable the true selection. Timing might be unavoidable though.
you can use setClickCountToStart() for XxxCellEditor, I don't know something about your JTable

Categories