|
-
March 10th, 2010, 10:58 PM
#1
Override BasicComboBoxUI and lose Look and Feel
I have a ScrollableComboBox class that basically allows me to horizontally scroll the list if the ComboBox is not wide enough. When I Use this class, the look and feel is different from a regular JComboBox.
There is no border and the arrow is grayed out.
Is there any way to maintain the look and feel of a regular ComboBox while still adding the horizontal scroll bar?
Would appreciate some help with this.
Thanks!
import com.lgc.calcSharedLight.ui.object.ScrollableComboBox;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
public class ComboBox extends JFrame {
String [] _items = {"Item Number One", "Item Number Two", "Item Number Three"};
public ComboBox() {
super("ComboBox");
getContentPane().setLayout(new BorderLayout());
JPanel mainPanel = new JPanel();
BoxLayout mainLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
JLabel label1 = new JLabel("Normal ComboBox");
JPanel label1Panel = new JPanel();
JComboBox label1ComboBox = new JComboBox(_items);
label1Panel.setLayout( new FlowLayout(FlowLayout.LEFT));
label1Panel.add(label1);
label1Panel.add(label1ComboBox);
JLabel label2 = new JLabel("Scrolled ComboBox");
JPanel label2Panel = new JPanel();
ScrollableComboBox label2ComboBox = new ScrollableComboBox(_items);
label2Panel.setLayout( new FlowLayout(FlowLayout.LEFT));
label2Panel.add(label2);
label2Panel.add(label2ComboBox);
mainPanel.add(label1Panel);
mainPanel.add(label2Panel);
getContentPane().add(mainPanel);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
// setResizable(false);
pack();
setVisible(true);
}
public static void main(String argv[]) {
new ComboBox();
}
}
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.basic.BasicComboPopup;
/**
* When all text does not fit in ComboBox, scrollable comboBox is useful
*/
public class ScrollableComboBox extends JComboBox{
/**
* Default Constructor
* Empty ScrollableComboBox
*/
public ScrollableComboBox()
{
super();
setUI(new myComboUI());
}
/**
* Populate ComboBox with array of items
* @param items
*/
public ScrollableComboBox(final Object items[])
{
super(items);
setUI(new myComboUI());
}
public class myComboUI extends BasicComboBoxUI
{
protected ComboPopup createPopup()
{
BasicComboPopup popup = new BasicComboPopup(comboBox)
{
protected JScrollPane createScroller()
{
return new JScrollPane( list, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
}
};
return popup;
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|