This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes JTable help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable help" Watch "JTable help" New topic

JTable help

Eric Eichler

Joined: Nov 18, 2003
Posts: 5
I'm using a Table Model to create a JTable. I only want two of the columns editable. When a user types information in the SKU column, the other cells on that row will be automatically populated with data pertaining to that SKU. My problem is that since I only set the two columns as editable, now setValueAt(int row, int column) doesn't work for the remaining columns. Any tips on how I can force data into these cells? Thanks. Code:
import java.awt.Point;
import java.util.Hashtable;
import javax.swing.event.*;
import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class POSTableModel extends AbstractTableModel
private Hashtable lookup;
private final int rows;
private final int columns;
private final String headers[];
public POSTableModel(int rows, String columnHeaders[])
if ((rows < 0) || (columnHeaders == null))
throw new IllegalArgumentException("Invalid row count/columnHeaders");
this.rows = rows;
this.columns = columnHeaders.length;
headers = columnHeaders;
lookup = new Hashtable();

public boolean isCellEditable(int row, int column)
if(getColumnName(column).equals("SKU") || getColumnName(column).equals("Quantity"))
return true;
return false;
//return getColumnName(column).equals("SKU");
public int getColumnCount()
return columns;
public int getRowCount()
return rows;
public String getColumnName(int column)
return headers[column];
public Object getValueAt(int row, int column)
return lookup.get(new Point(row, column));
public void setValueAt(Object value, int row, int column)
if ((rows < 0) || (columns < 0))
throw new IllegalArgumentException("Invalid row/column setting");
if ((row < rows) && (column < columns))
lookup.put(new Point(row, column), value);
other class file:
Object rowData[][] = new Object[50][6];
for(int i = 0; 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 , "Date", "SKU", "Quantity",
"Description", "Unit Price"};

TableModel model = new POSTableModel(50, columnNames);
posTable = new JTable(model);
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
I'm by no means an expert with Jtables but try this it might work or give you a start on a possible solution. - I hope. Set the defaultCellEditor of the cells/columns as a JTextField and set the TextField as uneditable. You should still be able to change the data with the setText method but the user shouldn't be able to do it in the table. If that doesn't work you can try to listen for keyboard events in the cells and just not do anything when the user types in the cells.
Hope one of those helps out

I agree. Here's the link:
subject: JTable help