You are here:

Java/JLabel in JTable

Advertisement


Question
Hi, Harper
         I m Nirav Raval Frm India. At now i m develop one Windowbased Application in java with Netbeans IDE 6.0 , in which I have to develop a printable customer bill. In which I m using JTAble to use as a printable bill. Here I have to attch one Image As A logo in the first row of JTable.
         So My Question is how can I Attch that logo with help of JLabel in JTable at particular Cell.?

          Thanks In Advance
          Bi.

Answer
Set the default renderer (for the Object class) in your JTable (using setDefaultRenderer) to a subclass of DefaultTableCellRenderer. In this subclass, override the getTableCellRendererComponent method to return your own JLabel that has your icon in it (just check too see if row and column match what you want, if they don't just call the super implementation).

Also note that you can render html in a JLabel, so using table.setValueAt("<html><img src=\""+YourClass.class.getResource("<<your image Path>>")+"\"/></html>", 0, 0);
will load an image from your jar into the cell. However, unless you adjust the cell width your image may be cut off.

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2012 About.com, a part of The New York Times Company. All rights reserved.