开发者

JComboBox change drop-down popup

basically is popup for a JComboBox displayed below its derived JTextField, how can change direction from bellowed orientations for JComboBox's popup and display JComboBox's popup on the top/over that

EDIT: code example for basic JComboBox

import java.awt.Dimension;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

public class HighRowCombo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new HighRowCombo().makeUI();
            }
        });
    }

    public void makeUI() {
        Object[] data = {"One", "Two with text", "Three with long text, with long text,with long text "};
        JComboBox comboBox = new JComboBox(data);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(comboBox);
        frame.pack();
        BasicComboBoxRenderer renderer = (BasicComboBoxRenderer) comboBox.getRenderer();
        Dimension size = renderer.getPreferredSize();
        size.height += 50;
        renderer.setPreferredSize(size);
        frame.setVisible(true);
    }
}

EDIT 2nd. Code for MacOX

开发者_如何学JAVAimport java.awt.*;
import javax.swing.*;

public class TestHighRow {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                new TestHighRow().makeUI();
            }
        });
    }

    public void makeUI() {
        Object[] data = {"One", "Two", "Three"};
        JComboBox comboBox = new JComboBox(data);
        comboBox.setPreferredSize(comboBox.getPreferredSize());
        comboBox.setRenderer(new HighRowRenderer(comboBox.getRenderer()));
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(comboBox);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static class HighRowRenderer implements ListCellRenderer {

        private final ListCellRenderer delegate;
        private int height = -1;

        public HighRowRenderer(ListCellRenderer delegate) {
            this.delegate = delegate;
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component component = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            Dimension size = component.getPreferredSize();
            if (height == -1) {
                height = size.height + 50;
            }
            size.height = height;
            component.setPreferredSize(size);
            if (component instanceof JLabel) {
                ((JLabel) component).setHorizontalTextPosition(JLabel.CENTER);
            }
            return component;
        }
    }
}


Try the setPopupAbove() method found in Combo Box Popup.


On my system, a JComboBox will open upwards if it is near the bottom of the screen and there is not enough space to open it downwards. (Is that what you meant?)

Given that fact, I doubt it would be easy to alter the defined behavior.


On Mac OS X, I get the following exception. Apparently, com.apple.laf.AquaComboBoxRenderer is not a subclass of BasicComboBoxRenderer.

Addendum: The ClassCastException is gone, and a screen capture of "EDIT 2nd. Code for MacOX" appears below:

JComboBox change drop-down popup

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
    com.apple.laf.AquaComboBoxRenderer cannot be cast to
    javax.swing.plaf.basic.BasicComboBoxRenderer
  at HighRowCombo.makeUI(HighRowCombo.java:26)
  at HighRowCombo$1.run(HighRowCombo.java:13)
  at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:678)
  at java.awt.EventQueue.access$000(EventQueue.java:86)
  at java.awt.EventQueue$1.run(EventQueue.java:639)
  at java.awt.EventQueue$1.run(EventQueue.java:637)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:648)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


package com.example.swing;

import javax.swing.JList;
import javax.swing.ListCellRenderer;
import java.awt.Component;
import java.awt.Dimension;

public class PaddingListCellRenderer<Type> implements ListCellRenderer<Type> {
    private final ListCellRenderer original;
    private final int hpadding;
    private final int vpadding;

    public PaddingListCellRenderer(ListCellRenderer original, int hpadding, int vpadding) {
        this.original = original;
        this.hpadding = hpadding;
        this.vpadding = vpadding;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Component getListCellRendererComponent(JList<? extends Type> list, Type value, int index, boolean isSelected, boolean cellHasFocus) {
        final Component component = original.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        component.setPreferredSize(null);
        final Dimension pref = component.getPreferredSize();
        component.setPreferredSize(new Dimension(pref.width + 2 * hpadding, pref.height + 2 * vpadding));
        return component;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