Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
To set a column of JTable uneditable?

Subject: To set a column of JTable uneditable?

From: Lihua Pan

Date: 27 Jan, 2009 12:59:02

Message: 1 of 9

I have used a JTable on the Matlab GUI, but if I want to make one column of JTable uneditable, it can not use the normal way under Java, that is, to override the isCellEditable() method. Is there any alternative to solve the problem?
Thanks a lot!

Subject: To set a column of JTable uneditable?

From: Yair Altman

Date: 13 Feb, 2009 12:28:02

Message: 2 of 9

"Lihua Pan" <plhalice@gmx.de> wrote in message <gln0em$633$1@fred.mathworks.com>...
> I have used a JTable on the Matlab GUI, but if I want to make one column of JTable uneditable, it can not use the normal way under Java, that is, to override the isCellEditable() method. Is there any alternative to solve the problem?
> Thanks a lot!

hTable.setEditable(colIdx,0); % 0=non-editable; 1=editable

Note: colIdx starts at 1 for Matlab-based hTable handles & 0 for java handles. If you have no clue what I mean then simply try numbers until it works for you (easier than to explain here...)

Yair altman

Subject: To set a column of JTable uneditable?

From: Lihua Pan

Date: 25 Feb, 2009 16:14:02

Message: 3 of 9

Hey Yair, thanks very much! Really nice of you!! I've just come back to work, and suddenly see so many replies from you! In fact I've seen your name for many times!
About the reply,

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <gn3p0h$k0q$1@fred.mathworks.com>...
> "Lihua Pan" <plhalice@gmx.de> wrote in message <gln0em$633$1@fred.mathworks.com>...
> > I have used a JTable on the Matlab GUI, but if I want to make one column of JTable uneditable, it can not use the normal way under Java, that is, to override the isCellEditable() method. Is there any alternative to solve the problem?
> > Thanks a lot!
>
> hTable.setEditable(colIdx,0); % 0=non-editable; 1=editable
I've tried this with matlab handle it did work but with javax.swing.JTable there was no appropriate method or public field setEditable. I haven't exactly understand what you mean.


>
> Note: colIdx starts at 1 for Matlab-based hTable handles & 0 for java handles. If you have no clue what I mean then simply try numbers until it works for you (easier than to explain here...)
>
> Yair altman

Subject: To set a column of JTable uneditable?

From: Arda

Date: 23 Aug, 2011 13:16:10

Message: 4 of 9

"Lihua Pan" wrote in message <gln0em$633$1@fred.mathworks.com>...
> I have used a JTable on the Matlab GUI, but if I want to make one column of JTable uneditable, it can not use the normal way under Java, that is, to override the isCellEditable() method. Is there any alternative to solve the problem?
> Thanks a lot!

I am using the same java object in my gui and couldnt find a way to make my table uneditable. In java it is fairly easy, just extending or overriding the DefaultTableModel such that isCellEditable returns false, works beautifully. But how can i do it in Matlab? Is it possible to override? I desperately hope that someone has an answer...
 


Here is a simple code to construct a java table;

> frame=javax.swing.JFrame('table');
> model=javax.swing.table.DefaultTableModel({true,'a';false,'b'},{'lgcl','str'});
>% i need to override "isCellEditable"
> table=javax.swing.JTable(model);
> sorter=javax.swing.table.TableRowSorter(model);
> table.setRowSorter(sorter);
> pane=javax.swing.JScrollPane(table);
> frame.add(pane,java.awt.BorderLayout.CENTER);
> frame.setSize(300,250);
> frame.setVisible(true);

Subject: To set a column of JTable uneditable?

From: Yair Altman

Date: 23 Aug, 2011 18:24:27

Message: 5 of 9

"Arda " <ardaaksu@hotmail.com> wrote in message <j3096q$n9$1@newscl01ah.mathworks.com>...
> "Lihua Pan" wrote in message <gln0em$633$1@fred.mathworks.com>...
> > I have used a JTable on the Matlab GUI, but if I want to make one column of JTable uneditable, it can not use the normal way under Java, that is, to override the isCellEditable() method. Is there any alternative to solve the problem?
> > Thanks a lot!
>
> I am using the same java object in my gui and couldnt find a way to make my table uneditable. In java it is fairly easy, just extending or overriding the DefaultTableModel such that isCellEditable returns false, works beautifully. But how can i do it in Matlab? Is it possible to override? I desperately hope that someone has an answer...


