aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes JTable Problem ....Help! 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 "JTable Problem ....Help!" Watch "JTable Problem ....Help!" New topic
Author

JTable Problem ....Help!

harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
hi all,
i am running through this problem.I get the data displyed ok in the JTable,when user clicks the search button.I
create new references of all these objects which are associated with displaying the data in the JTable, But the
problem is ,the old data suddenly appears on the jtable ,when i click on the empty space of JTable.it seems like
the TableModel keeps a reference to the old data .I can find a way to get rid of that.
Any help would be helpful
harcharan
This is my sample code
public class FlightInfoPanel extends JPanel{
JTable jTable;
ScrollPane scrollPane;
Object[] searchResulsts;

public FlightInfoPanel(Object[] searchResults){
jTable = new JTable(new MyTableModel());

//added mouse listener to JTable,when user click on
//one of the rows in JTable to pop up a dialog box.
jTable.addMouseListener(new SelectRecordListener());
scrollPane = new ScrollPane(jTable);
add(scrollpane);

}
class MyTableModel extends AbstractTableModel{
//implemented here
}

class SelectRecordListener implements MouseAdapter{
public void mousePressed(MouseEvent e){
//implemented here to pop up the dialog box
}
}
}
[ September 13, 2002: Message edited by: harcharan kanwal ]
Richard Solomon
Greenhorn

Joined: Sep 12, 2002
Posts: 11
What you should be doing in your TableModel is re-assign the data, once you have done that you should call fireTableDataChanged() of the AbstractTableModel and that should solve your problem that is if you are not doing that already, if you are doing this already then give us your table model class so that we can take a look. Also make sure that you are cleaning up old references to the data.
Here is a simple example:
public void setNewData(DataInfo[] di) {
this.di = di;
fireTableDataChanged();
}
Hope this helps
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
hi Richard,
thanks for you response
here is my Table model class.I do see new data in accordance with my search criteria,but when i click on these visible rows or on empty space of the JTable, (which(empty space) was displaying the results of previous search criteria),i get the this old data on my JTable,
thanks

class FlightInfoTableModel extends AbstractTableModel {
DataInfo[] searchResults;
FieldInfo[] fieldInfo;
DataInfo singleRow;
String[] columnValues;

public FlightInfoTableModel()
throws DatabaseException,RemoteException{
fieldInfo = fbnModel.getFieldInfo();
}
public String getColumnName(int fieldIndex){
StringBuffer newColumnNameBuffer = null;
String fullColumnName = fieldInfo[fieldIndex].getName();
int spaceSeparator = fullColumnName.indexOf(" ");
StringBuffer columnNameBuffer = new StringBuffer(fullColumnName);
if ( spaceSeparator != -1){
newColumnNameBuffer = columnNameBuffer.insert(spaceSeparator, "\n");
fullColumnName = newColumnNameBuffer.toString();
}
return fullColumnName;
}
public int getColumnCount(){
return fieldInfo.length;
}
public int getRowCount(){
return searchResults.length;
}
public Object getValueAt(int row, int column) {
singleRow = searchResults[row];
Object value = null;
columnValues = singleRow.getValues();
value = (Object)columnValues[column];
return value;
}
public void setNewData(DataInfo[] searchResults){
this.searchResults = searchResults;
fireTableDataChanged();
}
}
Richard Solomon
Greenhorn

Joined: Sep 12, 2002
Posts: 11
Hey there
Your TableModel looks fine to me, but what I would suggest for you to try is to place this method call into the
setValueAt(Object o, int row, int col)
fireTableCellUpdated(row, col) place this at the bottom of the method after you have set the values needed.
Also another thing, are you maybe using a custom TableCellRenderer for your table?
So just try the above and see what happens
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
hi Richard,
i am going to work on your suggestions.And yes i have implemented TableCellRenderer to render the column header name in two lines ,in case the column name is too long to be fit in.I commented out the portion of this code,but it does not help, the situation,the problem still remains the same.i am going to work on the above suggestions.
harcharan
[ September 13, 2002: Message edited by: harcharan kanwal ]
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
hi Richard,
i override the setValue method,but nothing seems to be working , any ideas
public void setValueAt(Object value, int row, int column) {
singleRow = this.searchResults[row];
columnValues = singleRow.getValues();
columnValues[column] = (String)value;
fireTableCellUpdated(row, column);
}
thanks
harcharan
[ September 13, 2002: Message edited by: harcharan kanwal ]
Sarita Gupta
Ranch Hand

Joined: Aug 12, 2002
Posts: 43
Hi Harcharan
How abt I suggest you remove the empty rows after each search. That's what i did.
Btw why is it that u have defined singleRow and columnValues as private variables. You can simply declare it locally within the setValueAt() and getValueAt() methods...
Sarita
[ September 13, 2002: Message edited by: Sarita Gupta ]

