gpt4 book ai didi

java - 单独且不连续的 JTable 单元格选择

转载 作者:搜寻专家 更新时间:2023-11-01 01:06:57 25 4
gpt4 key购买 nike

是否有任何简洁的方法允许用户选择 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。

最佳答案

  1. 如果未为 JTable#setSelectionMode(ListSelectionModel.SINGLE_SELECTION) 定义,则 CTRL + MOUSE_CLICK

  2. 或者您的意思是记住上次选择的内容?

  3. ListSelectionModelJTableJList 使用。

enter image description here enter image description here enter image description here enter image description here enter image description here

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();
}
});
}

关于java - 单独且不连续的 JTable 单元格选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7620579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com