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

