• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inserting a JLabel to a JTable

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I am trying to enter a JLabel to each cell in a JTable. Right now when I enter a JLabel in 1 of the cells it returns a string


I have started my modifying my table model as follows:



I am aware I also need to write a cell renderer class for this by that no idea how to do this. I have read about cell renderers and managed to customize my cells but not sure what I should do to return a JLabel. Can anyone explain in detail as I have tried a lot without success.

Thank you for reading my post, and I appreciate your help.

Regards,

Dani

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can't you just add a String? What does a JLabel offer that a String doesn't?

Anyway, if you would have used our search with just keywords "jtable" and "jlabel" you would have found this thread which again leads to this thread.
 
dani abdallah
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Why can't you just add a String? What does a JLabel offer that a String doesn't?

Anyway, if you would have used our search with just keywords "jtable" and "jlabel" you would have found this thread which again leads to this thread.



I wanted to have a background image in each cell, which is what a label offers me that a cell doesn't. I can also customize the data I have in each cell in better ways with positioning etc.

Anyway, I have kind of found a solution now by rendering HTML inside each cell.

Regards,

Dani
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An easier and IMO more correct way is to let the table model contain an Icon and return Icon.class from getColumnClass for that column.

If you need to display both an Icon and String, you only need a custom renderer extending DefaultTableCellRenderer (which extends JLabel) and appropriate code in getTableCellRendererComponent.
 
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also had the idea to show JLabels in a Jtable ( even I wodered if JPanels were possible to show ) but I fail with my
knowledge of an advanced beginner. Here is what I get instead of the labels shown :



Also I ran the TableDialogEditDemo from the Tutorials of the official Jave site and this program shows the same
problem with JLabel and Color. Here is what is shown :



Do you have a suggestion for an advanced book/tutorial where I could find help.

In my program I already added this piece of code where data is an array containing all the JLabel items but I get no
better results as when I use the JTable(data,columnnames) constructor directly :



 
Ranch Hand
Posts: 92
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a very simple example, drawing on what I knew already and the links provided by earlier posters;



I hope this helps.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John McParland wrote:


No offense but that class doesn't make much sense. You extend DefaultTableCellRenderer which a) is a JLabel subclass itself, and b) returns a JLabel (itself) from getTableCellRendererComponent. You can implement the same much more efficiently:
 
Wolfgang Tintemann
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much.
I ran your program and it worked immediately.
I think I begin to understand now the contributions entered formerly
in this thread. I will try to build the code into my Applet.

Sorry, I want to add this question : you write
table.setDefaultRenderer(String.class, new LabelRenderer());
Why not JLabel.class instead of String.class ?

Do you think I can also make a Table full of JPanels with this method
by replacing JLabel with JPanel ?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TableCellRenders can theoretically display any component, so that includes JPanels. All you need to do is create your own TableCellRenderer implementation. For interaction with that component you need to use TableCellEditors.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wolfgang Tintemann wrote:Sorry, I want to add this question : you write
table.setDefaultRenderer(String.class, new LabelRenderer());
Why not JLabel.class instead of String.class ?


This is related to the table model. There is a method getColumnClass in TableModel interface, which specifies type of values that will be provided by the table model for given column. When you specify some basic column classes in your table model, the table will start functioning all by itself. Numbers will be properly aligned and sorted, and if you specify Boolean.class, JTable will display checkboxes in that column. (It is always good to set the column classes right.)

This works because for the basic classes there are renderers and comparators assigned in the default setup of JTable. By calling setDefaultRenderer you can override these defaults. In this particular instance you say that every column that is of type String will be displayed using the LaberRenderer. And since the table model specifies that all columns are of type String (see the first post), we need to put the String class there.

You might want to override renderer for just one column instead of all columns of type String. In that case there is the setCellRenderer method of the TableColumnModel interface.

Note: it would not be a good idea to return JLabel instances from table model, for two reasons:

1) JLabel is a GUI component which is more expensive to construct and takes more memory than eg. a String. JTable is designed to support huge number of cells, so it reuses one (or several) instances of components to draw many cells, avoiding the need to create a component for every cell in the table.

2) TableModel is supposed to provide data to be displayed and should not care about the painting mechanism, that is the responsibility of the JTable and cell renderers. For this reason it is bad design to return components from TableModel. You should return all data needed to draw the cell from table model and then create a renderer that will use the data to display desired content. JTable will take care of sharing this component among its cells.



Do you think I can also make a Table full of JPanels with this method
by replacing JLabel with JPanel ?


I think this might be possible, although I've never tried. Your cell renderer will simply return instance of JPanel. You just need to understand that the JPanel will not be "physically" inserted into the cell, only its drawing method will be used to display its contents. If somebody clicks in the cell, the event will not be forwarded to your panel. The editing mechanism would be employed if you wanted the user to interact with the panel.

But again, I've never tried it. I personally used just textboxes and comboboxes as editors in JTable. There might be many caveats in this. Why not jut put all of the JPanels into a JScrollPane organized by some suitable layout?
 
Wolfgang Tintemann
Ranch Hand
Posts: 65
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a book/tutorial which deals exclusively with this complex
handling of the JTable object ? This would be very needed.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wolfgang Tintemann wrote:Is there a book/tutorial which deals exclusively with this complex
handling of the JTable object ? This would be very needed.



I learned everything I know from this tutorial, reading the JDK source code (sometimes stepping through it using a debugger to understand what is happening there) and a few other examples I've found on the web. It took me some time, though. I was new to Swing and Java, but I had some prior experience with event-driven GUI frameworks.
 
John McParland
Ranch Hand
Posts: 92
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:

John McParland wrote:


No offense but that class doesn't make much sense. You extend DefaultTableCellRenderer which a) is a JLabel subclass itself, and b) returns a JLabel (itself) from getTableCellRendererComponent. You can implement the same much more efficiently:



No offense taken Rob, however could you explain what you've done differently? Certainly my use of toString() wasn't required and the call to super is definitely advisable. But could you place this in a SSCCE?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are only two differences, and no need for an SSCCE. You create a new JLabel each time, I use the existing one (the renderer itself). Furthermore, my label may get different colouring dependent on the selected state. of the cell.
 
reply
    Bookmark Topic Watch Topic
  • New Topic