File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Help JTable update issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help JTable update issue" Watch "Help JTable update issue" New topic

Help JTable update issue

corn jew

Joined: Sep 06, 2003
Posts: 2
Hi all
I am playing around with JTable using the example from sun's TableDemo.The problem is that i have read through the tutorial ,but don't understand how the table updates it's data.I have written a method in the custom model class which extends AbstractTableModel to change the data when i call it from a button which calls that method,but nothing happens.I have also
tried to call tableData() method from that action ,but after clicking the
removeButton it gives me a blank table.Can anyone please explain to me
how these tables work with an example.

* is a 1.4 application that requires no other files.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;

* TableDemo is just like SimpleTableDemo, except that it
* uses a custom TableModel.
public class TableDemo extends JPanel implements ActionListener {
private boolean DEBUG = false;
MyTableModel mtm ;//= new MyTableModel();
JTable table = new JTable();
JButton rowButton;
JButton removeButton;

public TableDemo() {
super(new BorderLayout());
//super(new GridLayout(1,0));
mtm = new MyTableModel();

public void tableData(){
System.out.println("start of changeTableData");
//sets the size of the table to match the model which it contains
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

//Add the scroll pane to this panel.
System.out.println("end of changeTableData");


public void addButtons(){
System.out.println("add buttons ");

JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
rowButton = new JButton("select row");
removeButton = new JButton("remove row");


public void actionPerformed(ActionEvent ae){
if(ae.getSource() == rowButton){
int row = table.getSelectedRow();
System.out.println("row selected = "+row);
int column = table.getSelectedColumn();
System.out.println("column selected = "+column);
String value = (String)table.getValueAt(row,column);
System.out.println("cell selected = "+value);

Object[][] data = {
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true),new Integer(8),"banana"},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false),new Integer(9),"litchi"},


System.out.println("remove button clicked");
mtm = new MyTableModel();


class MyTableModel extends AbstractTableModel {
private String[] columnNames =
{"First Name","Last Name","Sport","# of Years","Vegetarian",

private Object[][] data = {
{"Gregory", "Adams",
"Snowboarding", new Integer(5), new Boolean(false),new Integer(5),"pear"},
{"Cornelius", "Jewell",
"Rowing", new Integer(3), new Boolean(true),new Integer(6),"apple"},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false),new Integer(7),"orange"},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true),new Integer(8),"banana"},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false),new Integer(9),"litchi"},


//to change table data
public void setData(Object[][] data)
{ = data;

//methods that you must implement
public int getColumnCount() {//these methods are called by JTable
return columnNames.length;
//methods that you must implement
public int getRowCount() {//these methods are called by JTable

return data.length;

public String getColumnName(int col) {
return columnNames[col];
//methods that you must implement table call this method to display data
public Object getValueAt(int row, int col) {//these methods are called by JTable
return data[row][col];

* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();

* Don't need to implement this method unless your table's
* editable.
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;

* Don't need to implement this method unless your table's
* data can change.
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");

data[row][col] = value;
fireTableCellUpdated(row, col);//for cell changes

if (DEBUG) {
System.out.println("New value of data:");

private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();

for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);

* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
public static void createAndShowGUI() {
//Make sure we have nice window decorations.

//Create and set up the window.
JFrame frame = new JFrame("TableDemo");

//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
//newContentPane.setOpaque(true); //content panes must be opaque

//Display the window.

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {

Thanks for all your help.

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535

[ July 27, 2004: Message edited by: Craig Wood ]
I agree. Here's the link:
subject: Help JTable update issue
It's not a secret anymore!