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