• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Image in JTable Cell

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;
import javax.swing.table.*;
import javax.swing.*;
import java.util.Vector;
import java.awt.Container;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Dimension;

public class JTableAndDatabase extends JFrame
{
public Vector col,row,rowdata;
public JTable table;

public JTableAndDatabase()
{
Container cnt = getContentPane();

col = new Vector();
row = new Vector();
rowdata = new Vector();

col.addElement("1");
col.addElement("2");
col.addElement("3");
col.addElement("4");

row.addElement("Vishal");
row.addElement("Vishal");
row.addElement("Vishal");
row.addElement(new ImageIcon("gogreen.gif"));
rowdata.addElement(row);

table = new JTable(rowdata,col);

JScrollPane scroll = new JScrollPane(table);

cnt.add(scroll);
setSize(500,500);
show();
}

public static void main(String[] args)
{
JTableAndDatabase demo = new JTableAndDatabase();
}
}



What's wrong with this code?

It is running properly but it can't show the image in the last collumn.

Help me........
Thanks.......
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just make an instance of the imageicon u want to display and try with the following JTable's method. Hope it will work :
=========================================================================
public void setValueAt(Object aValue,
int row,
int column)
Sets the value for the cell in the table model at row and column.
Parameters:
aValue - the new value
row - the row of the cell to be changed
column - the column of the cell to be changed
=========================================================================

let me know if it does...
till then
Yogesh
 
Yogesh Kodgule
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one more thing...i forgot to mention.

just check if u r having that image under the same directory as ur java files are.
best practice could be store all the images under 'images' directory which u can have under ur current working directory.
and the u refer ur images as ...say in ur case..."\images\gogreen.gif".


Yogesh Kodgule
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kkkkk lllll,

Welcome to Javaranch. I suspect you forgot to read our Naming Policy when you created your account. Please read it again and adjust your display name accordingly.

You can change your display name here.

Thanks.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic