JComboBox更改下拉弹出窗口

时间:2011-06-05 10:29:59

标签: java swing drop-down-menu popup jcombobox

基本上是弹出的JComboBox显示在其派生的JTextField下面,如何改变JComboBox弹出窗口的方向,并在顶部/上方显示JComboBox的弹出窗口

编辑:基本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);
    }
}

编辑第二。 MacOX代码

import 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;
        }
    }
}

4 个答案:

答案 0 :(得分:2)

在我的系统上,如果JComboBox靠近屏幕底部并且没有足够的空间向下打开,{{1}}将向上打开。 (这是你的意思吗?)

鉴于这一事实,我怀疑改变定义的行为会很容易。

答案 1 :(得分:2)

在Mac OS X上,我收到以下异常。显然,com.apple.laf.AquaComboBoxRenderer不是BasicComboBoxRenderer的子类。

附录:ClassCastException已消失,下面显示“EDIT 2nd.Code for MacOX”的屏幕截图:

enter image description here

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)

答案 2 :(得分:2)

尝试Combo Box Popup中的setPopupAbove()方法。

答案 3 :(得分:0)

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;
    }
}