This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming 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

Help JTable update issue

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


/*
* TableDemo.java 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();
addButtons();
tableData();
}

public void tableData(){
System.out.println("start of changeTableData");
//sets the size of the table to match the model which it contains
table.setModel(this.mtm);
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.
add(scrollPane,BorderLayout.CENTER);
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");
rowButton.addActionListener(this);
removeButton = new JButton("remove row");
removeButton.addActionListener(this);
jp.add(rowButton);
jp.add(removeButton);
add(jp,BorderLayout.SOUTH);

}

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);

}
else{
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();
mtm.setData(data);

}

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

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)
{
this.data = data;
fireTableDataChanged();
}

//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:");
printDebugData();
}
}

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]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}

/**
* 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.
JFrame.setDefaultLookAndFeelDecorated(true);

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

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

//Display the window.
frame.pack();
frame.setVisible(true);
}

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() {
createAndShowGUI();
}
});
}
}

Thanks for all your help.

Cheers.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ July 27, 2004: Message edited by: Craig Wood ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!