There are no great people in this world, only great challenges which ordinary people rise to meet.
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
hi Sarita,
i looked into your suggestion,but i do get new data accordingly everytime i perform a search on new search criteria,but when i click on these visible rows or on empty space of the JTable, (which(empty space) was displaying the results of previous search criteria),i get the this old data on my JTable,the one i got at the first time.Am i making this topic understandable.i am still debugging it.
There is no particular reason for declaring these variables private , i could have them in my method body.
thanks
harcharan
[ September 14, 2002: Message edited by: harcharan kanwal ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi harcharan,
Man this is a really confusing problem. Your code seems to be OK, although I havn't tried it. To clarify exactly what's going on here, the question I have is if the table initially has 15 rows displayed and then you do a restrictive search that results in only 5 records doesn't your table shrink to 5 rows? If not, then something odd is going on with table itself and not the model or at least that the way it seems to me.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

The Table itself inside the JScrollPane shrinks, but the JScrollPane doesn't shrink. Right?
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
Hi Michael, Mark ,

yes , my table shrink to 5 or whatever the number of rows the data has.when i click on these table rows or the empty space, the one was showing more rows of data previously,suddenly i get painted with this old data rows,which i searched first when the application was loaded.i am suspecting ,it has to do something with the paint() or repaint() of JFrame.My model is working fine.I even added my table directly on the contentpane of JFrame without the JScrollpane.It still does the same.Ofcourse it would be no problem to create new instance of JFrame everytime there is a new search .but i wanted have the updated on the same frame
hope this help to figure it out.
harcharan
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi harcharan,
Just out of curiosity are you adding components with add(component) or getContentPane().add(component) to your JFrame? That could be the problem. What SDK and OS are you using? This is looking more and more like an implementation bug. Sorry we're not more help here. Try giving us a step by step of how you are initializing the frame and maybe that will shed some light.
Michael Morris
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
Ok,i try to put it together here.OS is windows ME and jdk1.3.1
//Main Display JFrame
public Class FlightInfo extends JFrame{
private Container contentPane;

public FlightInfo()(

//added connectMenu to MenuBar
//action occurs on MenuItem pops up ConnecFBN dialog
//to connect to the data either locally or remotely

}

//This method has the access to the private contentPane
//for my successive gui components.
public Container getContainerReference(){
return contentPane;
}

//main method for this class{}
}
//ConnectionDialog class
public class ConnectFBN.java extends JDialog{
public ConnectFBN(FlightInfo flightInfo){
//gets the connection successfully
//instantiate the FBNModel
//and add the SelectionPanel.java class for to search the flights
flightInfo.getContainerReference().add(selectionPanel,BorderLayout.NORTH);
flightInfo.setVisible(true);
}
}

//SelectionPanel class
public class SelectionPanel extends JPanel {
public SelectionPanel(FlightInfo flightInfo, FBNModel fbnModel){
//has three comboboxes and a search button
//gets the searchStrin,perform the search,gets the DataInfo[]
//and adding the table to the contentpane
flightInfoPanel = new FlightInfoPanel(flightInfo,fbnModel,searchResult);
(flightInfo.getContainerReference()).add(flightInfoPanel,BorderLayout.CENTER);
flightInfo.setVisible(true);
}
}
//FlightInfoPanel class
public class FlightInfoPane extends JPanel{
JTable jtable;
MyTableModel tableModel;
JScrollPane scrollPane;

public FlightInfoPanel(FlightInfo ,FBNModel,DataInfo[]){
tableModel = tableModel();
jtable = new JTable(tableModel);
or
jtable = new JTable().setModel(tableModel);
scrollPane.add(jtable);
add(scrollPane.BorderLayout.CENTER);
}
//Tablemodel is implemented
class mytablemodel extends AbstractTableModel{
......................
}
}

hope above is readable
harcharan
[ September 15, 2002: Message edited by: harcharan kanwal ]
[ September 15, 2002: Message edited by: harcharan kanwal ]
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
hi everybody,
the problems is solved , i had to remove the old instance of FlightInfoPanel,which is containing the JTable,before adding the new instance of FlightInfoPanel at same position.
if(flightInfoPanel !=null){
(flightInfo.getContainerReference()).remove(flightInfoPanel);
}
execute the above code first before adding the new instance of FlightInfoPanel
thanks for everything
harcharan
Allen Chan
Ranch Hand

Joined: Jun 07, 2002
Posts: 64
Hi all,
I cannot understand how the following constructor in the posted code work, could someone explain? Thanks
/////////////////////////////////
public FlightInfoTableModel()
throws DatabaseException,RemoteException{
fieldInfo = fbnModel.getFieldInfo();
}
////////////////////////
harcharan kanwal
Ranch Hand

Joined: Jul 02, 2001
Posts: 64
hi
I cannot understand how the following constructor in the posted code work, could someone explain? Thanks
/////////////////////////////////
public FlightInfoTableModel()
throws DatabaseException,RemoteException{
fieldInfo = fbnModel.getFieldInfo();
}

i should have mentioned it.FlightInfoTableModel class is the inner class of FlightInfoPanel ,which is a JPanel class, has fbnModel reference to FBNModel (later i changed this ref. to the DataClient object)is passed as parameter in its constructor body ( FlightInfoPanel)and assigned to the class variable of this enclosing object(FlightInfoPanel),so that fbnModel ref can be useful for the rest of this enclosing object(FlightInfoPanel).
hope this clears things
harcharan
[ October 26, 2002: Message edited by: harcharan kanwal ]
 
Consider Paul's rocket mass heater.
 
subject: JTable Problem ....Help!