Solved

How to add  MouseListener to Icon in JTable cell?

Posted on 2008-06-26
3
2,903 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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. …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

730 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