JComboBox用户界面自定义

时间:2018-11-05 15:52:37

标签: java swing user-interface jcombobox

我目前正在通过扩展BasicComboBoxUI来进行JComboBox自定义。

`公共类MyComboBoxUI扩展了BasicComboBoxUI {

public Color transparentColor= new Color(0xff,0xff,0xff,1);
private boolean sameBaseline;
protected boolean   hasFocus = false;

public static ComboBoxUI createUI(JComponent c) {
    return new  MyComboBoxUI();
}

 protected void installDefaults() {
    LookAndFeel.installColorsAndFont( comboBox,
                                      "ComboBox.background",
                                      "ComboBox.foreground",
                                      "ComboBox.font" );
    LookAndFeel.installBorder( comboBox, "ComboBox.border" );
    LookAndFeel.installProperty( comboBox, "opaque", Boolean.TRUE);
}

@Override protected JButton createArrowButton() {
    return new BasicArrowButton(
        BasicArrowButton.SOUTH,
        Color.BLACK, transparentColor,
        Color.gray, transparentColor);
}

    @Override
public int getBaseline(JComponent c, int width, int height) {
    super.getBaseline(c, width, height);
    int baseline = -1;
    // force sameBaseline to be updated.
    getDisplaySize();
    if (sameBaseline) {
        Insets insets = c.getInsets();
        height = height - insets.top - insets.bottom;
        if (!comboBox.isEditable()) {
            ListCellRenderer renderer = comboBox.getRenderer();
            if (renderer == null)  {
                renderer = new DefaultListCellRenderer();
            }
            Object value = null;
            Object prototypeValue = comboBox.getPrototypeDisplayValue();
            if (prototypeValue != null)  {
                value = prototypeValue;
            }
            else if (comboBox.getModel().getSize() > 0) {
                // Note, we're assuming the baseline is the same for all
                // cells, if not, this needs to loop through all.
                value = comboBox.getModel().getElementAt(0);
            }
            Component component = renderer.
                    getListCellRendererComponent(listBox, value, -1,
                                                 false, false);
            if (component instanceof JLabel) {
                JLabel label = (JLabel) component;
                String text = label.getText();
                if ((text == null) || text.isEmpty()) {
                    label.setText(" ");
                }
            }
            if (component instanceof JComponent) {
                component.setFont(comboBox.getFont());
            }
            baseline = component.getBaseline(width, height);
        }
        else {
            baseline = editor.getBaseline(width, height);
        }
        if (baseline > 0) {
            baseline += insets.top;
        }
    }
    return baseline;
}

public void paintCurrentValue(Graphics g,Rectangle bounds,boolean hasFocus) {
    ListCellRenderer renderer = comboBox.getRenderer();
    Component c;

    if ( hasFocus && !isPopupVisible(comboBox) ) {
        c = renderer.getListCellRendererComponent( listBox,
                                                   comboBox.getSelectedItem(),
                                                   -1,
                                                   true,
                                                   false );
    }
    else {
        c = renderer.getListCellRendererComponent( listBox,
                                                   comboBox.getSelectedItem(),
                                                   -1,
                                                   false,
                                                   false );
        c.setBackground(UIManager.getColor("ComboBox.background"));
    }
    c.setFont(comboBox.getFont());
    if ( hasFocus && !isPopupVisible(comboBox) ) {
        c.setForeground(listBox.getSelectionForeground());
        c.setBackground(listBox.getSelectionBackground());
    }
    else {
        if ( comboBox.isEnabled() ) {
            c.setForeground(comboBox.getForeground());
            c.setBackground(comboBox.getBackground());
        }
        else {
            c.setForeground(DefaultLookup.getColor(
                     comboBox, this, "ComboBox.disabledForeground", null));
            c.setBackground(DefaultLookup.getColor(
                     comboBox, this, "ComboBox.disabledBackground", null));
        }
    }

    // Fix for 4238829: should lay out the JPanel.
    boolean shouldValidate = false;
    if (c instanceof JPanel)  {
        shouldValidate = true;
    }

    int x = bounds.x, y = bounds.y, w = bounds.width, h = bounds.height;
    if (padding != null) {
        x = bounds.x + padding.left;
        y = bounds.y + padding.top;
        w = bounds.width - (padding.left + padding.right);
        h = bounds.height - (padding.top + padding.bottom);
    }

    currentValuePane.paintComponent(g,c,comboBox,x,y,w,h,shouldValidate);
}

/**
 * Paints the background of the currently selected item.
 */
public void paintCurrentValueBackground(Graphics g,Rectangle bounds,boolean hasFocus) {
    Color t = g.getColor();
    if ( comboBox.isEnabled() )
        g.setColor(DefaultLookup.getColor(comboBox, this,
                                          "ComboBox.background", null));
    else
        g.setColor(DefaultLookup.getColor(comboBox, this,
                                 "ComboBox.disabledBackground", null));
    g.fillRect(bounds.x,bounds.y,bounds.width,bounds.height);
    g.setColor(t);
}

}

` 借助此代码,我可以自定义组合框的箭头按钮,但无法自定义所有其他功能,例如组合框的边框,项目的背景色等。那些定制的?

0 个答案:

没有答案