gpt4 book ai didi

java - 选择仅限于 2 行的 JTable

转载 作者:行者123 更新时间:2023-11-30 11:11:05 27 4
gpt4 key购买 nike

我想将 JTable 中的行选择限制为只有 2 行。如果用户尝试选择第三行(Ctrl + 单击),表中最旧的选择应以编程方式取消选择。为此,我在表中添加了一个 ListSelectionListener。在以下示例中,"clearSelection()" 似乎不起作用。

如果有更简单或优雅的方法来实现同样的目标,请提出建议。

package test;

import java.util.Arrays;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JTableDemo {
public static void main(String[] argv) {
Vector rowData = new Vector();
for (int i = 0; i < 10; i++) {
Vector colData = new Vector(Arrays.asList("row" + (i + 1)));
rowData.add(colData);
}

String[] columnNames = { "header" };

Vector columnNamesV = new Vector(Arrays.asList(columnNames));

JTable table = new JTable(rowData, columnNamesV);
table.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setColumnSelectionAllowed(false);
SelectionListener listener = new SelectionListener(table);
table.getSelectionModel().addListSelectionListener(listener);

JFrame f = new JFrame();
f.setSize(300, 300);
f.add(new JScrollPane(table));
f.setVisible(true);
}

private static class SelectionListener implements ListSelectionListener {
JTable table;
int prevSelectedRow;
int oldSelectedRow = -1;

SelectionListener(JTable table) {
this.table = table;
}

public void valueChanged(ListSelectionEvent e) {

if (e.getValueIsAdjusting()) {
return;
}
if (e.getSource() == table.getSelectionModel() && table.getRowSelectionAllowed()) {

int[] selectedRows = table.getSelectedRows();

if (selectedRows.length == 1) {
prevSelectedRow = selectedRows[0];
return;
}

if (selectedRows.length > 1) {
int currentSelectedRow = -1;
System.out.println("The first and last indexes are : " + e.getFirstIndex() + ", " + e.getLastIndex());

if (oldSelectedRow == -1) {
for (int i = 0; i < selectedRows.length; i++) {
if (prevSelectedRow != selectedRows[i]) {
currentSelectedRow = selectedRows[i];
break;
}
}
table.getSelectionModel().removeListSelectionListener(this);
table.clearSelection();
table.setRowSelectionInterval(prevSelectedRow, prevSelectedRow);
oldSelectedRow = prevSelectedRow;
prevSelectedRow = currentSelectedRow;
table.addRowSelectionInterval(currentSelectedRow, currentSelectedRow);
table.getSelectionModel().addListSelectionListener(this);
} else {
for (int i = 0; i < selectedRows.length; i++) {
if (prevSelectedRow != selectedRows[i] && oldSelectedRow != selectedRows[i]) {
currentSelectedRow = selectedRows[i];
break;
}
}

table.getSelectionModel().removeListSelectionListener(this);
table.clearSelection();
table.setRowSelectionInterval(prevSelectedRow, prevSelectedRow);
oldSelectedRow = prevSelectedRow;
prevSelectedRow = currentSelectedRow;
table.addRowSelectionInterval(currentSelectedRow, currentSelectedRow);
table.getSelectionModel().addListSelectionListener(this);
}

}

}

}
}
}

最佳答案

Please advice if there is a simpler or elegant way to achieve the same.

您可以扩展 DefaultListSelectionModel 并覆盖 addSelectionInterval(...) 方法。然后将自定义选择模型添加到表中。

关于java - 选择仅限于 2 行的 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27546977/

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