Solved

Image and string in a JTable cell

Posted on 2000-04-13
8
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

690 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