更改JCombobox弹出窗口宽度

时间:2014-01-08 12:21:45

标签: java swing layout jcombobox gridbaglayout

如何设置使用JComboboxsGridBagLayout的{​​{1}}弹出式菜单的固定宽度?

我尝试过的其中一件事就是覆盖fill=HORIZONTAL方法但不起作用。

getSize()

1 个答案:

答案 0 :(得分:0)

这是解决方案,这对我有用,将此PopupMenuListener添加到您的JComboBox:

import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.basic.ComboPopup;

public class CustomComboBoxPopupMenuListener implements PopupMenuListener {
    // ==============================================================================
    // Members
    // ==============================================================================
    private int bgTop = 0;
    private int bgLeft = 0;
    private int bgRight = 0;
    private int bgBottom = 0;

    // ==============================================================================
    // Constructors
    // ==============================================================================
    public CustomComboBoxPopupMenuListener() {
        super();
    }

    // ==============================================================================
    // Methods
    // ==============================================================================
    public void popupMenuCanceled(PopupMenuEvent e) {

    }

    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {

    }

    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        final JComboBox box = (JComboBox) e.getSource();
        final Object comp = box.getUI().getAccessibleChild(box, 0);
        if (!(comp instanceof JPopupMenu)) {
            return;
        }
        final JPopupMenu popupMenu = (JPopupMenu) comp;
        popupMenu.setBorder(null);
        if (popupMenu.getComponent(0) instanceof JScrollPane) {
        final JScrollPane scrollPane = (JScrollPane) popupMenu
                .getComponent(0);
        scrollPane.setBorder(BorderFactory.createEmptyBorder(bgTop, bgLeft,
                bgBottom, bgRight));
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        if (popupMenu instanceof ComboPopup) {
            final ComboPopup popup = (ComboPopup) popupMenu;
            final JList list = popup.getList();
            list.setBorder(null);
            final Dimension size = list.getPreferredSize();
            size.width = Math.max(box.getPreferredSize().width + bgLeft
                    + bgRight, box.getWidth());
            size.height = Math.min(scrollPane.getPreferredSize().height
                    + bgTop + bgBottom, size.height + bgTop + bgBottom);
            scrollPane.setPreferredSize(size);
            scrollPane.setMaximumSize(size);
        }
    }
}
}

代码中的示例:

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ComboBoxSize extends JFrame {
public static void main(String args[]) {

    // THE COMBOBOX
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    JComboBox<String> comboBox = new JComboBox<String>(labels);
    comboBox.setMaximumRowCount(comboBox.getModel().getSize());
    comboBox.addPopupMenuListener(new CustomComboBoxPopupMenuListener());
    // ADD COMBOBOX TO PANEL
    JPanel panel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    panel.add(comboBox, c);

    // ADD PANEL TO FRAME
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
}
}

来源:click here