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!

