From Coderanch and Sun Forum, I know the technique to have a horizontal scroll bar for JComboBox.
However, their suggested solution are bounded to Look n Feel specific.
As you can see, the below key code snippet will not work well, if users are under Linux machine with GTK+ look n feel, or Windows machine with Nimbus look n feel.
How I can have a portable way, to make JComboBox able to have a horizontal scroll bar?
The complete source code is AutoCompleteJComboBox.java
The key code snippet are as follow :
package org.yccheok.jstock.gui;
public class AutoCompleteJComboBox extends JComboBox {
@Override
public void setUI(ComboBoxUI ui)
{
if (ui != null)
{
// Let's try our own customized UI.
Class c = ui.getClass();
final String myClass = "org.yccheok.jstock.gui.AutoCompleteJComboBox$My" + c.getSimpleName();
try {
ComboBoxUI myUI = (ComboBoxUI) Class.forName(myClass).newInstance();
super.setUI(myUI);
return;
} catch (ClassNotFoundException ex) {
log.error(null, ex);
} catch (InstantiationException ex) {
log.error(null, ex);
} catch (IllegalAccessException ex) {
log.error(null, ex);
}
}
// Either null, or we fail to use our own customized UI.
// Fall back to default.
super.setUI(ui);
}
// This is a non-portable method to make combo box horizontal scroll bar.
// Whenever there is a new look-n-feel, we need to manually provide the ComboBoxUI.
// Any idea on how to make this portable?
//
protected static class MyWindowsComboBoxUI extends com.sun.java.swing.plaf.windows.WindowsComboBoxUI
{
@Override
protected ComboPopup createPopup()
{
return new MyComboPopup(comboBox);
}
}
protected static class MyMotifComboBoxUI extends com.sun.java.swing.plaf.motif.MotifComboBoxUI
{
@Override
protected ComboPopup createPopup()
{
return new MyComboPopup(comboBox);
}
}
protected static class MyMetalComboBoxUI extends javax.swing.plaf.metal.MetalComboBoxUI
{
@Override
protected ComboPopup createPopup()
{
return new MyComboPopup(comboBox);
}
}
private static class MyComboPopup extends BasicComboPopup
{
public MyComboPopup(JComboBox combo)
{
super(combo);
}
@Override
public JScrollPane createScroller()
{
return new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
}
}