JTree and right click popup menu ?

Posted on 2002-07-18
Last Modified: 2010-08-05
Well I have a JTree which is made using DefaultMutableTreeNode with userObjects.

Now also I have as a standard certain actions which I can perform on a node when I right click on the node. Thus for that I have a popup menu.

The popup menu has following actions which I can perform on the particular node.

1. Archive
2. Unarchive
3. Delete
4. Undelete
5. Rename
6. Add
7. Move (using self makde DND by implementing MouseMotionListener)

Now to enable or to disable these options the rules are all lying in DefaultMutableTreeNode instance.

How should I have a set pattern to make this enable/disabling feature execute.

The situation is like if the node has its beans deleteFlag() set to true the all options have to be disabled except for undelete.

If the archiveFlag() is true then only unarchive and delete options have to enabled rest all disabled.

I mean I want to have a object oriented solution to this problem instead of getting lost in -- if and else if statements.

Kinldy demostrate me with a scalable solution so that I can really work well for the same in not only robust but ease of use way.

Thanx in advance.


Question by:dumdum420
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4

Expert Comment

ID: 7162957
First, since all of the rules are contained in your extended version of the DefaultMutableTreeNode I would have a method in this class called something like:
void applyBeanState(JMenuItem/Component item)

BeanState is probably not a good name, but your smart you can think of a better name.  Anyway you would then call this method iterating through each MenuItem or Component of the JPopupMenu.  The applyBeanState() method would contain all of the messy if/else code you spoke of which would change the properties of the component you passed in it.  

Of course you could go a step further and create some type of delegate pattern for each different type of action, archive, unarchive.  That way you could get around having to write the if/else mess.  But not knowing your project, that sounds a bit like overkill.

Author Comment

ID: 7163042
I understand you dont no my project but think :

all these actions will return one of the two states

Now I will ellaborate one of the action UNARCHIVE to you ...

these are in a flow

  if node is archived (DISABLE THIS ACTION);
  else is nodes parent term is archived (DISABLE THIS ACTION);

Similarly other actions have there workflows set to ...
but for if the node has its deleteFlag() set to true we should not be even in the business to check for the UNARCHIVE workflow ...

Thus if i check each workflow individually against each action then lot of things are repeated and if i dont i have bad code of else if etc etc. I mean I want a a set example in a pattern how to do this stuff in OOP way.

I mean if it is delegate kindly send me a pseudo code of how to do this stuff else think and I really need a very practical and robust solution.

Thanx in advance.


Accepted Solution

wgilster earned 300 total points
ID: 7163196
It sounds like my simple solution isn't scalable in the direction you were looking for, although I wouldn't overlook the future need to do more than just boolean property setting.

Here is a possible solution.  You develop an interface called something like
interface PropertySettingDelegate {
  void applyProperties(DefaultMutableTreeNode node, Component mycomp);

Then you create subclasses of this class called:
class ArchiveDelegate implements PropertySettingDelegate
class DeleteDelegate implements PropertySettingDelegate
for each of your menu actions.  REMEMBER to put your property setting logic in each of these delegates.

Next when you are creating each of these menu items, you need to attach the menu item with it's corresponding delegate.  I would start off using a hashtable:

Hashtable menuItemComponentToDelegateLinks = new Hashtable();
menuItemComponentToActionLinks.put(ArchiveMenuItem, new ArchiveActionDelegate());
menuItemComponentToActionLinks.put(DeleteMenuItem, new DeleteActionDelegate());

Then when the user clicks on the node you will iterate through the menuItems in your popup and lookup it's corresponding delegate like this:
for (int t = 0; t < numberOfMenuItemsInPopup; t++) {
  Component currentMenuItem = getComponentInPopupMenuByIndex(popupMenuToShow, t);
  PropertySettingDelegate someDelegate = menuItemComponentToDelegateLinks.get(currentMenuItem);
  someDelegate.applyProperties(ClickedNode, currentMenuItem);

This gives you some extreme scalibility, because you get to reuse the delegates for different popup menus that you might want to create in the future and you have a separation of duties for each different instance of the delegate. The problem is that it's OO which means it takes some thinking to get used to it.
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.


Author Comment

ID: 7163319
Well this sounds very intersting I am really going to try this approach.

This approach essentially means that I have to apply business rule for each and every item in my popup menu.

Just on a wild card I am asking you is there a way that if the node has its deleteFlag() set to true  i dont even have to check for other deletgates and just enable the undelteFlag delegate.

This saves lot of iteration and that is what is being in the digital world is all about. I think what I am asking is definitely a wrong approach but still ...???

Get back to you as soon as I can!!!

Thanks again.


Expert Comment

ID: 7171742
So how did it all turn out?

To answer your last question, yes.  This will start to get ugly if you start putting optimizations such as what you stated.  But if you think about it, you wouldn't even be saving an iteration as you thought, because you have to iterate through all of those menus to turn them off anyway.  All you would be saving is maybe an if statement which is very cheap.

Wes G.

Author Comment

ID: 7177700
It did work for me. Thanx a lot the appraoch was real clean and made the code real flexible. But one thing since my move operation is GUI based like drag and drop how would this fit into this approach.

Anyways I am giving points. I will very sincerely wait for your reply for the move operations. If you have anyother query that I can clarify please do post.

Thax in advance.


Author Comment

ID: 7177702
Thanx I have posted another query do let me know about the move operation.

Thax again.

Author Comment

ID: 7178200
well i got stuck when i tried to put lineSeperators in my JMenuItem since I am getting things from indexing of the JPopupMenu and seperator also is added to the index.

Help I am really stuck.


Expert Comment

ID: 7178237
You need to add a line in this method:

for (int t = 0; t < numberOfMenuItemsInPopup; t++) {
 Component currentMenuItem = getComponentInPopupMenuByIndex(popupMenuToShow, t);
 PropertySettingDelegate someDelegate = menuItemComponentToDelegateLinks.get(currentMenuItem);
 >>> Add Line the line here
 someDelegate.applyProperties(ClickedNode, currentMenuItem);

>>>Add this line
 if (!(someDelegate instanceof JLineSeperator))


Author Comment

ID: 7186156
thanx I could add line seperator.


Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 52
maven module vs maven project 3 75
Problem to Alipay 10 73
Need help aligning JRadioButton that put in a box using GridBagLayout 2 27
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question