set ToolTipText!

Hi All,

Could you please me to solve this problem?

I have two Icons (with 2 different ToolTipText on its) display on 2 JButtons. The Buttons are display on a JPanel. The Panel is display on one of the cell inside JTable. Is it possible that when I move the mouse over each Icon it will displays the ToolTipText of that Icon. How do I get the component (JButton) when mouse over?

I have try working with glassPane, addMouseMotionListener, MouseEvent.getPoint(), SwingUtilities.convertPoint, SwingUtilities.getDeepestComponentAt.

Thank you in advance.

Note: I am getting the Icons from Object [].

[631 byte] By [JohnPhama] at [2007-10-1 0:40:04]
# 1

> Is it possible that when I move the mouse over each Icon it will displays the ToolTipText of that Icon.

Icons don't have a setToolTipText(...) method, so I don't understand this statement.

> How do I get the component (JButton) when mouse over?

The component is not added to the JTable, only the image of the components is painted. The JTable is the real component and as such has its own getToolTipText(..) method which you can override based on the mouse location in the table. Here is a simple example:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TableToolTip extends JFrame

{

public TableToolTip()

{

Object[][] data = { {"1", "A"}, {"2", "B"}, {"3", "C"} };

String[] columnNames = {"Number","Letter"};

JTable table = new JTable(data, columnNames)

{

public String getToolTipText( MouseEvent e )

{

int row = rowAtPoint( e.getPoint() );

int column = columnAtPoint( e.getPoint() );

Object value = getValueAt(row, column);

return value == null ? null : value.toString();

}

};

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

}

public static void main(String[] args)

{

TableToolTip frame = new TableToolTip();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setVisible(true);

}

}

So in your case after you determine which cell the mouse is in you would then need to determine the relative location of the mouse within the cell.

I don't know the exact code you would use but the following methods might be helpfull:

a) getCellRect(...) - using the MouseEvent point and the cell rectangle you should be able to determine the relative point within the cell.

b) prepareEditor(...) - this method does return a real component so you should then be able to use the getComponentAt(point) method to find the real compnent. Once you do this your return the ToolTipText associated with this component.

camickra at 2007-7-8 0:53:09 > top of Java-index,Security,Event Handling...
# 2

> Icons don't have a setToolTipText(...) method, so I don't understand this statement.

I am trying to get the ToolTipText of the JButton and the Icon is on top of the JButton.

> The component is not added to the JTable, only the image of the components is painted. The JTable is the real component and as such has its own getToolTipText(..) method which you can override based on the mouse location in the table. Here is a simple example:

This example work great (Thanks!) but that is not what I looking for. I want to replace the ToolTipText of the JTable with the ToolTipText of the JButton when mouse over and when I move the mouse to the next JButton I get a different ToolTipText. I am using addMouseMotionListener() method.

The reason I am using the JButton is that in the future I will add actionListener() to do something when the user click on it.

<code>

// addMouseMotionListener to the table and

// get the value of the seleted cell that have JButtons on it and

// replace the tool tip of the JTable cell with the tool tip of the selected JButton.

table.addMouseMotionListener(new MouseMotionAdapter() {

public void mouseMoved(MouseEvent e) {

Point point = e.getPoint();

// this is work but the x coordinate was hard coded.

// and the user can抰 select the Button.

// if the x coordinate is change (the screen is change) it抯 not working.

int i = point.x;

if (i > 500 && i <530){

// the Object [] also hard coded.

JButton button = (JButton)data[0];

table.setToolTipText(""+button.getToolTipText());

}

if (i > 530 && i <560){

JButton button = (JButton)data[1];

table.setToolTipText(""+button.getToolTipText());

}

if (i > 560 && i <590){

JButton button = (JButton)data[2];

table.setToolTipText(""+button.getToolTipText());

}

}

});

</code>

> So in your case after you determine which cell the mouse is in you would then need to determine the relative location of the mouse within the cell.

> I don't know the exact code you would use but the following methods might be helpfull:

> a) getCellRect(...) - using the MouseEvent point and the cell rectangle you should be able to determine the relative point within the cell.

> b) prepareEditor(...) - this method does return a real component so you should then be able to use the getComponentAt(point) method to find the real compnent. Once you do this your return the ToolTipText associated with this component.

I will try the getCellRect() and prepareEditor() methods.

Thanks!

JohnPhama at 2007-7-8 0:53:09 > top of Java-index,Security,Event Handling...
# 3
I found the result for this at: http://forum.java.sun.com/thread.jspa?messageID=2937017&#2937017Thank you to "weebib"JohnPham
JohnPhama at 2007-7-8 0:53:09 > top of Java-index,Security,Event Handling...