在我的 JTable 中,我想使用右键单击选项重命名表标题。右键单击任何标题,它提供了重命名标题的选项。但是当我这样做时,它会重命名所选列的标题。所以我必须先左键单击该列,然后才能进一步处理。有什么方法可以让我在任何列上单击鼠标右键,它也会被选中,以便在重命名后它的值被重命名。
我的动作监听器如下:
int column = jTable1.getSelectedColumn();
if (column == -1) {
return;
}
if (column == 0) {
JOptionPane.showMessageDialog(jTable1, "You can not rename this column.");
return;
}
boolean blag = true;
String sColumnName = null;
while (blag) {
sColumnName = (String) JOptionPane.showInputDialog(jTable1, "Enter Column Name", "Add Column", JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (sColumnName == null) {
return;
}
if (sColumnName.trim().equalsIgnoreCase("")) {
JOptionPane.showMessageDialog(jTable1, "Column name can not be blank.");
blag = true;
} else {
blag = false;
}
Vector vColumns = new Vector();
for (int i = 0; i < defaultTableModel.getColumnCount(); i++) {
vColumns.add(defaultTableModel.getColumnName(i));
}
if (vColumns.contains(sColumnName)) {
JOptionPane.showMessageDialog(jTable1, "Column name " + sColumnName + " already exist.");
return;
}
}
int viewColumn = jTable1.convertColumnIndexToView(column);
int totalColumn = defaultTableModel.getColumnCount();
Object[] headers = new Object[totalColumn];
for (int i = 0; i < totalColumn; i++) {
if (i == column) {
headers[i] = sColumnName;
} else {
headers[i] = defaultTableModel.getColumnName(i);
}
}
defaultTableModel.setColumnIdentifiers(headers);