I am creating a combobox that shows a tooltip when the display area is too small to show all text of the selected item.
The problem I have with the solution I have so far:
- It does not maintain the look and feel of a regular combobox.
- I'm not sure my solution works on systems other than Windows systems. What I came up with is by setting breakpoints, and see what I need to override to get the proper sizing: I overrode
DefaultListCellRenderer.setBounds
to do that.
The little MCVE I included at the bottom shows what I came up with. It shows four comboboxes. The first and third are regular untweaked JComboBox instances, the second and fourth are tweaked comboboxes to show tooltips if the display area is too small for the text. The first and second are enabled, the third and fourth disabled; this to better show the difference in look and feel. A snapshot:
How can I maintain the look and feel of a regular combobox and have a tooltip when the display area becomes too small? Am I on the right path and if so, what do I need to do further? If I am not, what should I do instead?
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ComboWithTooltip {
public static void main(String[] args) {
try {
// On my system: "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName( ));
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
System.err.println("Can't set look and feel");
return;
}
EventQueue.invokeLater (
new Runnable() {
@Override
public void run() {
buildFrame().setVisible(true);
}
}
);
}
static JFrame buildFrame() {
JPanel contentPane = new JPanel();
BoxLayout layout = new BoxLayout(contentPane, BoxLayout.Y_AXIS);
contentPane.setLayout(layout);
contentPane.add(Box.createVerticalStrut(25));
contentPane.add(createDefaultCombo(true/*enabled*/));
contentPane.add(Box.createVerticalStrut(25));
contentPane.add(createTweakedCombo(true/*enabled*/));
contentPane.add(Box.createVerticalStrut(25));
contentPane.add(createDefaultCombo(false/*enabled*/));
contentPane.add(Box.createVerticalStrut(25));
contentPane.add(createTweakedCombo(false/*enabled*/));
contentPane.add(Box.createVerticalStrut(25));
JFrame frame = new JFrame();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
private final static String[] items = new String[]{ "Long text 1234444444444444444444444", "Some more of that long texttttttttttttttttttttt", "The longer the text, the harder to read it becomessssssssssssssssssssssssssssssssssssssss" };
@SuppressWarnings("serial")
private static JComboBox<String> createDefaultCombo(boolean enabled) {
JComboBox<String> comboBoxDefault = new JComboBox<String>(items) {
@Override
public Dimension getPreferredSize() {
return new Dimension(100,25); // intentionally too small
}
};
comboBoxDefault.setEnabled(enabled);
return comboBoxDefault;
}
@SuppressWarnings("serial")
private static class JComboBoxTweaked extends JComboBox<String> {
public JComboBoxTweaked(String[] items) {
super(items);
setRenderer (
new DefaultListCellRenderer() {
@Override
public void setBounds(int x, int y, int width, int height) {
super.setBounds( x, y, width, height );
if( width != 0 ) {
if( getPreferredSize( ).width > getSize( ).width )
JComboBoxTweaked.this.setToolTipText( getText( ) );
else
JComboBoxTweaked.this.setToolTipText( null );
}
}
}
);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(100,25); // intentionally too small
}
};
private static JComboBox<String> createTweakedCombo(boolean enabled) {
JComboBox<String> comboBoxTweaked = new JComboBoxTweaked(items);
comboBoxTweaked.setEnabled(enabled);
return comboBoxTweaked;
}
}
getPreferredSize
method, but it' usually unproblematic).