Using Customized ComboBoxUI : UI Delegate « Swing « Java Tutorial






Using Customized ComboBoxUI
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);
  }
}








14.120.UI Delegate
14.120.1.Creating a New UI Delegate
14.120.2.Using Customized ComboBoxUIUsing Customized ComboBoxUI