• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tool tip and JComboBox

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I was wondering if there is some way to do a tool tip text on each item in a JComboBox. I have a table that includes a short description, and a long description. Initially, I want to display the short description, and when the user places their mouse over the item, it displays the long description in a tool tip text.
Any ideas??
Thanks for any help!!
 
Philip Pross
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a snippet a code for a tooltip combobox
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import javax.swing.plaf.basic.*;

public class MyComboBox extends JFrame
{
JComboBox cb = new JComboBox();
public static void main(String args[])
{
MyComboBox combo = new MyComboBox();
}
MyComboBox()
{
setSize(200,50);
cb.setRenderer(new ComboBoxRenderer());
cb.addItem("select something");
cb.addItem("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
cb.addItem("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
cb.addItem("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV");
cb.addItem("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
getContentPane().add(cb);
show();
}
class ComboBoxRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent( JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus)
{
if (isSelected)
{
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
if (-1 < index)
{
list.setToolTipText(cb.getItemAt(index).toString());
}
}
else
{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setFont(list.getFont());
setText((value == null) ? "" : value.toString());
return this;
}
}
}
Phil
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another example of how to do this...

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!