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:
- 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.
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);
to the jTable
table.setRowSelectionAllowed(true);
Make Jtable Noneditable without using setModel()
Here are 2 ways to achieve that:
Create and use your own
TableModel
implementation which forwards all calls to the table model returned byDbUtils
except forisCellEditable()
in which you can return alwaysfalse
hence disabling editing. Your own table model could get the model returned byDbUtils
as a constructor argument for example.You can extend
JTable
and override itsisCellEditable()
method to returnfalse
(by default it calls the model'sisCellEditable()
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
Using File.Listfiles with Filenameextensionfilter
Getting Java Gui to Open a Webpage in Web Browser
Android - How to Take Screenshot Programmatically
Is Custom Enum Serializable Too
Java/Android - How to Print Out a Full Stack Trace
What Is the Easiest Way to Get the Current Day of the Week in Android
Android.Content.Res.Resources$Notfoundexception
Android - How to Replace Part of a String by Another String
How to Get Count of Number Methods Used in a Jar File
Why Doesn't Java Throw an Exception When Dividing by 0.0
How to Scroll More Than One Object at the Same Time
Is It Possible/How to Embed and Access HTML Files in a Jar
Map.Clear() VS New Map:Which One Will Be Better
What's a Good Library for Parsing Mathematical Expressions in Java
Android - Activity Constructor VS Oncreate