Results 1 to 5 of 5
- 01-18-2011, 03:24 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Updating a JTable after Inserting Record
Greeetings All,
Iam developing a swing application with a derby database and using Toplink JPA for persistence. My issue is that the JTable will not update to reflect the inserted record.
For the table, I have created a tablemodel that extends DefaultTableModel and implements TableModelListener and in the tableChanged method of the class I've tried a variety of update methods e.g validate(), revalidate(), and SwingUtilities.updateComponentTreeUI(comp).None seems to work.:(
I've called the fireTableDataChanged() method after adding my record . I am out of ideas on the issue. I could post some snippets of the code I have if need be.
Help.Last edited by muhindi; 01-19-2011 at 01:03 PM.
- 01-18-2011, 06:24 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Is the insert into the DB reflected in the data model that backs the JTable? I suggest posting an SSCCE that demonstrates the problem to clarify
- 01-19-2011, 09:44 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
Pardon for the delay in reply. Yes the database reflects the inserted record but only
after I run the application again is the new record shown.
The custom table model I created is
In the Netbeans form designer I used post creation code tab on the properties window to attach myPHP Code:// class AppModel : The Customized tableModel to // facilitate updating of the JTable after records are inserted class AppModel extends DefaultTableModel implements TableModelListener{ public AppModel(){ System.out.println("AppModel created"); } public void tableChanged(TableModelEvent e) { if(e.getType()==TableModelEvent.INSERT){ tblStores.validate();//tried this did'nt work System.out.println(" New Record Added "); } }
custom tableModel to the Jtable and also set the Listener as below:
In the addStore button I added the following code:PHP Code:tblStores.setModel(appmodel); tblStores.getModel().addTableModelListener(appmodel);
The tableDataChanged method is being executed since I can see the "New record Added "statementPHP Code:Stores s=new Stores(txStoreName.getText(),txBuilding.getText(), txStreet.getText()); if(dbworker.addStore(s))//after successful insert to database... { appmodel.fireTableDataChanged();// call tableChanged() in custom table model
on the console. But the JTable just wont refresh.
I hope these snippets will help in clarification of the problem.Last edited by muhindi; 01-20-2011 at 08:02 AM.
- 01-24-2011, 10:16 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
i need help......
tell me how can i remove Table header...
i tried columnRemove method .it removed the column but when ever i add new column using model.addColumn("new Field"); but what happened here old header and new header are append...
what i need means i need only the new header only...
and this is my code:::::
package sample;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class TableColumnRemove {
DefaultTableModel model;
JTable table;
public static void main(String[] args) {
new TableColumnRemove();
}
public TableColumnRemove() {
JFrame frame = new JFrame("Remove a column from a JTable");
JPanel panel = new JPanel();
String data[][] = {{"Vinod", "MCA", "Computer"},
{"Deepak", "PGDCA", "History"},
{"Ranjan", "M.SC.", "Biology"},
{"Radha", "BCA", "Computer"}};
String col[] = {"Name.", "Course", "Subject"};
model = new DefaultTableModel(data, col);
table = new JTable(model);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table);
panel.add(pane);
frame.add(panel);
frame.setSize(500, 150);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRoot Pane.PLAIN_DIALOG);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
int columnCount = table.getColumnCount();
for (int i = 0; i < columnCount; i = 0) { // here im remove columns
Remove(table, i);
columnCount = table.getColumnCount();
}
model.addColumn("new Field"); //after add new column
}
private void Remove(JTable table, int col_index) {
TableColumn tcol = table.getColumnModel().getColumn(col_index);
table.removeColumn(tcol);
}
}
- 01-24-2011, 12:48 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
SOLVED AT LAST!! Worked on it for 3 nights. Solution ?.
1. I set the List holding the query resultset to observable.
2. In the eventhandler responsible for persisting the entity, I added the following single line of code:
This will cause the change(insert or delete) to be 'viewable' on the JTable.PHP Code:myList.add(myObject);
Hope this helps someone else.;)
Similar Threads
-
problem with inserting new record
By gerard kowara in forum JDBCReplies: 1Last Post: 10-26-2010, 11:33 AM -
Updating the existing JTable !
By Stephen Douglas in forum New To JavaReplies: 2Last Post: 04-07-2010, 08:38 PM -
Problem with updating JTable
By kwaspl in forum New To JavaReplies: 2Last Post: 12-20-2009, 10:41 PM -
inserting values from jtable into database
By sandeepsai17 in forum New To JavaReplies: 1Last Post: 06-29-2009, 07:31 PM -
how to populate large record in jTable
By ahmed88 in forum New To JavaReplies: 4Last Post: 04-17-2009, 07:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks