Solved

How to add  MouseListener to Icon in JTable cell?

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

757 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

21 Experts available now in Live!

Get 1:1 Help Now