I wish to be able to make my JList respond to a click even when it is pressed more than once.Value changed is called only when another index is clicked.This is the default behaviour.However,i Want it to always tell when a click occurs.From this simple example,it is clear that the value changed will be fired only if i click it once,clicking it twice will not respond.I need this specific functionality,The main reason is legitimate but more complex to explain.
public class TestFrame extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JList list = new JList();
list.setModel(new AbstractListModel() {
String[] values = new String[] {"one", "two", "three"};
public int getSize() {
return values.length;
}
public Object getElementAt(int index) {
return values[index];
}
});
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO Auto-generated method stub
System.out.println("selected index : "+list.getSelectedIndex());
}
});
contentPane.add(list, BorderLayout.CENTER);
}
}
You would need to use a MouseListener to solve this:
list.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int index = list.locationToIndex(e.getPoint());
System.out.println("MouseListener index: " + index);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.