• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Customizing JTable

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic