How to Make a Jtable Non-Editable

How to make a JTable non-editable

You can use a TableModel.

Define a class like this:

public class MyModel extends AbstractTableModel{
//not necessary
}

actually isCellEditable() is false by default so you may omit it. (see: http://docs.oracle.com/javase/6/docs/api/javax/swing/table/AbstractTableModel.html)

Then use the setModel() method of your JTable.

JTable myTable = new JTable();
myTable.setModel(new MyModel());

How to make a jtable not editable in java?

 private TableModel model = new DefaultTableModel(data, columnNames)
{
public boolean isCellEditable(int row, int column)
{
return false;//This causes all cells to be not editable
}
};
private JTable table = new JTable(model);

Edited.
If you are doing this in Netbeans IDE designer, follow the steps below:

  • Select the form on which the JTable is placed
  • From the Navigation Pane, expand JScrollPane and right-click on JTable and Select Customize Code as shown below:

Navigator Pane

  • On the code customizer, select the second drop down and choose custom property. This enables you to edit the DefaultTableModel code definition.
  • Now paste this:
    {public boolean isCellEditable(int row, int column){return false;}} before the last closing blacket );

Your final setup should look as shown below:

  • Press ok to save - and job done.

Code Customizer

how to make JTable cell non editable but should be able to select and copy the value in current cell

when i try to copy the entire row is getting selected I want to copy only the selected cell value instead of entire row

The default Action for the Ctrl+C key is to copy the entire row. If you only want the data of the currently selected cell then you need to replace the default Action with a custom Action.

The logic would be something like:

Action copyCell = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
JTable table = (JTable)e.getSource();
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
Object value = table.getValueAt(row, column);

// copy the data to the clipboard

Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection testData = new StringSelection( value.toString() );
c.setContents(testData, testData);
}
};

table.getActionMap().put("copy", copyCell);

The above code will create the custom Action and replace it in the ActionMap of the JTable. See Key Bindings. The program provided in the link shows all the default Actions and the keyword for each Action.

How to make jTable contents clickable but non-editable - java netbeans

Here are some sources that can help you solve your problem. If you cannot find the exact code of the jTable, you can refer to the first link. Otherwise, you can refer to the second link.

Link 1: Customizing code by adding isCellEditable

Link 2: Adding table.setFocusable(false);
table.setRowSelectionAllowed(true);
to the jTable

Make Jtable Noneditable without using setModel()

Here are 2 ways to achieve that:

  1. Create and use your own TableModel implementation which forwards all calls to the table model returned by DbUtils except for isCellEditable() in which you can return always false hence disabling editing. Your own table model could get the model returned by DbUtils as a constructor argument for example.

  2. You can extend JTable and override its isCellEditable() method to return false (by default it calls the model's isCellEditable() method). Maybe other Swing enthusiasts will see this as an evil hack, but it is the simplest solution to your problem here.

Elaborating method #1

This is how you can create your model:

class MyModel implements TableModel {
private final TableModel m;
public MyModel(TableModel m) {
this.m = m;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
// This is how we disable editing:
return false;
}

// The rest of the methods just forward to the other model:

@Override
public int getRowCount() {
return m.getRowCount();
}

@Override
public int getColumnCount() {
return m.getColumnCount();
}

// ...and all other methods which I omit here...
}

And this is how you can use it:

jTable1.setModel(new MyModel(DbUtils.resultSetToTableModel(rs)));

Elaboration of method #2

Extending JTable can even be an anonymous class:

JTable jtable1 = new JTable() {
@Override
public boolean isCellEditable(int row, int column) {
// This is how we disable editing:
return false;
}
};

And using it:

// You can set any model, the table will not be editable because we overrode
// JTable.isCellEditable() to return false therefore the model will not be asked
// if editable.

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

DefaultTableModel make cell not editable JTable

You should not subclass the JTable itself, but the table model:

DefaultTableModel myModel = new DefaultTableModel(...) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
}

Or even better, don't use a DefaultTableModel, and use an AbstractTableModel that directly gets the information in your business objects rather than copying all the information from the business objects to Vectors.



Related Topics



Leave a reply



Submit