單獨(dú)和不連續(xù)的JTable細(xì)胞選擇是否有任何干凈的方法允許用戶選擇JTable的多個(gè)非連續(xù)單元格?或者我被迫實(shí)現(xiàn)我自己的ListSelectionModel?我在JTable上使用setCellSelectionEnable()和setSelectionModel()方法,但是我只能選擇連續(xù)單元格的組。編輯:我試過(guò)@mKorbel尼斯SSCCE。對(duì)于List來(lái)說(shuō),它很好,但是它似乎不能完全處理表。下面是一個(gè)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);
}
};我可以選擇非連續(xù)行,但不能選擇單個(gè)單元格。我的意思是,我希望能夠選擇單元格0,0和3,3,例如。
單獨(dú)和不連續(xù)的JTable細(xì)胞選擇
臨摹微笑
2019-07-26 15:15:08