JComboBox更改下拉式弹出式窗口 [英] JComboBox change drop-down popup

查看:118
本文介绍了JComboBox更改下拉式弹出式窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上是一个 JComboBox 的弹出式窗口,显示在其派生的JTextField,如何从JComboBox弹出窗口的方向改变方向,并在顶部/底部显示JComboBox的弹出窗口。

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

编辑:基本JComboBox的代码示例

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

编辑第二。 MacOX代码

EDIT 2nd. Code for 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;
        }
    }
}


推荐答案

尝试在组合框弹出窗口中找到的setPopupAbove()方法/ a>。

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

这篇关于JComboBox更改下拉式弹出式窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