This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes inserting a JLabel to a JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "inserting a JLabel to a JTable" Watch "inserting a JLabel to a JTable" New topic
Author

inserting a JLabel to a JTable

dani abdallah
Greenhorn

Joined: Jul 12, 2010
Posts: 13
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

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
dani abdallah
Greenhorn

Joined: Jul 12, 2010
Posts: 13
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
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Wolfgang Tintemann
Ranch Hand

Joined: Sep 11, 2010
Posts: 65

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 :





Time isn't Money, Money is Time.
The man who works looses his time ( spanish proverb )
John McParland
Ranch Hand

Joined: May 11, 2009
Posts: 92

Here's a very simple example, drawing on what I knew already and the links provided by earlier posters;



I hope this helps.


Passed: SCJP 6 (90%), SCJD 6
Other: Spring training, extensive Swing experience
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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

Joined: Sep 11, 2010
Posts: 65

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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Sep 11, 2010
Posts: 65

Is there a book/tutorial which deals exclusively with this complex
handling of the JTable object ? This would be very needed.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: May 11, 2009
Posts: 92

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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: inserting a JLabel to a JTable
 
Similar Threads
JTable
jtable
Pasting image in JTable cells
JTable
JTable