• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

JTable help

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
else
{
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);
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric
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
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic