0

I have a JComboBox where i use the method setPrototypeDisplayValue to make it wide enough for the combobox to display the whole value of the widest value.

Gui.form

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="gui.Gui">
  <grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
    <margin top="0" left="0" bottom="0" right="0"/>
    <constraints>
      <xy x="313" y="137" width="252" height="86"/>
    </constraints>
    <properties/>
    <border type="none"/>
    <children>
      <component id="2aff5" class="javax.swing.JComboBox" binding="comboBox">
        <constraints>
          <grid row="0" column="0" row-span="2" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="1" use-parent-layout="false">
            <minimum-size width="150" height="24"/>
            <preferred-size width="180" height="24"/>
          </grid>
        </constraints>
        <properties>
          <foreground color="-16777216"/>
          <maximumRowCount value="2"/>
          <model>
            <item value="stringOfLengthEqualTo23"/>
            <item value="stringOfLengthEqualTo23"/>
            <item value="stringOfLengthGreaterThan23"/>
          </model>
        </properties>
      </component>
    </children>
  </grid>
</form>


Gui.java

package gui;

import javax.swing.*;

public class Gui {
    private JComboBox<String> comboBox;
    private JPanel panel;

    public Gui(){
        initComponents();
    }

    public void initComponents(){
        comboBox = new JComboBox();
        comboBox.setMaximumRowCount(2); //max row count is 2
        comboBox.setMinimumSize(new java.awt.Dimension(150, 24));
        comboBox.setName("connectedHardwareTool"); // NOI18N
        comboBox.setPreferredSize(new java.awt.Dimension(180, 24));
        comboBox.setPrototypeDisplayValue("stringOfLengthGreaterThan23");
    }

    public static void main(String[] args){

        JFrame frame = new JFrame();
        frame.setContentPane(new Gui().panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

maxRowCount is set to 2 if 3 tools are connected, the scrollbar comes into picture and the dropdown is not as wide as per the prototype value

Why is this happening?

P.S. I have tried extending the popupmenulistener to increase the width, when the pop up becomes visible but that gives a completely different issue that i don't want to get into now.

FiddleBug
  • 177
  • 2
  • 11
  • 1
    I see no change in the combo box width. I use JDK11 on Windows 10. Post your [mre] demonstrating the problem. – camickr Nov 10 '20 at 19:30
  • I am currently building the minimal reproducible example as requested. Something i forgot to mention in the original description is that i use a toggle to switch the items that are populated in the combobox, and it is while toggling is when i get the width issue. – FiddleBug Nov 10 '20 at 23:06
  • *it is while toggling is when i get the width issue.* - and that is why a [mre] should be posted with every question. We can't guess what you may or may not be doing. It only takes 5 minutes to create an "MRE". – camickr Nov 10 '20 at 23:59
  • That is not an [mre]. We can't copy/paste/compile and test that code. The point of the "MRE" is to simplify the problem to isolate the condition that causes the problem. This will often mean hardcoding data, for example to recreate the items in the combo box, instead of getting the items from an external source. – camickr Nov 11 '20 at 15:44
  • I've updated the problem with the "minimal reproducible example" – FiddleBug Nov 11 '20 at 16:58
  • I don't use Intellij, so that form definition means nothing to me. An MRE should not assume usage of an IDE. Here is an example of an MRE: https://stackoverflow.com/questions/7109798/how-to-update-2-jcombo-boxs/7110051#7110051. The data is hard code and is fully contained in a single class. Just realizes it even uses the prototype display value and changes data dynamically without a problem, so I'm not sure why you have a problem. Update that code to demonstrate your problem or maybe that code will fix your problem? – camickr Nov 11 '20 at 17:25
  • 1
    A prototype display value and an explicit preferred size set via `comboBox.setPreferredSize(…)` are obviously conflicting. – Holger Nov 12 '20 at 11:41
  • @holger I have removed both preferred size and minimum size but it still gives me the issue. Also, i don't get this problem when the number of items is less than max row count regardless of preferred size – FiddleBug Nov 12 '20 at 16:38
  • 1
    You didn’t describe the problem sufficiently. The popup is as wide as the combobox. Whether there’s a scrollbar or not, doesn’t change that. Further note that your example code never initializes `panel`. – Holger Nov 12 '20 at 16:41
  • Apologies. I am not very experience in GUI development. Panel hasn't been initialized indeed but somehow it still works for me. Although if i initialize the Panel and add the combobox to the panel, then the problem resolves :) – FiddleBug Nov 12 '20 at 17:29
  • 1
    When the code can’t work that way (`comboBox` never added to anything and `panel` is always `null`), but appears to work, it’s a strong sign that the code that is actually running is not the code you’re looking at (resp. the code you’ve posted). – Holger Nov 13 '20 at 10:47
  • The MRE i posted actually works as expected if i set the model in the initComponents method. The original code is a very complex legacy code and is not worth reducing into the MRE. I have found a work around though, i don't have to use setPrototypeDisplayValue and instead i can override the getPreferredSize method of BasicComboBoxRenderer by creating a custom comboboxrenderer. This will increase the width of the combobox as per the size of the largest item (which was the original feature from the beginning) – FiddleBug Nov 16 '20 at 22:23

0 Answers0