The Artima Developer Community
Sponsored Link

Java Answers Forum
Table does not get updated

2 replies on 1 page. Most recent reply: Jun 23, 2004 5:45 AM by Sam Li

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Sam Li

Posts: 5
Nickname: sam2004
Registered: Jun, 2004

Table does not get updated Posted: Jun 22, 2004 12:33 AM
Reply to this message Reply
Advertisement
Hello, there
I have got a problem about JTable. I used DefaultTableModel to create a table, and the table is displayed properly. When I try to add or delete a row, it just does not update my table. Here is the code:

class tabletest extends JFrame {


String dataValues[][] = new String[4][2];
String columnNames[] = {"tag name", "description"};

/* some codes are missing here ; they are for buttons, textfield etc.*/

/* use for loops to fill in dataValues[][].*/

DefaultTableModel tableModel = new DefaultTableModel(dataValues, columnNames);
JTable table = new JTable(tableModel);


lower_panel.add (new JScrollPane(table), "Center");


}


The above code can display the table, but when I try to add a row or remove a row , it does not work(table is not updated.)


These codes are somewhere in the class tabletest:

add_row_button.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {

String temp[] = {"a new row", " "};


DefaultTableModel t = (DefaultTableModel) table.getModel();
t.addRow(temp);
//t.fireTableChanged(null);
t.fireTableDataChanged();
//table.invalidata;
//table.validate;
//table.repaint();
//table.validate();
//table.invalidate();
//table.tableChanged();
table.repaint();
}
}

Codes for removing a row:

remove_row_button.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent e) {

int index = table.getSelectedRow();
if (index != -1)
{

((DefaultTableModel)table.getModel()).removeRow(index);

}


}

});


You can see that I try many ways to update my table, but all failed.So I suspect that : either addRow(), removeRow() do not cause any effect to the tablemodel, or I have not found the right way to update the change of the table.

Will appreciate anyone's help.

Sam


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Table does not get updated Posted: Jun 23, 2004 3:14 AM
Reply to this message Reply
I dont know why it is not working for u.

for me it works

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
 
class TableTest extends JFrame 
{
 
	String dataValues[][] = new String[4][2];
	String columnNames[] = {"tag name", "description"};
 
 
	DefaultTableModel tableModel = new DefaultTableModel(dataValues, columnNames);
	JButton addButton =  new JButton("ADD");
	JButton removeButton =  new JButton("Remove");	
	JTable table = new JTable(tableModel);
 
	public TableTest() 
	{
		super("test");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add (new JScrollPane(table), BorderLayout.CENTER);
 
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 
		// remove button definition
		buttonPanel.add(addButton);
		removeButton.addActionListener( new ActionListener()
		                               {
		                                  public void actionPerformed(ActionEvent e)
		                                  {
		                                     if (table.getSelectedRow() != -1)
		                                        ((DefaultTableModel)table.getModel()).removeRow(table.getSelectedRow());
		                                  }
		                               });
 
		// add button definition
		buttonPanel.add(removeButton);
		addButton.addActionListener( new ActionListener()
		                            {
		                               public void actionPerformed(ActionEvent e)
		                               {
		                                  ((DefaultTableModel)table.getModel()).addRow(new Object [] { "Added row column1", ""} );
		                               }
		                            });
 
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
 
	}
 
	public static void main(String args[]) 
	{
		TableTest t = new TableTest();
		t.show();
		t.pack();
	}
}

Sam Li

Posts: 5
Nickname: sam2004
Registered: Jun, 2004

Re: Table does not get updated Posted: Jun 23, 2004 5:45 AM
Reply to this message Reply
Thank you very much, mausam. I try your code and it works fine. Actually, the code I provided is only a part of my class(it is a big one), so maybe something wrong with the rest of my class. I will keep working on it. Thank you again.

Sam

Flat View: This topic has 2 replies on 1 page
Topic: UI developement standards using Struts tiles Previous Topic   Next Topic Topic: Is there a JParagraph?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use