Customize JComboBox with BasicComboBoxUI in Java

Description

The following code shows how to customize JComboBox with BasicComboBoxUI.

Example


//from  ww  w. j  a  v  a 2s  .  c  o  m
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.plaf.ComboBoxUI;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.plaf.basic.BasicComboBoxUI;

public class Main {
  public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G" };
    JFrame frame = new JFrame("Popup JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    comboBox.setUI((ComboBoxUI) MyComboBoxUI.createUI(comboBox));
    frame.add(comboBox, BorderLayout.NORTH);

    frame.setSize(300, 200);
    frame.setVisible(true);
  }


}   
class MyComboBoxUI extends BasicComboBoxUI {
  public static ComponentUI createUI(JComponent c) {
    return new MyComboBoxUI();
  }

  protected JButton createArrowButton() {
    JButton button = new BasicArrowButton(BasicArrowButton.EAST);
    return button;
  }
}

The code above generates the following result.

Customize JComboBox with BasicComboBoxUI in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer