This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Customizing JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Customizing JTable" Watch "Customizing JTable" New topic
Author

Customizing JTable

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
I am trying to create a JTable where certain columns are right-justified. I found some code that seemed to make sense, but I can't get it to work.
Here is what I am doing:

And here's my code for the AdjRateModel:

What am I doing wrong?
I also have another question on custom JTables.
I have a table that has a custom model that populates the table with values for different departments. When there are no more records for that dept, a subtotal line is printed. My question is, how do I get these subtotal lines to be a different color, or font? I looked at an example of a custom cell renderer, but I don't understand how I can tell it to make that certain line a different color. I can't tell it a row number because the subtotal lines will not always be the same row number.
How can I accomplish this?
I would really appreciate any help anyone could give me.
I'm having a hard time with these darn tables!
Thanks!
raj madhuram
Ranch Hand

Joined: Oct 10, 2001
Posts: 71
Hi,
Take a look at the examples in http://www2.gol.com/users/tame/swing/examples/JTableExamples3.html
There is a DecimalFormat example that is similar to what you are trying to achieve.
regards,
Raj
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hi Jennifer,
just to exclude some string comparison error:
could you try

actually, the cell renderer should handle the formatting of the numbers, not the model. in this case you could install a tablecellrenderer for the Double.class for example.
cheers,
Chantal
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for the replies. However I still have a question. How do I tell the table I want certain rows colored? I made a custom renderer telling the table to Bold column 0. How can I tell this same custom renderer to shade various rows, as well as right adjust other columns? Do I have to have a seperate renderer for each of these tasks.
Any help you can give me is appreciated!
Thanks!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Maybe you could have the table model keep track of this, and have the renderer ask the model if it should treat the current value specially.

Something like:


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
I tried the suggestion you gave me, and it worked, sort of. I got the rows to be shaded, however, now all of the data is gone??? What could have caused this?
Here's my renderer:

What have I done wrong?
Thanks again!
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
I tried putting in the following code:

and changed the 'this' reference to 'cell', and that got my data back.
Now my problem is that when I use the setHorizontalAlignment() method (I had to cast 'cell' to a JLabel to use it), it right-adjusts all of the columns, even though I'm only telling it to right-adjust if it is column 0.
Could someone please help me understand this behavior?
Thanks!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

The same cell renderer is used for all the cells, so you have to explicitly turn it on and off for the appropriate cells.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
How do I explicitly turn on/off my renderer for different cells?
Also, if I were to turn my renderer off for the columns that I don't want right justified, then doesn't the bold attribute also go away, since it's in the same renderer?
Sorry to be a pest, I just don't understand.
Thanks again!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Sorry, I'll try to explain my previous comment better...

The same renderer is used for all cells (of the column you set the renderer on, or the class you specify, or for the whole table, depending on how you set the renderer.) So if you want some cells to be white and some to be red, you can't assume that the "default" is white. You either have to explicitly tell the cell to be white or red. If you want some cells to be right aligned and some to be center aligned, the same is true. You have to explicitly state alignment. So you would explicitly set alignment to be right aligned when you want it, and you have to explicitly set the alignment to be center if it's not. This is because the same component is used for all cells, so once it is set red, or right aligned, it will be red or right aligned until you set it to be something else.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Aha! Now I understand. Thank you so much for taking the time to help me out. This is the first time I've really worked with JTables other than the default model.
Since I will probably be doing more of this kind of thing, do you have any suggestions where I could find more information on this stuff? Not just code examples, but something that actually explains what is happening so I know what I'm doing?
Thanks again, you've been a tremendous help!!
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

There is the Swing Tutorial hosted on Sun's website... it gives some descriptions and code examples. If you'd rather have a paper reference any good swing book worth its salt should explain tables in depth, since they're a pretty complex component. I learned most of my Swing information from Graphic Java 2 - Mastering the JFC Vol. 2 - Swing by David Geary.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Customizing JTable
 
Similar Threads
Setting foreground of a table cell on date column
getValueAt in JTable
Removing rows from DefaultTableModel
jtable
JCheckBox in JTable not working