是否有任何干净的方法允许用户选择JTable的多个非连续单元格? 或者我被迫实现自己的ListSelectionModel?
我在JTable上使用setCellSelectionEnabled()和setSelectionModel()方法,但我只能选择连续单元组。
编辑:
我试过@mKorbel不错的SSCCE。它适用于列表,但似乎没有完全在表上工作。这是一个SSCCE:
import java.awt.Component;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TableSelection extends JFrame{
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
public TableSelection(){
JPanel main= new JPanel();
JTable table = new JTable(data, columnNames){
@Override
protected void processMouseEvent(MouseEvent e) {
int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
// change the modifiers to believe that control key is down
int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
// can I use this anywhere? I don't see how to change the modifiersEx of the MouseEvent
MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers, e.getX(),
e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getButton());
super.processMouseEvent(myME);
}
};
JScrollPane pane = new JScrollPane(table);
main.add(pane);
this.add(main);
this.setSize(800, 600);
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new TableSelection();
}
}
我可以选择非连续行而不是单个单元格。我的意思是,我希望能够选择单元格0,0和3,3。
答案 0 :(得分:9)
如果没有为JTable#setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
定义,那么CTRL + MOUSE_CLICK
或者你的意思是记得最后一次选择?
ListSelectionModel
由JTable
和JList
使用。
import java.awt.Component;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class Ctrl_Down_JList {
private static void createAndShowUI() {
String[] items = {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"};
JList myJList = new JList(items) {
private static final long serialVersionUID = 1L;
@Override
protected void processMouseEvent(MouseEvent e) {
int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
// change the modifiers to believe that control key is down
int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
// can I use this anywhere? I don't see how to change the modifiersEx of the MouseEvent
MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers, e.getX(),
e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getButton());
super.processMouseEvent(myME);
}
};
JFrame frame = new JFrame("Ctrl_Down_JList");
frame.getContentPane().add(new JScrollPane(myJList));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowUI();
}
});
}
答案 1 :(得分:7)
使用MULTIPLE_INTERVAL_SELECTION
中显示的How to Use Tables: User Selections。
附录:因为MULTIPLE_INTERVAL_SELECTION
的{{1}}也可用JList
,您可以利用后者的HORIZONTAL_WRAP
获取非连续选择,如图所示下方。
控制台:
[Cell:06] [Cell:06, Cell:16] [Cell:06, Cell:16, Cell:18] [Cell:06, Cell:08, Cell:16, Cell:18]
代码:
import java.awt.*;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.event.*;
/**
* @see http://stackoverflow.com/questions/7620579
* @see http://stackoverflow.com/questions/4176343
*/
public class ListPanel extends JPanel {
private static final int N = 5;
private DefaultListModel dlm = new DefaultListModel();
private JList list = new JList(dlm);
public ListPanel() {
super(new GridLayout());
for (int i = 0; i < N * N; i++) {
String name = "Cell:" + String.format("%02d", i);
dlm.addElement(name);
}
list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(N);
list.setCellRenderer(new ListRenderer());
list.addListSelectionListener(new SelectionHandler());
this.add(list);
}
private class ListRenderer extends DefaultListCellRenderer {
public ListRenderer() {
this.setBorder(BorderFactory.createLineBorder(Color.red));
}
@Override
public Component getListCellRendererComponent(JList list, Object
value, int index, boolean isSelected, boolean cellHasFocus) {
JComponent jc = (JComponent) super.getListCellRendererComponent(
list, value, index, isSelected, cellHasFocus);
jc.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
return jc;
}
}
private class SelectionHandler implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
System.out.println(Arrays.toString(list.getSelectedValues()));
}
}
}
private void display() {
JFrame f = new JFrame("ListPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ListPanel().display();
}
});
}
}