• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error on trying to add JLabel in a cell of JTable  RSS feed

 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I am getting the output in the cell of JTabel as javax.swing.JLabel but I want an image over there .
When I tried to add an Image Directly , it worked but when I tried for the JLabel , I failed.
*I am not extending AbstractTableModel in this class so no getClass function is being imported.



 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no default renderer for the JLabel class, so you need to write your own.

Also, instead of actually adding a JLabel to the model it would be better to create a custom class that contains the text and icon you want to display. Then you can create a custom renderer that is shared by all rows in the column.
 
buntha Choudhary
Ranch Hand
Posts: 136
Android Eclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you provide any sample code for the above solution
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, you can read the JTable API and follow the link to the Swing tutorial on "How to Use Tables" for an example of a custom renderer. You can try to create your own.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, the component renderer should just return the cell's value itself. Be warned though that for buttons, it will probably not get the events (or not as you expect), as the component is only used for drawing. All events are handled by the JTable initially.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!