Solved

Image and string in a JTable cell

Posted on 2000-04-13
8
435 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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:

828 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