trouble with tooltips for jcomponent located inside jtable cell

So heres my problem:

I have a jtable which has a jpanel inside its cells for 1 column. Now Inside this jpanel is a textbox and a button. Basically, what i want to do is have a tooltip show up when the mouse hovers over the textbox only, not when the mouse is over the entire cell which contains the jpanel.

I tried adding a mouseMotionListener to the textbox, but that only works if the textbox has focus, which means it needs to be clicked on first, which isn't what i want.

So then I tried something like this:

public String getToolTipText(MouseEvent me){

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

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

}

with the getValueAt() method, but the problem is, I still cant figure out if the mouse is over the textbox or not, and thus the tooltip shows for the whole cell. Here is a working example (note: I got this example from this forum, and tweaked it to my liking):

import org.jdesktop.swingx.table.ColumnHeaderRenderer;

import javax.swing.*;

import javax.swing.table.TableCellEditor;

import javax.swing.table.TableCellRenderer;

import java.util.EventObject;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.*;

publicclass TableTestextends JFrame{

private JTable table;

public TableTest(){

setDefaultCloseOperation(EXIT_ON_CLOSE);

Object[][] moa =new Object[][]{

{new MultiObject("Text1",true),"String1"},

{new MultiObject("Text2",false),"String2"}};

table =new JTable(moa,new String[]{"Header1","Header2"}){

public String getToolTipText(MouseEvent me){

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

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

MultiObject o = (MultiObject) getValueAt(row, column);

return o.fText;

}

};

TableCellRenderer tcr =new MultiRenderer();

table.getColumnModel().getColumn(0).setCellRenderer(tcr);

table.getColumnModel().getColumn(0).setCellEditor(new MultiEditor());

table.setShowGrid(false);

table.setIntercellSpacing(new Dimension(3,20));

table.setRowHeight(50);

table.setRowSelectionAllowed(false);

table.setBackground(getContentPane().getBackground());

((ColumnHeaderRenderer)table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(SwingConstants.CENTER);

getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);

pack();

}

publicstaticvoid main(String args[]){

try{

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}catch (Exception e){

e.printStackTrace();

}

EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new TableTest().setVisible(true);

}

});

}

}

class MultiRendererextends JPanelimplements TableCellRenderer{

private JTextField fText;

private JButton fSelection;

public MultiRenderer(){

setLayout(new FlowLayout());

fText =new JTextField("Test");

add(fText);

fSelection =new JButton();

add(fSelection);

}

public Component getTableCellRendererComponent(JTable table, Object value,

boolean isSelected,boolean hasFocus,int row,int column){

MultiObject obj = (MultiObject) value;

fText.setText(obj.fText);

if(obj.read){

fSelection.setText("Option1");

}

else{

fSelection.setText("Option2");

}

setFont(table.getFont());

returnthis;

}

}

class MultiEditorextends AbstractCellEditorimplements ActionListener,

TableCellEditor{

private JPanel fPanel;

private JTextField fText;

private JButton fSelection;

private MultiObject fValue;

public MultiEditor(){

fPanel =new JPanel();

fPanel.setLayout(new FlowLayout());

fText =new JTextField("Test");

fText.addActionListener(this);

fPanel.add(fText);

fSelection =new JButton();

fSelection.addActionListener(this);

fPanel.add(fSelection);

}

public Component getTableCellEditorComponent(JTable table, Object value,

boolean isSelected,int row,int column){

fValue = (MultiObject) value;

fText.setText(fValue.fText);

if(fValue.read){

fSelection.setText("Option1");

}

else{

fSelection.setText("Option2");

}

return fPanel;

}

public Object getCellEditorValue(){

fValue.fText = fText.getText();

return fValue;

}

publicboolean isCellEditable(EventObject anEvent){

returntrue;

}

publicboolean shouldSelectCell(EventObject anEvent){

returntrue;

}

publicvoid actionPerformed(ActionEvent e){

MultiEditor.this.stopCellEditing();

}

}

class MultiObject{

public String fText;

publicboolean read;

public MultiObject(String text,boolean read){

fText = text;

this.read = read;

}

}

any ideas? thanks!

[10364 byte] By [gmonieya] at [2007-11-27 3:07:00]
# 1

Whats a textbox?

Well, I guess you have to convert the MousePoint to determine where in the cell it is positioned. You can get the TableColumnModel from the table and then get the TableColumn. Once you have the TableColumn you can get the width of the column. Assuming you know the fixed with of the button you should be able to calculate whether the mouse position is over the button or not.

camickra at 2007-7-12 3:53:42 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi camickr,

thanks for the reply. I mean textfield (i.e. JTextField)...sorry for the mixup.

As for your suggestion. I have been playing with finding the location of the mousepoint versus the location of my textfield, however, I havent been able to figure out a programmatic way of finding the exact location of the textfield (i.e. where it is placed within the cell), because there is a bit of a gap from the edge of the JPanel (top & left edges) to the beginning of the JTextField.

I tried to grab the location of the textfield, but it returns 0,0 (which i am assuming is a result of being embedded within a JTable)

gmonieya at 2007-7-12 3:53:42 > top of Java-index,Desktop,Core GUI APIs...
# 3
i figured it out....my problem was that i was getting the JtextField from the Editor, rather than the rendered....
gmonieya at 2007-7-12 3:53:43 > top of Java-index,Desktop,Core GUI APIs...
# 4

So,

I thought i figured out my problem, but after testing it some more, i am stuck yet again.

The correct tooltips show in the right places is the cell is not being EDITED, basically, if you are typing anything into the textfield, the tooltip is not displayed until the focus is removed.

I was hoping to show a tooltip as the user typed, so i tried the following:

textField.addMouseMotionListener(new MouseMotionAdapter(){

public void mouseMoved(MouseEvent me) {

textField.setToolTipText(regObject.getTooltipText());

System.err.println(regObject.getTooltipText(textField.getText()));

}

});

but it doesnt display the tooltip (the event is being fired however, since the print statements appear...

any ideas?

gmonieya at 2007-7-12 3:53:43 > top of Java-index,Desktop,Core GUI APIs...
# 5
figured it out....i was accidentally sending NULL....
gmonieya at 2007-7-12 3:53:43 > top of Java-index,Desktop,Core GUI APIs...
# 6
could you post what you changed? Thanks!
geapia at 2007-7-12 3:53:43 > top of Java-index,Desktop,Core GUI APIs...