Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: JTable/ListSelectionModel right-click issues



I have come up with a solution to this so I figured I would post it. I
used the class Doug posted for my example solution. For those that
weren't following this thread, the problem was that if you selected
multiple items in a JTable or JList, then ctrl-left-clicked the list
(emulating a two-button mouse) in order to display a popup menu, all
selected items except for the one you clicked on would be deselected.
Basically ctrl-left-click performed a left-click followed by a
right-click.

My solution is to cache the previous values and reset them for the case
of a ctrl-left-click which is identified by a left-click and a
popup-click at the same time. This is still platform independent
because a left-click will never be a popup-click on another platform
where the right-click behavior works as expected.

It makes use of the getSelectedIndices() function to store the current
selection in an integer array then setSelectedIndices(int[]) to restore
the values. Because a JTable does not implement
setSelectedIndices(int[]), don't ask me why not, I had to use a for loop
with addRowSelectionInterval() to restore the values of my JTable. For
example:

table.clearSelection();
for (int i=0; i<table.length; i++) {

table.addRowSelectionInterval(selectedCache[i],selectedCache[i]);
}

You will also have to implement a KeyListener if you want to catch a
ctrl-a (select all) event and cache the selection before the user
ctrl-left-clicks.
I didn't put this in the example but you could simply do the following
for a JTable:

table.addKeyListener(new KeyListener() {
public void keyPressed(keyEvent e){}
public void keyTyped(keyEvent e){}
public void keyReleased(keyEvent e) {
selectedCache = table.getSelectedIndices();
}
});

Anyway, here is the modified example that Doug gave. Let me know if you
see any flaws with this design.

--

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Popup extends JFrame {

private static String[] initData = { "Alpha", "Bravo", "Charlie",
"Delta", "Echo", "Foxtrot" };
private int[] selectedCache;

JList list = new JList (initData);

public static void main (String[] args) {
Popup me = new Popup();
me.show();
System.err.println (me.list.getUI().getClass().getName());
}

public Popup () {
super ("Popup Tester");

getContentPane().setLayout (new FlowLayout());
getContentPane().add (list);

list.addMouseListener (new MouseAdapter() {
private boolean trigger = false;

public void mousePressed(MouseEvent e) {
//System.err.println ("mousePressed: " + e);
handlePopup(e);
selectedCache = list.getSelectedIndices();

}
public void mouseClicked(MouseEvent e) {
//System.err.println ("mouseClicked: " + e);
handlePopup(e);
selectedCache = list.getSelectedIndices();
}
public void mouseReleased(MouseEvent e) {
//System.err.println ("mouseReleased: " + e);
handlePopup(e);
selectedCache = list.getSelectedIndices();
}
public void handlePopup(MouseEvent e) {
int count=0;
System.err.println (" popup: " +
e.isPopupTrigger());
System.err.println (" right: " +
SwingUtilities.isRightMouseButton(e));
System.err.println (" left: " +
SwingUtilities.isLeftMouseButton(e));
if (e.isPopupTrigger() &&
!SwingUtilities.isRightMouseButton(e)) {
if (selectedCache.length > 1)
list.setSelectedIndices(selectedCache);
}
for (int i=0; i<6; i++) {
if(list.isSelectedIndex(i)) count++;
}
System.err.println(" selected: " + count);
}
});
pack();
}
}

--
Christopher Huyler
Computer Associates Intl.
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.