Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

None editable cells in JTable

Posted on 1999-07-20
9
Medium Priority
?
790 Views
Last Modified: 2008-03-06
Hi,
Using a JTable with DefaultTableModel, I don't want to allow a user to edit any cell.
How do I do that?
I know there is a methode isCellEditable(int, int), but is there any setCellEditable(int, int), or something like this???
0
Comment
Question by:s_lavie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 2

Expert Comment

by:mwibbels
ID: 1246531
There's no setCellEditable(int, int), you have to create a subclass of DefaultTableModel and override isCellEditable(int, int) to return false.
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1246532
It looks a little strange, don't you think?
There is a method isCellEditable(int, int) that returns a boolean i.e. true or false. Assuming that true is the default, shouldn't there be a nice and easy way to set that value?????
0
 
LVL 2

Expert Comment

by:mwibbels
ID: 1246533
I guess the idea here is that for a lot of tables tables some cells are editable while others are not (at least when I use them). If there would be a setCelEditable(int, int, boolean) you would have to call that method for every cell in the model.

The only way to change the setting is to override the DefaultTableModel.

0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 2

Author Comment

by:s_lavie
ID: 1246534
Ok, I agree that setCelEditable(int, int, boolean) to each cell dosn't make sense, but at least setTableEditable() to the whole table...
0
 

Expert Comment

by:roslyn
ID: 1246535
private void buildTable()
{
   myTableModel = new AbstractTableModel()
   {
      public int getRowCount()
      {
         return myTableData.size();
      }

      public int getColumnCount()
      {
         return myHeaders.length;
      }

      public Object getValueAt(int row, int col)
      {
         Object[] entry = (Object[])myTableData.elementAt(row);
         return entry[col];
      }

      public String getColumnName(int col)
      {
          return myHeaders[col];
      }

      public boolean isCellEditable(int row, int col)
      {
         return false;
      }

      public void setValueAt(Object aValue, int row, int col)
      {
         Object[] entry = (Object[])myTableData.elementAt(row);
         entry[col] = aValue;
         myTableData.setElementAt(entry,row);
      }
   };
}

the function isCellEditable returning false prevents all table cells from being edited by the user.  i know you asked for the default model but it inherits the abstract model.  this is what i use for all my basic tables.  you set cells editable by specifing the return value.

ex.

isCellEditable func().
if(row == 2)
  return true;
else
   return false;
0
 

Expert Comment

by:roslyn
ID: 1246536
myTableData being a Vector;
myHeaders being a String[],
myColSizes being an int[]
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1246537
Hi roslyn,
Sorry but as far as I understand, your suggestion is a much more comlicated than mwibbels's, since in his suggestion I have to override only the methode isCellEditable(int, int), while in yours I have to do much more work.
Unless I got you wrong I'll have to reject your answer and accept mwibbels's, just to be fair...
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1246538
mwibbels, I wish to give you points - post an answer :-)

Sorry roslyn, his comment was better for my needs, and earlier... :-(
0
 
LVL 2

Accepted Solution

by:
mwibbels earned 200 total points
ID: 1246539
Thanks!
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question