Настройка пользовательского интерфейса JComboBox - PullRequest
0 голосов
/ 05 ноября 2018

В настоящее время я работаю над настройкой JComboBox, расширяя BasicComboBoxUI.

`открытый класс 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);
}

}

` С помощью этого кода я могу настроить кнопку со стрелкой в ​​поле со списком, но я не могу настроить все другие функции, такие как границы поля со списком, цвет фона элементов и т. Д. Что можно обойти, чтобы получить те подгоняли?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...