Solved

Setting specific columns of JTable as uneditable

Posted on 2003-11-17
3
259 Views
Last Modified: 2008-02-26
Hello.  I would like to set my SKU column of my JTable as the only editable column.  When the user puts in the SKU, the other cells in the row will automatically be filled in (different issue).  How can I set all of the columns except the SKU column as uneditable?  It would be a bonus to have the uneditable cells white instead of gray.  Thanks.

Here is the code I used to create the JTable with blank cells:

Object rowData[][] = new Object[50][5];
for(int i = 1; i < rowData.length; i++)
{
      rowData[i][0]= String.valueOf(i);
      for(int j = 1; j < rowData[i].length; j++)
      {
            rowData[i][j] = "";
      }
}

String columnNames[] = {"Line Number", "Quantity", "SKU", "Description", "Unit Price"};
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
             
0
Comment
Question by:ike2010
  • 3
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9768140
You'll need to use a custom table model:

public class MyTableModel extends DefaultTableModel
{
   public MyTableModel(Vector data, Vector columns)
   {
      super(data, columns);
   }

   public boolean isCellEditable(int row, int column)
   {
      return column==2;
   }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9768492
  public boolean isCellEditable(int row, int column)
   {
      return getColumnName(column).equals("SKU");
   }
0
 
LVL 92

Expert Comment

by:objects
ID: 9774389
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now