Java JTable按代码选择非连续单元格
我读了这个问题 本帖 这个帖子呢 采取行动守则Java JTable按代码选择非连续单元格,java,swing,jtable,cell,Java,Swing,Jtable,Cell,我读了这个问题 本帖 这个帖子呢 采取行动守则 table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); //table.setRowSelectionInterval(0, 2); //table.setColumnSelectionInterval(0, 1); table.setRowSelectionAllowed(false); table.setColumnSelectionAllowed(fa
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
//table.setRowSelectionInterval(0, 2);
//table.setColumnSelectionInterval(0, 1);
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
table.changeSelection(0, 0, true, false);
table.changeSelection(2, 2, true, false);
但我有:
我想这样做,但只使用代码,没有点击(对于我使用的图像点击)
如何仅使用代码(不单击)选择JTable的非连续单元格?编辑
在列表中填入所需的点
List<Point> selected = new ArrayList<>(Arrays.asList(new Point(0, 0), new Point(2, 2)));
List selected=newarraylist(Arrays.asList(新点(0,0),新点(2,2));
这是代码
public class TableSelection extends JFrame {
private static final long serialVersionUID = 1L;
String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
Object[][] data = {{"Kathy", "Smith", "Snowboarding", Integer.valueOf(5), Boolean.valueOf(false)},
{"John", "Doe", "Rowing", Integer.valueOf(3), Boolean.valueOf(true)},
{"Sue", "Black", "Knitting", Integer.valueOf(2), Boolean.valueOf(false)},
{"Jane", "White", "Speed reading", Integer.valueOf(20), Boolean.valueOf(true)},
{"Joe", "Brown", "Pool", Integer.valueOf(10), Boolean.valueOf(false)}};
public TableSelection() {
JPanel main = new JPanel();
JTable table = new JTable(data, columnNames) {
private static final long serialVersionUID = 1L;
List<Point> selected = new ArrayList<>(Arrays.asList(new Point(0, 0), new Point(2, 2)));
@Override
protected void processMouseEvent(MouseEvent e) {
if (e.getID() != MouseEvent.MOUSE_PRESSED) {
return;
}
int row = ((JTable) e.getSource()).rowAtPoint(e.getPoint());
int col = ((JTable) e.getSource()).columnAtPoint(e.getPoint());
if (row >= 0 && col >= 0) {
Point p = new Point(row, col);
if (selected.contains(p)) {
selected.remove(p);
} else {
selected.add(p);
}
}
((JTable) e.getSource()).repaint();
}
@Override
public boolean isCellSelected(int arg0, int arg1) {
return selected.contains(new Point(arg0, arg1));
}
};
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();
}
}
公共类表选择扩展JFrame{
私有静态最终长serialVersionUID=1L;
String[]columnNames={“名字”、“姓氏”、“运动”、“年岁”、“素食者”};
对象[][]数据={{“Kathy”,“Smith”,“Snowboarding”,Integer.valueOf(5),Boolean.valueOf(false)},
{“John”,“Doe”,“Rowing”,Integer.valueOf(3),Boolean.valueOf(true)},
{“Sue”,“Black”,“kniting”,Integer.valueOf(2),Boolean.valueOf(false)},
{“Jane”,“White”,“Speed reading”,Integer.valueOf(20),Boolean.valueOf(true)},
{“Joe”,“Brown”,“Pool”,Integer.valueOf(10),Boolean.valueOf(false)};
公共选举(){
JPanel main=新的JPanel();
JTable table=新的JTable(数据、列名){
私有静态最终长serialVersionUID=1L;
所选列表=新ArrayList(Arrays.asList(新点(0,0),新点(2,2));
@凌驾
受保护的void进程MouseEvent(MouseEvent e){
如果(e.getID()!=MouseEvent.MOUSE_按下){
返回;
}
int row=((JTable)e.getSource()).rowAtPoint(e.getPoint());
int col=((JTable)e.getSource()).columnAtPoint(e.getPoint());
如果(行>=0和列>=0){
点p=新点(行、列);
如果(选中。包含(p)){
选中。删除(p);
}否则{
选中。添加(p);
}
}
((JTable)e.getSource()).repaint();
}
@凌驾
已选择公共布尔值(int arg0,int arg1){
返回所选。包含(新点(arg0,arg1));
}
};
JScrollPane=新的JScrollPane(表);
main.add(窗格);
添加(主);
这个。设置大小(800600);
此.setVisible(true);
}
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
新选举();
}
}
编辑的答案
在列表中填入所需的点
List<Point> selected = new ArrayList<>(Arrays.asList(new Point(0, 0), new Point(2, 2)));
List selected=newarraylist(Arrays.asList(新点(0,0),新点(2,2));
这是代码
public class TableSelection extends JFrame {
private static final long serialVersionUID = 1L;
String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
Object[][] data = {{"Kathy", "Smith", "Snowboarding", Integer.valueOf(5), Boolean.valueOf(false)},
{"John", "Doe", "Rowing", Integer.valueOf(3), Boolean.valueOf(true)},
{"Sue", "Black", "Knitting", Integer.valueOf(2), Boolean.valueOf(false)},
{"Jane", "White", "Speed reading", Integer.valueOf(20), Boolean.valueOf(true)},
{"Joe", "Brown", "Pool", Integer.valueOf(10), Boolean.valueOf(false)}};
public TableSelection() {
JPanel main = new JPanel();
JTable table = new JTable(data, columnNames) {
private static final long serialVersionUID = 1L;
List<Point> selected = new ArrayList<>(Arrays.asList(new Point(0, 0), new Point(2, 2)));
@Override
protected void processMouseEvent(MouseEvent e) {
if (e.getID() != MouseEvent.MOUSE_PRESSED) {
return;
}
int row = ((JTable) e.getSource()).rowAtPoint(e.getPoint());
int col = ((JTable) e.getSource()).columnAtPoint(e.getPoint());
if (row >= 0 && col >= 0) {
Point p = new Point(row, col);
if (selected.contains(p)) {
selected.remove(p);
} else {
selected.add(p);
}
}
((JTable) e.getSource()).repaint();
}
@Override
public boolean isCellSelected(int arg0, int arg1) {
return selected.contains(new Point(arg0, arg1));
}
};
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();
}
}
公共类表选择扩展JFrame{
私有静态最终长serialVersionUID=1L;
String[]columnNames={“名字”、“姓氏”、“运动”、“年岁”、“素食者”};
对象[][]数据={{“Kathy”,“Smith”,“Snowboarding”,Integer.valueOf(5),Boolean.valueOf(false)},
{“John”,“Doe”,“Rowing”,Integer.valueOf(3),Boolean.valueOf(true)},
{“Sue”,“Black”,“kniting”,Integer.valueOf(2),Boolean.valueOf(false)},
{“Jane”,“White”,“Speed reading”,Integer.valueOf(20),Boolean.valueOf(true)},
{“Joe”,“Brown”,“Pool”,Integer.valueOf(10),Boolean.valueOf(false)};
公共选举(){
JPanel main=新的JPanel();
JTable table=新的JTable(数据、列名){
私有静态最终长serialVersionUID=1L;
所选列表=新ArrayList(Arrays.asList(新点(0,0),新点(2,2));
@凌驾
受保护的void进程MouseEvent(MouseEvent e){
如果(e.getID()!=MouseEvent.MOUSE_按下){
返回;
}
int row=((JTable)e.getSource()).rowAtPoint(e.getPoint());
int col=((JTable)e.getSource()).columnAtPoint(e.getPoint());
如果(行>=0和列>=0){
点p=新点(行、列);
如果(选中。包含(p)){
选中。删除(p);
}否则{
选中。添加(p);
}
}
((JTable)e.getSource()).repaint();
}
@凌驾
已选择公共布尔值(int arg0,int arg1){
返回所选。包含(新点(arg0,arg1));
}
};
JScrollPane=新的JScrollPane(表);
main.add(窗格);
添加(主);
这个。设置大小(800600);
此.setVisible(true);
}
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
新选举();
}
}