Better way to handle two dependent events?

Hello all,

I have a JList which displays the contents of a directory based on their extensions, and I've created a context-sensitive menu which appears when an item is selected and right-clicked.

Here's my code:

privatevoid dispatchClickEvent(MouseEvent evt)

{

if(listModel.size() == 0)

return;

elseif(evt.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(evt))

{

/*

*Code to handle Double-Clicks

*/

}

elseif(evt.isPopupTrigger())

{

setSelectedIndex(getSelectedIndex(evt.getX(), evt.getY()));

if(getCellBounds(getSelectedIndex(), getSelectedIndex()).contains(evt.getPoint()))

{

buildPopup(evt.getX(), evt.getY());

}

}

}

And here's a simplified popup builder:

privatevoid buildPopup(int x,int y)

{

JPopupMenu menu =new JPopupMenu();

JMenuItem open =new JMenuItem("Open File");

JMenuItem delete =new JMenuItem("Delete File");

delete.addActionListener(new ActionListener()

{

publicvoid actionPerformed(ActionEvent evt)

{

absoluteFiles.get(getSelectedIndex()).delete();

listModel.remove(getSelectedIndex());

refresh();

}

});

menu.add(open);

menu.add(delete);

menu.show(this, x, y);

}

The method I included is a very simplified version of what I'm actually using, so needless to say the code gets very verbose. Is there an easier/more elegant way to handle this sort of thing?

Thanks in advance.

Joe

[2663 byte] By [Joe_ha] at [2007-11-27 7:53:41]
# 1

I'm not sure what you find verbose about the code.

If you want to handle MouseEvents then you need to add the MouseListener. The code looks reasonable. I tried to create a simple framework for supporting double clicks, which you may, or may not find less verbose:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=626866&start=2

Regarding the popup menu. Again, it looks reasonable to me. You need to create all the menu items and related Actions so I'm not sure how you can make it any simpler.

If you like my code from above, then maybe you could prebuild the popup menu and pass it in as a parameter so that all the MouseEvents are handled in the same place.

camickra at 2007-7-12 19:34:54 > top of Java-index,Desktop,Core GUI APIs...