• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is the onFucus not working properly after adding a render a jTable cell?

 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I added a renderer to a jTable cell which adds padding to the inside of the jTable cell. The desired effect is to make the cell contents easier to read. The onFucus now only partially works (seemingly). Can anyone help to explain what I have done wrong. This is my first time using renders, everything was going great with the first 'header renderer' (to adjust header height), then after trying to add padding with a cell render i ran into problems. Any help letting me know where I have gone wrong would be greatly appreciated.  Any constructive criticism would also be welcome, i am trying to learn how to do this and haven't found any solutions to this problem on oracle, or stackoverflow - at least any solutions I could understand.

To summarise : Im adding padding to the inside of jTable cells and now the on focus 'highlight' appears to partial work.


Regards

The code:


 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now the on focus 'highlight' appears to partial work.


A JLabel is transparent by default so you don't see its background.

When creating a custom renderer using a JLabel you need to use setOpaque(true) in the constructor.

Also, don't call you class TableModel. There is an interface with that name in the JDK so it gets confusing.
 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob -

Works perfectly. Thank-you. Side question, when I have Integer object values in table cells I need to get the toString value example: new Integer(56745).toString() in order to get the renderer to work - Why is this? Thanks again for your help, Im slowly getting my head around renderers. And thank you for pointing out my mistake with the class name, appreciated

Regards
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JTable uses different renderers for different types of data. When you add an Integer value to the model the IntegerRenderer is used. This renderer will right just the numbers.

By using the toString() method you are actually storing the String value so it is treated like any other String and becomes left justified. You really don't want to do this.

So you need to also create a custom renderer for the Integer class. Read the section from the Swing tutorial on Using Custom Renderers for more information and working example.

Or, another approach I like to use sometime is to use Table Row Rendering. This link shows how to apply rendering changes for all data types without creating multiple renderers.
 
Peter Lock
Greenhorn
Posts: 24
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob - With your help I was able to separate the data and the table to create a 'Table Model' class to house the data, and add a custom renderer. Im reading through the Java Tips Weblog on Table Row Rendering now. Thanks for all your help. Greatly Appreciated.

Regards
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!