Solved

How to display BufferedImages in tables of Applet? (Applet, JTable, DefaultTableModel, and BufferedImage)

Posted on 2004-04-16
17
852 Views
Last Modified: 2012-06-27
Hi, I want to display a couple of images in a table in applet. I am pretty new to JApplet and AWT, please help me.

Those images are currently stored in a database as binaries. I guess I need to read them into BufferedImage objects. I have successifully created tables in applet using Applet, JTable, DefaultTableModel classes, unfortunately, these tables can only host String objects. When I put a BufferedImage object into a cell of the table, the reference of that object displayed instead of the picture. Casting seems not working. I also tried a JLabel (since it's simple), no luck. So am I using wrong classes, or is there an additional layer need to be created between the table and picture?

Could you give me a sample code for this purpose?

Thanks a lot and have a good weekend.

D.
0
Comment
Question by:ds869
  • 8
  • 8
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10846927
If you make the getColumnClass of your model return ImageIcon, that should do it
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 10846928
Try adding ImageIcon's to your table.

0
 
LVL 92

Expert Comment

by:objects
ID: 10846935
> If you make the getColumnClass of your model return ImageIcon, that should do it

No that definitely won't work.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 92

Expert Comment

by:objects
ID: 10846940
0
 
LVL 92

Expert Comment

by:objects
ID: 10846945
That example actually does more than you need but should give you the basic idea.
Just remove the bits you don't need if u want
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
ID: 10847059
>>No that definitely won't work.

It does, as the following demonstrates:



import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class JTableDemo extends JFrame {
      JTable table;


      public JTableDemo() {
            Object[][] data = new Object[1][2];
            data[0][0] = new ImageIcon("..\\next.gif");
            data[0][1] = new ImageIcon("..\\next.gif");
            String[] columnNames = {"Image1", "Image2"};

            DefaultTableModel model =
                  new DefaultTableModel(data, columnNames) {
                        public Class getColumnClass(int index) {
                              return ImageIcon.class;
                        }
                  };
            table = new JTable(model);
            JScrollPane sp = new JScrollPane(table);
            getContentPane().add(sp, BorderLayout.CENTER);
      }

      public static void main(String[] args) {
            JTableDemo frame = new JTableDemo();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
      }
}

0
 
LVL 92

Expert Comment

by:objects
ID: 10847102
> It does, as the following demonstrates:

Because you are adding ImageIcons as I already suggested :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10847121
LOL - you suggested adding them to the table. They should be added to the model. Naturally they've got to come from somewhere
0
 
LVL 92

Expert Comment

by:objects
ID: 10847145
please don't go twisting my words (yet again) the example I posted demonstrated exactly what I meant.
Which you have simply duplicated probably after realizing that just setting the column class was not sufficient.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10847224
>>...after realizing that just setting the column class was not sufficient

ROTFL. Yes of course. I was expecting ImageIcons to appear out of thin air together with their images merely by returning ImageIcon.class from getColumnClass
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10847226
...and no - i haven't even looked at your link btw
0
 
LVL 92

Expert Comment

by:objects
ID: 10847228
> I was expecting ImageIcons to appear out of thin air together with their images merely by returning ImageIcon.class from getColumnClass

Well thats what your comment seems to suggest, I was just correcting it.
And as you appear to agree with my correction I'm not sure what your being argumentative about.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10847229
These were your words:

>>Try adding ImageIcon's to your table.

So how is


>>you suggested adding them to the table. They should be added to the model.

'twisting' them?
0
 

Author Comment

by:ds869
ID: 10901415
Thanks guys.

Object's 'Try adding ImageIcon's to your table' is the 'official' answer to my question, and CEHJ's sample code is easier to copy and paste.

Have a good day.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10903847
>>Object's 'Try adding ImageIcon's to your table' is the 'official' answer to my question

Shouldn't be as it's wrong ;-)  But thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 10904475
> Shouldn't be as it's wrong ;-)  

Must work by magic then :-D
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10904547
As i've had to explain here before 'working' is not synonymous with right.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sites similar to codingbat to improve coding hanson skills 3 49
jar file executable 12 51
Apps blocked by Java 9 79
Configure a Bean in an XML file 4 31
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

813 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

10 Experts available now in Live!

Get 1:1 Help Now