Solved

Image and string in a JTable cell

Posted on 2000-04-13
8
419 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Author Comment

by:s_lavie
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points from 100 to 150
0
 
LVL 2

Author Comment

by:s_lavie
Comment Utility
falter,
Thanks for everything!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

15 Experts available now in Live!

Get 1:1 Help Now