Solved

Image and string in a JTable cell

Posted on 2000-04-13
8
425 Views
Last Modified: 2008-02-01
Hi,
I want to show an image + a string in the same cell of a JTable.
How do I do this???
0
Comment
Question by:s_lavie
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:AlexJ030200
ID: 2712293
Hi s_lavie,

A simple solution is to insert a JLabel into the JTable cell and set the text and image of the JLabel to the values that you need.
0
 
LVL 3

Expert Comment

by:falter
ID: 2712331
Hi,
one way is to define a TableCellRenderer

public class MyCellRenderer extends DefaultTableCellRenderer {      
public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      setIcon("Image");
      setText("Your text");
      

}
public class MyTable extends Jtable {
setDefaultRenderer(Object.class, new MyCellRenderer());
}

Have a closer look at javax.swing.JTable and
javax.swing.table.TableCellRenderer

Hope this helps!
jf
0
 
LVL 2

Author Comment

by:s_lavie
ID: 2720317
I didn't get it:

AlexJ,
How do I insert a JLabel into the JTable cell?

falter,
Can you elaborate?

I would like to have a complete simple code, that implements a 2 columns table which one of its columns contains img+string, and the other contains string.
I would really appreciate this kind of demo - increasing points is a guaranty!!!
0
 
LVL 3

Accepted Solution

by:
falter earned 150 total points
ID: 2721155
I have a little demo
three classes
Demotabelle the main program:

import com.sun.java.swing.table.*;
import com.sun.java.swing.*;
import com.sun.java.swing.JTable;
import com.sun.java.swing.table.AbstractTableModel;
import com.sun.java.swing.JScrollPane;
import com.sun.java.swing.JFrame;
import java.awt.event.*;
import java.awt.*;
import java.util.*;


public class Demotabelle extends JFrame  
{
      public static void main(String args[])
      {
            Demotabelle frame = new Demotabelle();
            frame.setSize(300,300);
            frame.setVisible(true);
      }
public Demotabelle() {
      super("TabellenDemo");
      Object[][] cellData = new Object[][] {{(Object) new ImgObj(new ImageIcon("duke2.gif"), "Row1Col1"), (Object) "Row1Col2"}, {(Object) new ImgObj(new ImageIcon("duke2.gif"), "Row2Col1"), (Object) "Row2Col2"}};
      String[] colNames = {"Col1", "Col2"};


      JTable table = new JTable(cellData, colNames);
      table.setDefaultRenderer(Object.class, new DemoTableRenderer());
      table.setRowHeight(50) ;

      JScrollPane scrollpane = new JScrollPane(table);

      setContentPane(scrollpane);


      addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                  System.exit(0);
            }
      });
}
}

An Object which only has an Image and a String field used in the datamodel of the table.
The paint(DefaultTableCellrenderer) method is doing the painting of an Image and a String in one table cell.



import com.sun.java.swing.*;
import com.sun.java.swing.table.*;
import java.awt.*;
public class ImgObj {
      private ImageIcon img;
      private String myLabel;
/**
 * Insert the method's description here.
 * Creation date: (16.04.2000 %T)
 * @return java.awt.Image
 */
public ImageIcon getImage() {
      return img;
}
/**
 * Insert the method's description here.
 * Creation date: (16.04.2000 %T)
 * @return java.lang.String
 */
public String getLabel() {
      return myLabel;
}

public ImgObj(ImageIcon img, String label) {
            this.img  = img;
            this.myLabel = label;
      }
      /**
       * Insert the method's description here.
       * Creation date: (16.04.2000 %T)
       * @param renderer DefaultTableCellRenderer
       */
      public void paint(DefaultTableCellRenderer renderer) {
            System.out.println("#### paint ImgObj####");

            if (img != null) {
            System.out.println(img + "#### paint Img####");
                  
                  renderer.setIcon(img);
                  
            }
            renderer.setText(this.getLabel());
      }
/**
 * Insert the method's description here.
 * Creation date: (17.04.2000 %T)
 * @return java.lang.String
 */
public String toString() {
      return myLabel;
}
/**
 * ImgObj constructor comment.
 */
 
public ImgObj() {
      super();
      img = null;
      myLabel = "Dummy Text";
}
}

and the TableRenderer

import com.sun.java.swing.table.*;
import com.sun.java.swing.*;
import java.awt.*;
public class DemoTableRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

      if (value == null)
            return null;
      System.out.println(value + "#### getTableCellRendererComponent####");
      super.setHorizontalAlignment(DefaultTableCellRenderer.LEFT);
      try {
            if (value instanceof ImgObj) {
            System.out.println(value + "#### Is ImgObj ####");
                  ((ImgObj) value).paint(this);
            } else
                  if (value instanceof String) {
                        setIcon(null);
                        super.setValue((String) value);
                  }
      } catch (Exception exc) {
            System.err.println("Table Renderer : " + exc.getClass().getName());
      }
      return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
/**
 * DemoTableRenderer constructor comment.
 */
public DemoTableRenderer() {
      super();
}
}

substitute duke2.gif with what you want, the program assumes to find the image file in the current directory means the one where you are when you start the program

Try it with
java Demotabelle

This program was written with an old version of VisualAge for Java, so the old Swing 1.0.3 was used.

Change all occurences of com.sun.hava.swing to javax.swing if you have a newer one.

Hope this gives you a starting point.

JF
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:s_lavie
ID: 2721686
Hi falter,

It worked fine, but only when I wrote the fullpath of the gif. I tried to locate the gif file both where Demotabelle.java and Demotabelle.class are, but it made no difference.

Any idea? - don't worry, you'll get your points anyway :-)
0
 
LVL 3

Expert Comment

by:falter
ID: 2722013
Hi s_lavie,

the only thing I do is to set my classpath

I'm running on Linux, so I do
export CLASSPATH=.:./swingall.jar
java Demotabelle

Ok I start the programm from the directory where duke2.gif was located,
this makes the difference.

regards,
jf
0
 
LVL 2

Author Comment

by:s_lavie
ID: 2722107
Adjusted points from 100 to 150
0
 
LVL 2

Author Comment

by:s_lavie
ID: 2722109
falter,
Thanks for everything!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now