MyComboBoxUI.java Source code

Java tutorial

Introduction

Here is the source code for MyComboBoxUI.java

Source

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;

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

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

public class PopupComboSample {
    public static void main(String args[]) {
        String labels[] = { "A", "B", "C", "D" };
        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);

        JComboBox comboBox2 = new JComboBox(labels);
        frame.add(comboBox2, BorderLayout.SOUTH);

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