aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes little boxes in my JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "little boxes in my JTable" Watch "little boxes in my JTable" New topic
Author

little boxes in my JTable

Skip Cole
Ranch Hand

Joined: Jan 05, 2001
Posts: 175
Hi,
I am getting little boxes in my JTable:


I believe this is because I am filling the rows with an array of objects, and for values beginning with numerics, these are being incorrectly rendered. Has anyone seen this, or know a good way around?
Thanks!
Skip


If you love me, you will visit docs.opensimplatform.org
(FYI, Getting it tattooed on is a bit much.)
joey phillip
Ranch Hand

Joined: Jan 22, 2001
Posts: 40
use trim function before copying to array of object.
joey
Skip Cole
Ranch Hand

Joined: Jan 05, 2001
Posts: 175
Hi,
Thanks for the information. I should have explained the problem more:
I would use the trim function, but these Strings come staight out of the database as an array of Strings. I then put them into a vector of vectors (one vector for each row representing each flight), and then pass them as a serialized object to the client. This vector of vectors feeds right into the JTable. To do 'trim' on each String I either have to modify the database, or unbundle everything, trim them, and then package them up again to feed to the JTable.
I was hoping to use something like a modified TableRender to clean this up. I guess I could put them in a loop and trim them at some point. I just hoped there was a better way. Also, I want to learn more about how to use the JTable.
Thanks for the response,
Skip
Douglas Kent
Ranch Hand

Joined: Oct 06, 2000
Posts: 171
Skip,
Are you using "setModel" to feed the data into your JTable? I implemented a class that extends AbstractTableModel, and when I use setModel, it wipes out the headers. I too am having the problem with the squares...
Anil Sadineni
Greenhorn

Joined: Feb 06, 2001
Posts: 20
Hi skip & Douglas,
I have got the solution for this problem. In your JTable's tablemodel At getValueAt(int row, int col), before returning value, you have to trim the value. That's the exact position for placing trim() method.

code:
public Object getValueAt(int row, int col)
{
return (Object)(records[row][col].trim());
}
I think it will help you
mail me to anilkumar_java@rediffmail.com
Douglas Kent
Ranch Hand

Joined: Oct 06, 2000
Posts: 171
Anil,
Thanks very much--have not tried that yet, but that's the correct way to handle this. My follow-on question was displaying new query results. As I mentioned, I created a descendant of the AbstractTableModel (shown)
public class TableDataInfo extends AbstractTableModel {
String [] columnNames=null;
String [][] data=null;
/**
* This constructor creates a descendant of the AbstractTableModel
* object from the array of DataInfo objects provided in the argument.
*
* @param DataInfo[] - DataInfo array.
*/
TableDataInfo (DataInfo [] inputDataInfo) {
FieldInfo [] TFieldInfo = inputDataInfo[0].getFields();
String [] vals = new String [TFieldInfo.length];
columnNames = new String [TFieldInfo.length];
data = new String [inputDataInfo.length][inputDataInfo[0].getValues().length];
/*
* load columnNames
*/
for (int i=0; i < TFieldInfo.length; i++) {
columnNames[i] = new String (TFieldInfo[i].getName());
}
/*
* load data
*/
for (int i=0; i < inputDataInfo.length; i++) {
vals = inputDataInfo[i].getValues();
for (int j=0; j < vals.length; j++) {
data[i][j] = new String (vals[j]);
}
}
}
HOWEVER, WHEN DISPLAYING THE RESULTS, THIS WIPES OUT THE HEADERS. DO YOU HAVE ANY IDEAS ON THIS?
THANKS MUCH,
DOUG
Skip Cole
Ranch Hand

Joined: Jan 05, 2001
Posts: 175
My headers were wiped out until I put it in a JScrollPane. Maybe this will help.
Thanks for your help guys! Now I got rid of my little boxes :-)
Skip
Dave Boden
Ranch Hand

Joined: Nov 08, 2000
Posts: 37
Does anyone know why the headers on a JTable disappear unless you put the table in a JScrollPane? Is this a bug or a "feature"?
ramandeep singh
Greenhorn

Joined: Jun 27, 2001
Posts: 20
Hey Dave
I think It is neither a bug nor a feature
But it is starange
But if U need to diplay ur JTable in any container except
JScrollPane, U Need to do the following:
container.setLayout(new BorderLayout());
container.add(table.getTableHeader(), BorderLayout.NORTH);
container.add(table, BorderLayout.CENTER);

The exciting thing about this is that by using above
procedure, U can display ur headers on top or bottom of ur
container.
Hope This Wil Help

Ramandeep Singh
Trevor Dunn
Ranch Hand

Joined: Jun 13, 2001
Posts: 84
Concerning the trim, I had the same problem. What I did was nab the problem right at the root. The problem is in the readRecord()method in Data class. It constructs a string that is the length of the field, even if the string is smaller. The method it uses is deprecated, so since I had to change it anyway, I just added a trim to it. Now I have a good colection of data.
The problem with putting it in the getValueAt is that you are only affecting the display. Any manipulation you do on the data you will have to remember to trim all the time.
Just a thought
Trevor
ramandeep singh
Greenhorn

Joined: Jun 27, 2001
Posts: 20
Hey Trevor
I have also done the same thing
I also feel that when we have to modify it any way then
why not to add trim() at that point
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: little boxes in my JTable