Solved

Setting specific columns of JTable as uneditable

Posted on 2003-11-17
3
266 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 104
starter POM and spring-boot-starter,  spring-boot-web 2 53
VB Script to add site to Java Exception List 4 73
hibernate jars 4 26
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

816 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

8 Experts available now in Live!

Get 1:1 Help Now