Link to home
Start Free TrialLog in
Avatar of cathalmchale
cathalmchale

asked on

Tree cell editing

Hi,

I have a JTree - each cells can be 1 of 2 data-types - only one of these types is editable
 - is there a way of telling the editor this so that the editor will not be invoked for the un-editable type??
Avatar of girionis
girionis
Flag of Greece image

Can you do something like:

if (<variable> instanceof <type you want>)
{
   // invoke editor
}
Avatar of cathalmchale
cathalmchale

ASKER

where abouts would u have this code??
Can you post the relevant code where you have your cell editor?
for example this method in the editor:

public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row)

is called when a cell gets clicked, but its too late to do my checking data type here as the method insists a component is returned,
so I thought perhaps over-riding the isCellEditable method, but dont know how this would help??
You can always create your own cell editor and define the functionallity you want: http://javaalmanac.com/egs/javax.swing.table/CustEdit.html
Then put my pseudo-code inside the getTableCellEditorComponent. If the data type of the value passed is the one you want, do your processing and return the new value, otherwise return the same component.
>> You can always create your own cell editor

I have!!

Another question - is there a way of saying "Only start cell editing after a double click??"
 - i think i noticed this functionality in some component before??
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> otherwise return the same component

this is what i was doing.  It means returning an appropriate JLabel which doesnt look quite as good, and then it also means that i now have to handle the selection highlighting myself
The cell editor will be called anyway, no way to change that apart from locking the ceel (but then you wont be able to edit even the value you want). You will have to look to another solution like reading the call but ignoring the value passed. What happens if you return null? Is this acceptable?
>> What happens if you return null? Is this acceptable?

No.

But this double click routine is perfect - now for single click on cell editing isnt invoked (and JTree handles its own selection highlighting), for double click i handle the editing (and just return an appropriate JLabel for the data-type which isnt editable)

Thanks a lot for your help :)
Thank you for accepting, glad I was of help :)