This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Machine Learning with R: Expert techniques for predictive modeling and have Brett Lantz on-line!
See this thread for details.
Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!