In Java tables there is absolutely no need for extending the model etc. You can simply use jtable.setEditable(columnIndex,true/false) as I have noted above.

In Matlab's uitable (at least the new version that is available since R2008a), use the ColumnEditable property:
http://www.mathworks.com/help/techdoc/ref/uitableproperties.html#brkie07-1

Yair Altman
http://UndocumentedMatlab.com

Subject: To set a column of JTable uneditable?

From: Arda

Date: 23 Aug, 2011 19:09:28

Message: 6 of 9

"Yair Altman" wrote in message <j30r8r$3bj$1@newscl01ah.mathworks.com>...
> In Java tables there is absolutely no need for extending the model etc. You can simply use jtable.setEditable(columnIndex,true/false) as I have noted above.
>
> In Matlab's uitable (at least the new version that is available since R2008a), use the ColumnEditable property:
> http://www.mathworks.com/help/techdoc/ref/uitableproperties.html#brkie07-1
>
> Yair Altman
> http://UndocumentedMatlab.com

Yair, thanks for the reply. I tried both "javax.swing.Jtable" and "com.jidesoft.grid.SortableTable" but neither of them has setEditable method. Which java table are you referring?

Subject: To set a column of JTable uneditable?

From: Arda

Date: 23 Aug, 2011 19:14:27

Message: 7 of 9

I forgot to mention, i dont want to use an uitable. Neither the old one nor the new.. Both have serious disadvantages for me so i need to built it with Java.

Subject: To set a column of JTable uneditable?

From: Yair Altman

Date: 31 Aug, 2011 00:04:29

Message: 8 of 9

"Arda " <ardaaksu@hotmail.com> wrote in message <j30u6j$dus$1@newscl01ah.mathworks.com>...
> I forgot to mention, i dont want to use an uitable. Neither the old one nor the new.. Both have serious disadvantages for me so i need to built it with Java.

Here is a simple solution that works with *ALL* java-based tables in Matlab - both the old and the new uitable, as well as plain-ol'-JTable:

  ce = javax.swing.DefaultCellEditor(javax.swing.JTextField);
  ce.setClickCountToStart(intmax); % =never...
  jTable.getColumnModel.getColumn(columnIndex).setCellEditor(ce);

where columnIndex starts at 0 for the left-most column (as is normal for Java indices).

Yair Altman
http://UndocumentedMatlab.com

Subject: To set a column of JTable uneditable?

From: Arda

Date: 1 Sep, 2011 19:18:10

Message: 9 of 9

"Yair Altman" wrote in message <j3jtqd$2cr$1@newscl01ah.mathworks.com>...
> Here is a simple solution that works with *ALL* java-based tables in Matlab - both the old and the new uitable, as well as plain-ol'-JTable:
>
> ce = javax.swing.DefaultCellEditor(javax.swing.JTextField);
> ce.setClickCountToStart(intmax); % =never...
> jTable.getColumnModel.getColumn(columnIndex).setCellEditor(ce);
>
> where columnIndex starts at 0 for the left-most column (as is normal for Java indices).
>
> Yair Altman
> http://UndocumentedMatlab.com

@Yair, thanks for the reply. However your solution is not a complete one. With it it can be prevented to get in the cell but table is still editable with a key press. Actually following code is more appropriate;
 
 sclass=java.lang.String('').getClass;
 renderer=table.getDefaultRenderer(sclass);
 editor=table.getDefaultEditor(sclass);
 editor.getComponent.setEditable(0);
 editor.setClickCountToStart(20);
 table.getColumnModel.getColumn(0).setCellRenderer(renderer)
 table.getColumnModel.getColumn(0).setCellEditor(editor)

But, unfortunately but, even though table can be made uneditable with this approach, when a keyboard key is pressed selected cell changes color.

Anyway, this is something right? :)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us