Mouse even on JLists

A

Aaron Fude

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
 
A

Alex.From.Ohio.Java

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 item");
frame.add(label,BorderLayout.NORTH);
final JList list=new JList(new String[]{"one","two"});
list.addListSelectionListener(new ListSelectionListener(){

public void valueChanged(ListSelectionEvent e) {
label.setText((String)list.getSelectedValue());
}});

list.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
label.setText("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/
 
J

Jennifer Eden

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!

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 item");
                frame.add(label,BorderLayout.NORTH);
                final JList list=new JList(new String[]{"one","two"});
                list.addListSelectionListener(new ListSelectionListener(){

                        public void valueChanged(ListSelectionEvent e) {
                                label.setText((String)list.getSelectedValue());
                        }});

                list.addMouseListener(new MouseListener(){
                        public void mouseClicked(MouseEvent arg0) {
                        }
                        public void mouseEntered(MouseEvent arg0) {
                        }
                        public void mouseExited(MouseEvent arg0) {
                        }
                        public void mousePressed(MouseEvent arg0) {
                                label.setText("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?
 
A

Alex.From.Ohio.Java

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 item");
frame.add(label,BorderLayout.NORTH);
final JList list=new JList(new String[]{"one","two"});
list.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
label.setText((String)list.getSelectedValue());
}});
list.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
label.setText("Mose pressed");
}
public void mouseReleased(MouseEvent arg0) {
}});


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.

Thanks, but how would you know whether the second click was on the
last item or on the empty space?

That's not about second or third click. It's about having additional
control/listener/approach.

JList does act on each click notifying listeners. When mouse clicked
outside of existing items it assumes that last item was clicked.

But when you listen for mouse too you can see mouse coordinates and
decide was it close enough to last item to actually make selection or
it was empty area. And based on your decision you can even deselect
this last item and act only as for empty area.
Using mouse pressed and released methods you can act before or after
selection listeners acts.
You also can use the same changelistener to catch actual selection
events and compare them to mouselisteners events and calculate actual
size and bounds of real empty area and act accordingly.

After all you can retrieve size and bounds of real JList as real
Component, calculate where empty area is and act accordingly.

Possibilities are endless. Choose one which is comfortable/achievable
for you or ask somebody who's affordable for you (for example me) and,
actually, there is no problem at all.

That's why I love Java. You can do whatever you can imagine. Which is
the only limit.

Alex.
 
J

Jennifer Eden

On Jul 27, 2:37 pm, (e-mail address removed) wrote:
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 item");
                frame.add(label,BorderLayout.NORTH);
                final JList list=new JList(new String[]{"one","two"});
                list.addListSelectionListener(new ListSelectionListener(){
                        public void valueChanged(ListSelectionEvent e) {
                                label..setText((String)list.getSelectedValue());
                        }});
                list.addMouseListener(new MouseListener(){
                        public void mouseClicked(MouseEvent arg0) {
                        }
                        public void mouseEntered(MouseEvent arg0) {
                        }
                        public void mouseExited(MouseEvent arg0) {
                        }
                        public void mousePressed(MouseEvent arg0) {
                                label..setText("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?

That's not about second or third click. It's about having additional
control/listener/approach.

JList does act on each click notifying listeners. When mouse clicked
outside of existing items it assumes that last item was clicked.

But when you listen for mouse too you can see mouse coordinates and
decide was it close enough to last item to actually make selection or
it was empty area. And based on your decision you can even deselect
this last item and act only as for empty area.
Using mouse pressed and released methods you can act before or after
selection listeners acts.
You also can use the same changelistener to catch actual selection
events and compare them to mouselisteners events and calculate actual
size and bounds of real empty area and act accordingly.

After all you can retrieve size and bounds of real JList as real
Component, calculate where empty area is and act accordingly.

Possibilities are endless. Choose one which is comfortable/achievable
for you or ask somebody who's affordable for you (for example me) and,
actually, there is no problem at all.

That's why I love Java. You can do whatever you can imagine. Which is
the only limit.

Alex.
--http://www.myjavaserver.com/~alexfromohio/- Hide quoted text -

- Show quoted text -

Thanks. What methods tell me the coordinates of the actual items in
the list?
 
D

Daniele Futtorovic

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.)

[Alex.From.Ohio's observations withal]

Mind the Javadoc!
<http://java.sun.com/javase/6/docs/api/javax/swing/JList.html>

Look at the method:
<http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#locationToIndex(java.awt.Point)>

I'm not sure I interpret the doc correctly, and I'm too lazy to try it
myself, but it seems as though that method might return, for instance,
the last index in the JList's model, in case the click happened /below/
that item. If this were the case (test it), you might want to perform an
additional check using:
<http://java.sun.com/javase/6/docs/api/javax/swing/JList.html#getCellBounds(int, int)>,
combined with:
<http://java.sun.com/javase/6/docs/api/java/awt/Rectangle.html#contains(java.awt.Point)>
 
M

Martin Gregorie

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?
Here's an alternative suggestion to using lots of listeners.

Extend the class that defines the list items by adding a boolean attribute
that says whether its empty. This would control the way it appears in the
list and the actions to be taken when its selected. Then simply make sure
that the last item in the list is an empty item.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top