Change the width of the JCombobox popup

How to set fixed width of JComboboxs popup menu that uses GridBagLayout and fill=HORIZONTAL ?

One of the things I've tried is just to override the getSize() method, but the dose does not work.

 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) { public Dimension getSize() { Dimension d = getPreferredSize(); d.width = 50; return d; } }; comboBox.setMaximumRowCount(comboBox.getModel().getSize()); // 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); } } 
0
source share
1 answer

Here is the solution, it worked for me, add this PopupMenuListener to the 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); } } } } 

An example in your code:

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

Source: click here

0
source

Source: https://habr.com/ru/post/1243219/


All Articles