Jak sprawić, by JTable nie był edytowalny

Jak zrobić JTable nieedytowalną? Nie chcę, aby moi użytkownicy mogli edytować wartości w komórkach, klikając je dwukrotnie.

Author: ROMANIA_engineer, 2010-01-02

7 answers

Możesz użyć TableModel.

Zdefiniuj klasę taką jak ta:

public class MyModel extends AbstractTableModel{
    //not necessary
}

W rzeczywistości isCellEditable() jest false domyślnie, więc możesz go pominąć. (zobacz: http://docs.oracle.com/javase/6/docs/api/javax/swing/table/AbstractTableModel.html )

Następnie użyj metody setModel() twojego JTable.

JTable myTable = new JTable();
myTable.setModel(new MyModel());
 18
Author: JCasso,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-19 10:23:12

Możesz nadpisać metodę isCellEditable i zaimplementować jak chcesz na przykład:

//instance table model
DefaultTableModel tableModel = new DefaultTableModel() {

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

table.setModel(tableModel);

Lub

//instance table model
DefaultTableModel tableModel = new DefaultTableModel() {

   @Override
   public boolean isCellEditable(int row, int column) {
       //Only the third column
       return column == 3;
   }
};

table.setModel(tableModel);

uwaga Dla jeśli twój JTable znika

Jeśli Twoje JTable zniknie podczas używania tego najprawdopodobniej dlatego, że musisz użyć DefaultTableModel(Object[][] data, Object[] columnNames) konstruktor zamiast.

//instance table model
DefaultTableModel tableModel = new DefaultTableModel(data, columnNames) {

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

table.setModel(tableModel);
 137
Author: nelson eldoro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-19 20:08:18

Po prostu dodaj

table.setEnabled(false);
Dla mnie działa dobrze.
 36
Author: Siddhu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-16 13:06:46
table.setDefaultEditor(Object.class, null);
 24
Author: Oleg Mikhailov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-01 12:28:39

Jeśli tworzysz TableModel automatycznie z zestawu wartości (z "new JTable (Vector, Vector)"), być może łatwiej będzie usunąć edytory z kolumn:

JTable table = new JTable(my_rows, my_header);

for (int c = 0; c < table.getColumnCount(); c++)
{
    Class<?> col_class = table.getColumnClass(c);
    table.setDefaultEditor(col_class, null);        // remove editor
}

Bez edytorów dane nie będą edytowalne.

 8
Author: freesoft,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-13 14:13:16

Użyłem tego i zadziałało: jest to bardzo proste i działa dobrze.

JTable myTable = new JTable();
myTable.setEnabled(false);
 4
Author: user3518835,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-27 15:49:22

Utwórz nową klasę DefaultCellEditor:

public static class Editor_name extends DefaultCellEditor {
  public Editor_name(JCheckBox checkBox) {
   super(checkBox);
  }
  @Override
  public boolean isCellEditable(EventObject anEvent) {
    return false;
  }
}

I użyj setCellEditor:

JTable table = new JTable();
table.getColumn("columnName").setCellEditor(new Editor_name(new JCheckBox()));
 2
Author: Ehsan Jelodar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-23 08:50:01