Solved

How to add  MouseListener to Icon in JTable cell?

Posted on 2008-06-26
3
2,899 Views
Last Modified: 2013-12-14
Hi Experts,
In my JTable , I am using custom cellRenderer to add the  icon depending upon value of cell.
Table cell shows icon followed by text, I need to open new JMessageDialog when user clicks this icon .

How I can add  mouselistner to this icon ?

public class DescriptionCellRenderer extends DefaultTableCellRenderer {
 
	
	 public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
 Component cell = super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);
		
		String val=value.toString();
		
		if (!val.equals("ok")){
							ImageIcon icon = new ImageIcon("help.gif");
			setText(val);
			setIcon(icon);
 
		}		
		return cell;
	 
	    }
 
	}

Open in new window

0
Comment
Question by:inditrozen
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 21879871
you don't, you add your listener to the table. Then calculate which cell was clicked using rowAtPoint(), and columnAtPoint()


0
 

Author Comment

by:inditrozen
ID: 21882920

I added listner to table , and now when I click on the cell new dialog will open.
Now this cell contains icon followed by text. And my requirement is ,when  user clicks on icon then only dialog message should open.and when he clicks on text ,nothing should happen.
How can listener identify difference between icon and text.?
Is it possible to get the cell value at perticular position?

Thanks in advance
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 21888249
yes, use the getValueAt() method of the table/model

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
error when i compile 5 714
Oracle TopLink example 3 91
netbeans not showing web project 3 87
VS can't find System.IO.Packaging 5 638
In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

789 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