steppedcombobox.java

来自「自制java的swing组件」· Java 代码 · 共 81 行

JAVA
81
字号
package zb.swing;

import java.util.*;

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;

/**
 * @version 1.0 12/12/98
 */
class SteppedComboBoxUI
    extends MetalComboBoxUI {
  protected ComboPopup createPopup() {
    BasicComboPopup popup = new BasicComboPopup(comboBox) {

      public void show() {
        Dimension popupSize = ( (SteppedComboBox) comboBox).getPopupSize();
        popupSize.setSize(popupSize.width,
                          getPopupHeightForRowCount(comboBox.getMaximumRowCount()));
        Rectangle popupBounds = computePopupBounds(0,
            comboBox.getBounds().height, popupSize.width, popupSize.height);
        scroller.setMaximumSize(popupBounds.getSize());
        scroller.setPreferredSize(popupBounds.getSize());
        scroller.setMinimumSize(popupBounds.getSize());
        list.invalidate();
        int selectedIndex = comboBox.getSelectedIndex();
        if (selectedIndex == -1) {
          list.clearSelection();
        }
        else {
          list.setSelectedIndex(selectedIndex);
        }
        list.ensureIndexIsVisible(list.getSelectedIndex());
        setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled());

        show(comboBox, popupBounds.x, popupBounds.y);
      }
    };
    popup.getAccessibleContext().setAccessibleParent(comboBox);

    return popup;
  }
}

public class SteppedComboBox
    extends JComboBox {
  protected int popupWidth;

  public SteppedComboBox(ComboBoxModel aModel) {
    super(aModel);
    setUI(new SteppedComboBoxUI());
    popupWidth = 0;
  }

  public SteppedComboBox(final Object[] items) {
    super(items);
    setUI(new SteppedComboBoxUI());
    popupWidth = 0;
  }

  public SteppedComboBox(Vector items) {
    super(items);
    setUI(new SteppedComboBoxUI());
    popupWidth = 0;
  }

  public void setPopupWidth(int width) {
    popupWidth = width;
  }

  public Dimension getPopupSize() {
    Dimension size = getSize();
    if (popupWidth < 1) {
      popupWidth = size.width;
    }
    return new Dimension(popupWidth, size.height);
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?