Re: Mouse even on JLists
On Jul 27, 2:37 pm, Alex.From.Ohio.J...@gmail.com wrote:
On Jul 27, 11:30 am, Aaron Fude <aaronf...@gmail.com> wrote:> Hi,
Suppose that my JList is tall and only has a few items in it so there
is a lot of white space.
When capturing a mouse even on a JList, how does one tell whether an
item was clicked or the empty space below?
(There reason why I need this is this functionality: If the user
double clicks on an item, he is taken to an editor window to edit that
item. If the user clicks on the empty space, a new item is created and
then edited.)
Thank you very much in advance!
Aaron
You can't press item which does not exist. Empty space is interpreted
as last item in the list.
But since it's Java anything is possible.
You can add some other listeners to the JList which is still, for
example, Component and, eventually, get what you need.
Like this:
public class Gui {
public static void main (String []a){
JFrame frame=new JFrame();
final JLabel label=new JLabel("Select i=
tem");
frame.add(label,BorderLayout.NORTH);
final JList list=new JList(new String[]=
{"one","two"});
list.addListSelectionListener(new ListSel=
ectionListener(){
public void valueChanged(=
ListSelectionEvent e) {
label.set=
Text((String)list.getSelectedValue());
}});
list.addMouseListener(new MouseListener()=
{
public void mouseClicked(=
MouseEvent arg0) {
}
public void mouseEntered(=
MouseEvent arg0) {
}
public void mouseExited(M=
ouseEvent arg0) {
}
public void mousePressed(=
MouseEvent arg0) {
label.set=
Text("Mose pressed");
}
public void mouseReleased=
(MouseEvent arg0) {
}});
frame.add(list);
frame.setSize(200,200);
frame.setVisible(true);
}
}
First click is processed by both listeners and last item is selected
anyway. But it easy could be rearranged with some flag or internal
state of the program.
Even without any complexity second click on empty space gives you what
you want.
Of course you can use other listeners or approaches.
That's just first thing which came to my mind.
Alex.http://www.myjavaserver.com/~alexfromohio/
Thanks, but how would you know whether the second click was on the
last item or on the empty space?