GeeCON Prague 2014*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes updating jtables in abstract tablemodel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "updating jtables in abstract tablemodel" Watch "updating jtables in abstract tablemodel" New topic
Author

updating jtables in abstract tablemodel

Abhi Chat
Ranch Hand

Joined: Oct 10, 2004
Posts: 57
hi,

im tryin to update my jtables each time on a button click...i need thr table to clear of the old contents and put new one. But the problem is im not able to use firetablerowdeleted or any function similar to this..simply they dont work....could anybody advice me on this....

abhi


SCJP 1.4<br />Preparing for <br />SCJD and SCWCD
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
I am not sure how your going around updating the Jtable. I implement my GUI with the MVC pattern, and my model basically extends the AbstractTableModel. Whenever I update the table(search, update etc), I update my underlying table datastrcture(list) and then call this.fireTableDataChanged. You dont call any method on the talbe to clear it, you just need to update the underlying datastructure and fire the changed event. As long as your implementing the methods properly it should work without any problem.

This maybe a stupid question but did you check if your button action listner is set properly. make sure you add the action listner, and in the implemntation your doing the comaparison properly. I was going in circles for about 30minutes until I realized I didnt add a listner to the buton . Your problem is probably something other than the tablemdodel

[ November 01, 2004: Message edited by: Inuka Vincit ]
[ November 01, 2004: Message edited by: Inuka Vincit ]

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Abhi Chat
Ranch Hand

Joined: Oct 10, 2004
Posts: 57
my problems still not solved it..im pasing a part of code..could u guys help me in it....



Appreciate any help.....
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
first of all you do realize that the model you pass to JTable is a different instance from the model you attempt to do something to in ActionPeformed right?
Secondly pasing public void clearT(AbstractTableModel m) an AbstractTableModel is redundant. You can call fireTableChanged() withing MyTableModel. So change it to a no arg method.

But if you just call model.clearT()(instead of creating a new instance) within the ActionPeformed method the table should be loaded with the new values.

So the problem is your clearing an instance of MyTableModel that is different from the one that is displayed by JTable.

check out this example
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Give it a shot... and post up if I am wrong...
GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Hi Guys, I got the same problem too, this is my code but I can't seem to figure it out

public class TableModel extends AbstractTableModel {
private String[] columnNames = {
"ContractorID", "Name", "Location", "Specialities", "Staff Size", "Rate", "Owner" };

private ArrayList records = new ArrayList();

public TableModel() {
super();
}

public int getColumnCount() {
return this.columnNames.length;
}

public String getColumnName(int col) {
return columnNames[col];
}

public int getRowCount() {
return this.records.size();
}
public Object getValueAt(int row, int column) {
Object[] temp = (Object[]) this.records.get(row);

return temp[column];
}

public void setValueAt(Object obj, int row, int column) {
Object[] temp = (Object[]) this.records.get(row);
temp[column] = obj;
fireTableCellUpdated(row, column);
}
//method used to add record(s) to the table
public void addContactor(String[] temp) {
this.records.add(temp);
//This does not work. Cannot clear the previous records
fireTableDataChanged();
}
}
Abhi Chat
Ranch Hand

Joined: Oct 10, 2004
Posts: 57
Thanx Inuka...my problem was callin the table with wrong instance....and u rightly pointed out...

I was just wondering that u are also giving the mcp C# test...so how is C# different from java ...even i wish to prepare for the test..could u give some advice.

For GD..

For Jtable you can only populate it with data of type objects. Now this fuction looks fine as I guess u play around with objects.

public void setValueAt(Object obj, int row, int column) {
Object[] temp = (Object[]) this.records.get(row);
temp[column] = obj;
fireTableCellUpdated(row, column);
}

// For this i see u have string array and u add it to records...to update
u have to again add it to the data type object which u started with...

//method used to add record(s) to the table
public void addContactor(String[] temp) {
this.records.add(temp);
//This does not work. Cannot clear the previous records
fireTableDataChanged();
}
Abhi Chat
Ranch Hand

Joined: Oct 10, 2004
Posts: 57
I have one more problem...when i call firedatachanged...only 2 rows [according to the search] shows up and the rest of the portion goes blank..like in the GUI ..that looks really inconsistent....is there a way i could only remove the data without disturbin the GUI.

Any thoughts on that.....
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
I just finished most of the codeing operation havent finished beautifying yet. But JTable has a setPreferredWidth method that you set the widht with.

DG your code looks fine are you sure your always adding a new String[] object? Or are you saying cant clear it. In order to "clear" the table you have to clear the ArrayList.


As for c# vs Java. The languages are very similar but c# is more restrictive(you have to use keywords when you overload or overwrite etc ..) the Microsoft test is a lot broader than SCJP. Where SCJP testd the language fundementals the MCP c# likes to test everything under the sun inlcuding features of the IDE. A lot lot more material to cover and understand. Microsoft assumes you have some development experiance so it will be very helpful if you do some programing. As a language c# has some very very powerful features( databinding, webservices) and building applications with it is very quick(depending on the functionality ofcourse).
[ November 04, 2004: Message edited by: Inuka Vincit ]
GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Thanks Inivita, but I still can't solve the proble. What do you meany by the following:

// For this i see u have string array and u add it to records...to update
u have to again add it to the data type object which u started with...
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
you need to call records.Clear() to clear the array list if you want to clear the table since that is the underlying data structure to the table. The second part I ment was make sure you calling new each time you add something to the table and that your not reassigning the same refreance.. sounds like your not.
GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Hi Inuka, I am doing exactly that. findByCriteria(String[] searchString) is in the controller class which instianties TableModel. What is funny is that when I search for a "Single" record, that record is displayed properly. When I search for another Single record, the previous record is not displayed anymore which is what I want BUT when I search for a FULL set of records, it correctly dsplays but a second search for a FULL set is "appended" to the first FULL set records which is NOT what I want. Help guys. I just want to show the second FULL set of records.

public TableModel findByCriteria(String[] searchString)
throws ClientControllerException {
TableModel model = new TableModel();
ArrayList arrayList = null;

try {
arrayList = (ArrayList) this.services.findByCriteria searchString);
Iterator it = arrayList.iterator();
while (it.hasNext()) {
model.addContactor((String[]) it.next());
}

} catch (Exception e) {
log.log(Level.WARNING, e.getMessage(), e);
throw new ClientControllerException(e);
}

return model;
}

public class TableModel extends AbstractTableModel{
//instianted when the TableModel class is instianted
ArrayList records = new ArrayList()

public void addContactor(String[] temp) {
//this method does not work
this.fireTableDataChanged();
this.records.add(temp);
}
}
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
first of fall the fire event should be called after all the data has been added. But in your case since you seem to adding a new data model each time so you dont realy have to call firechanged. Even that should be called if its only after the data is added

When you do a single line search the reason the initial search disappears is because your changing the previous tablemodel with a new instance. In JTable once you add a table model you can just change that table mdoel and the firechanged event without creating a model each time. As for the weird table behavior for a multi line search I am puzzled but it may be dure to the fact that your changing the model each time.
 
GeeCON Prague 2014
 
subject: updating jtables in abstract tablemodel