?
Solved

Setting specific columns of JTable as uneditable

Posted on 2003-11-17
3
Medium Priority
?
289 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
[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
  • 3
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 750 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

764 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