JList: Double-click deselection without sensitizing entire viewport?

C

Charles Packer

Our customer wants its users to click _twice_ on a list item to remove it
from a listbox. I figured out how to do this -- against all the odds, since
I'm relatively new to Java. I have included example code below. The problem
now is that the white space below the last list item is sensitive to
mouse clicks and clicking _anywhere below_ the last item would remove the
last item.

The following example puts up a small window containing a list of four
text strings. Clicking twice on a string causes a typeout to the terminal
window of the index of the item clicked on. Note that double-clicking
anywhere below the last item causes the index of the last item to be typed.
I need to know how to de-sensitize that area.

//-------------------- code follows
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//-----------------------------------------
public
class
aa extends JFrame {
//-----------------------------------------
public
static
void
main(String[] args) {
aa Fr = new aa(50, 50, 200, 200);
Fr.show();
}
//-----------------------------------------

public
aa (int InitX, int InitY, int Width, int Height) {
Create();
setSize(new Dimension(Width, Height));
setLocation(InitX, InitY);
}
//-----------------------------------------
Vector Contents = new Vector();
JList L = new JList();
JPanel P = new JPanel();
//-----------------------------------------
private
void
Create () {
new JFrame();
Contents.addElement("Line0");
Contents.addElement("Line1");
Contents.addElement("Line2");
Contents.addElement("Line3");
L.setListData(Contents);
L.setVisibleRowCount(10);
L.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
L.addListSelectionListener(new ClickedOnTheViewport());
L.addMouseListener(new ClickedTwiceOnIt());
JScrollPane SP = new JScrollPane(L);
P.setLayout(new BoxLayout(P, BoxLayout.Y_AXIS));
P.add(SP);
getContentPane().add(P);
}
//-----------------------------------------
class
ClickedOnTheViewport implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {

//OperateOnThisItem();
}
} // End of inner class definition

//-----------------------------------------
class
ClickedTwiceOnIt extends MouseAdapter {
public void mouseClicked (MouseEvent E) {
if (E.getClickCount() >= 2)
OperateOnThisItem();
}
} // End of inner class definition

//-----------------------------------------
void
OperateOnThisItem () {
System.out.println(L.getSelectedIndex());
}
//-----------------------------------------
} // End of class definition
 
J

Jeff

You simply need to check that the x, y location of
the mouse click is within the bounds of the list item
returned by getSelectedIndex() before removing it.
Use JList.locationToIndex(Point location) to verify.
--
Jeff
jlar310 at yahoo

Charles Packer said:
Our customer wants its users to click _twice_ on a list item to remove it
from a listbox. I figured out how to do this -- against all the odds, since
I'm relatively new to Java. I have included example code below. The problem
now is that the white space below the last list item is sensitive to
mouse clicks and clicking _anywhere below_ the last item would remove the
last item.

The following example puts up a small window containing a list of four
text strings. Clicking twice on a string causes a typeout to the terminal
window of the index of the item clicked on. Note that double-clicking
anywhere below the last item causes the index of the last item to be typed.
I need to know how to de-sensitize that area.

//-------------------- code follows
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//-----------------------------------------
public
class
aa extends JFrame {
//-----------------------------------------
public
static
void
main(String[] args) {
aa Fr = new aa(50, 50, 200, 200);
Fr.show();
}
//-----------------------------------------

public
aa (int InitX, int InitY, int Width, int Height) {
Create();
setSize(new Dimension(Width, Height));
setLocation(InitX, InitY);
}
//-----------------------------------------
Vector Contents = new Vector();
JList L = new JList();
JPanel P = new JPanel();
//-----------------------------------------
private
void
Create () {
new JFrame();
Contents.addElement("Line0");
Contents.addElement("Line1");
Contents.addElement("Line2");
Contents.addElement("Line3");
L.setListData(Contents);
L.setVisibleRowCount(10);
L.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
L.addListSelectionListener(new ClickedOnTheViewport());
L.addMouseListener(new ClickedTwiceOnIt());
JScrollPane SP = new JScrollPane(L);
P.setLayout(new BoxLayout(P, BoxLayout.Y_AXIS));
P.add(SP);
getContentPane().add(P);
}
//-----------------------------------------
class
ClickedOnTheViewport implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {

//OperateOnThisItem();
}
} // End of inner class definition

//-----------------------------------------
class
ClickedTwiceOnIt extends MouseAdapter {
public void mouseClicked (MouseEvent E) {
if (E.getClickCount() >= 2)
OperateOnThisItem();
}
} // End of inner class definition

//-----------------------------------------
void
OperateOnThisItem () {
System.out.println(L.getSelectedIndex());
}
//-----------------------------------------
} // End of class definition
 
S

Sudsy

Charles said:
Our customer wants its users to click _twice_ on a list item to remove it
from a listbox. I figured out how to do this -- against all the odds, since
I'm relatively new to Java. I have included example code below. The problem
now is that the white space below the last list item is sensitive to
mouse clicks and clicking _anywhere below_ the last item would remove the
last item.

The following example puts up a small window containing a list of four
text strings. Clicking twice on a string causes a typeout to the terminal
window of the index of the item clicked on. Note that double-clicking
anywhere below the last item causes the index of the last item to be typed.
I need to know how to de-sensitize that area.

Javadocs are your friends! Sun provides an excellent example of exactly
what you want to do. Here's the modified code:

//-----------------------------------------
class
ClickedTwiceOnIt extends MouseAdapter {
public void mouseClicked (MouseEvent E) {
if (E.getClickCount() == 2) {
int index = L.locationToIndex(E.getPoint());
OperateOnThisItem( index );
}
}
} // End of inner class definition

//-----------------------------------------
void
OperateOnThisItem ( int index ) {
System.out.println( "Index = " + index );
}
//-----------------------------------------
} // End of class definition

Note the invocation of locationToIndex with an argument of the
mouse click location provided in the MouseEvent. If you click
in the white area below the items then the index will be -1.
Just what the doctor ordered, eh?
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top