File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable" Watch "JTable" New topic
Author

JTable

ramesh kancherla
Ranch Hand

Joined: May 01, 2008
Posts: 124
how can I insert a image into cell with actions

like I have to insert a iconImage into table cell whenever i clicked icon then it does some actions.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

The easy way is have your TableModel return ImageIcon.class or Icon.class for that column. That will render the cell's value as an image.
Next, add a MouseListener to the entire table. Inside its methods, you can call the JTable's columnAtPoint and rowAtPoint methods to find out which cell was clicked. If the column matches then perform the action:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ramesh kancherla
Ranch Hand

Joined: May 01, 2008
Posts: 124
setCellRenderer() method taking cellRenderer value only

you told as if TableModel returns icon,it will takes into cell

how can i pass this icon into cellrenderer
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

You don't need a custom cell renderer. If your table model returns one of those classes, then JTable will use one of its own internal cell renders to display the icon.

So your table model will have to override getColumnClass:

You can also choose other classes; Number.class and its subclasses will handle numeric conversion as well as right-aligning the number. Boolean.class will use a check box for both rendering and editing. And as said before, ImageIcon.class and Icon.class will use an image.

Please note that the value must be castable to the class, or you will get ClassCastExceptions.
ramesh kancherla
Ranch Hand

Joined: May 01, 2008
Posts: 124
How to add MouseListener to table

addMouseListener(java.awt.event.MouseListener) in java.awt.Component cannot be applied to (table.TableIcon)

I got this error, when I am trying to add MouseListener
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Apparently your not adding a MouseListener (or MouseAdapter) but an instance of table.TableIcon.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable
 
Similar Threads
how to check value of cell in JTable
Insert \n in a cell of a .xls file
Jtable customization
Help with NullPointerException
How can i insert JRadioButton to Cell in JTable