Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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;
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
247 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...