当前位置: 首页 > 面试题库 >

JComboBox更改下拉弹出窗口

龙承颜
2023-03-14
问题内容

基本上是弹出用于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;
        }
    }
}

问题答案:

尝试在“ 组合框弹出”中找到的setPopupAbove()方法。



 类似资料:
  • 问题内容: 我正在使用Bootstrap 3设计页面。我试图在输入元素上使用带有popover的页面。新的Bootstrap可确保您在使用时基本上具有全角输入元素。 HTML代码如下所示: 我认为popover的宽度太小,因为它们在div中没有​​剩余的任何宽度。我想在左侧输入表单,在右侧显示宽弹出窗口。 通常,我正在寻找无需覆盖Bootstrap的解决方案。 附加的JsFiddle。第二个输入选

  • 问题内容: 我有一个包含单个字母值列表的可编辑文件。因此,组合框非常小。 每个字母都有特殊的含义,在很少使用的字母的情况下,有时对用户来说并不清楚。因此,我创建了一个自定义,该自定义显示了下拉列表中每个字母的含义。 不幸的是,此解释不适合下拉菜单,因为它太小了,因为它的宽度与组合框相同。 有什么方法可以使下拉列表比组合框更宽? 这是我要实现的目标: 我无法更改组合框的宽度,因为该应用程序是对旧的旧

  • 大多数情况下,我在寻找一个不用重写引导的解决方案。 附加的jsfiddle。第二个输入选项。没有经常使用jsfiddle,所以不知道,但尝试增加输出框的大小来查看结果,在更小的屏幕上甚至看不到。http://jsfiddle.net/rqx8t/

  • 弹出菜单是可触发的、上下文叠加显示链接列表和别的内容。它们可以与Bootstrap内置的弹出菜单JavaScript插件交互。它通过点击触发,而不是通过鼠标悬停悬浮。这是一个故意设计决策。 示例 把弹出菜单的触发器以及弹出菜单包裹在一个.dropdown中,或者其它声明了position:relative;的元素中。然后,添加菜单的HTML。 <div class="dropdown open">

  • 问题内容: 我正在尝试结合2个jcombobox。1个组合框用于显示费用类别。第二个组合框正在从文本文件读取文件以显示产品类型。如果我更改第一个组合框,我希望第二个组合框将根据用户在第一个组合框中的选择进行更改。 我是否仍有可能从文本文件加载其他组合框?该子项将不是Arrays,而是与以前相同,因为它位于cboperson代码的底部。 编辑的代码: 问题答案: 例如