简体   繁体   English

具有现有TableModel的不可编辑JTable

[英]Non editable JTable with existing TableModel

I know I can make JTable not editable by override isCellEditable() function like that: 我知道我可以通过覆盖isCellEditable()函数使JTable不可编辑:

DefaultTableModel tableModel = new DefaultTableModel() {
    @Override
    public boolean isCellEditable(int row, int column) {
       //all cells false
       return false;
    }
};

table.setModel(tableModel);

but I am using someone else's class (which I can't change) that generates TableModel for me. 但我正在使用别人的类(我无法改变)为我生成TableModel。

Is there any other way that I can achieve non-editable JTable ? 有没有其他方法可以实现不可编辑的JTable?

EDIT: 编辑:

I want non-editable JTable but I want to keep row selection (highlight whole row) 我想要不可编辑的JTable,但我想保持行选择(突出显示整行)

EDIT 2: 编辑2:

This is code that worked for me: 这是适合我的代码:

table.setModel(DbUtils.resultSetToTableModel(rs));

for (Class c: Arrays.asList(Object.class, Number.class, Boolean.class)) {
    TableCellEditor ce = table.getDefaultEditor(c);
    if (ce instanceof DefaultCellEditor) {
            ((DefaultCellEditor) ce).setClickCountToStart(Integer.MAX_VALUE);
    }
}

Is there any other way that I can achieve non-editable JTable ? 有没有其他方法可以实现不可编辑的JTable?

Override the isCellEditable(...) method of JTable : 覆盖JTableisCellEditable(...) method

JTable table = new JTable( tableModel )
{
    @Override
    public boolean isCellEditable(int row, int column)
    {
        return false;
    }
};

From the JTable API it can be seen that there are several methods that can be used to control a table's selection behavior: JTable API可以看出,有几种方法可用于控制表的选择行为:

setCellSelectionEnabled(boolean cellSelectionEnabled);
setColumnSelectionAllowed(boolean val);
setRowSelectionAllowed(boolean rowSelectionAllowed);

You can look up the specific descriptions to see if what you want is achieved with any combination of those. 您可以查找具体描述,以查看是否可以通过这些描述实现所需。 From the same API it can also be seen that JTable inherits the method 从同一个API中也可以看出JTable继承了该方法

setEnabled(boolean enabled);

which is described as Sets whether or not this component is enabled. 描述为设置是否启用此组件。 A component that is enabled may respond to user input, while a component that is not enabled cannot respond to user input. 启用的组件可能会响应用户输入,而未启用的组件则无法响应用户输入。 Some components may alter their visual representation when they are disabled in order to provide feedback to the user that they cannot take input. 某些组件在被禁用时可能会改变其可视化表示,以便向用户提供他们无法接受输入的反馈。

Also, if you cannot change the model class, but you are not forced to use the exact model, you might be able to create a new class extending from it that overrides the isCellEditable method and set this model to the tables. 此外,如果您无法更改模型类,但不强制使用精确模型,则可以创建一个从其中扩展的新类,该类将覆盖isCellEditable方法并将此模型设置为表。

Here is another approach: 这是另一种方法:

  • table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE)
  • disable a startEditing action 禁用startEditing操作
  • DefaultCellEditor#setClickCountToStart(Integer.MAX_VALUE)
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class NonEditableButKeepRowSelectionTableTest {
  public JComponent makeUI() {
    String[] columnNames = {"String", "Integer", "Boolean"};
    Object[][] data = {
      {"aaa", 12, true}, {"bbb", 5, false},
      {"CCC", 92, true}, {"DDD", 0, false}
    };
    DefaultTableModel model = new DefaultTableModel(data, columnNames) {
      @Override public Class<?> getColumnClass(int column) {
        return getValueAt(0, column).getClass();
      }
    };
    JTable table = new JTable(model);
    //table.setEnabled(false);

    table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);

    table.getActionMap().put("none", new AbstractAction() {
      @Override public void actionPerformed(ActionEvent e) {
        System.out.println("dummy");
      }
    });
    table.getInputMap().put(KeyStroke.getKeyStroke("pressed F2"), "none");

    for (Class c: Arrays.asList(Object.class, Number.class, Boolean.class)) {
      //TEST: table.setDefaultEditor(c, null);
      TableCellEditor ce = table.getDefaultEditor(c);
      if (ce instanceof DefaultCellEditor) {
        ((DefaultCellEditor) ce).setClickCountToStart(Integer.MAX_VALUE);
      }
    }
    JPanel p = new JPanel(new BorderLayout());
    p.add(new JScrollPane(table));
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new NonEditableButKeepRowSelectionTableTest().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM