rhwood/jsplitbutton

Size doesn't display correctly when text is listed below image

jpage4500 opened this issue · 4 comments

Hi,

I just wanted to report this before I forgot. I wanted to create a drop-down button in a toolbar.

My current toolbar buttons shows icons primarily with the label listed below -- like this:
image

The last item "Custom" is trying to use this library to show a drop-down menu. But, as you can see the button size isn't large enough to cover the image and dropdown arrow.

Is this something I'm doing wrong or is this button just not meant to work this way? If it's an easy fix I can look at making it but wanted to get your $.02 first

                Image icon = loadImage("icon_script.png");
                JSplitButton button = new JSplitButton(new ImageIcon(icon));
                button.setText("Custom");
                button.setFont(new Font(Font.SERIF, Font.PLAIN, 10));
                button.setToolTipText("View custom scripts");
                button.setVerticalTextPosition(SwingConstants.BOTTOM);
                button.setHorizontalTextPosition(SwingConstants.CENTER);
                button.setPopupMenu(popup);
                toolbar.add(button);

quick update.. i fixed it (somewhat) but just increasing the size

image
                Dimension preferredSize = button.getPreferredSize();
                preferredSize.width += 25;
                button.setPreferredSize(preferredSize);

I'm considering this a bug, but won't be able to look at it until this weekend.

@jpage4500
I am struggling to recreate the problem you have (I think I can see it, but I'm not able to definitively replicate it). So I have three questions:

  1. What is the LayoutManager for the component the button is embedded in?
  2. What is the Look and Feel being used?
  3. Does this workaround produce acceptable results?
Dimension preferredSize = button.getPreferredSize();
preferredSize.width += button.getSplitWidth() + button.getArrowSize();
button.setPreferredSize(preferredSize);

Update: I now realize that during Covid, I changed the getPreferredSize() method to incorporate the width returned in getSplitWidth() but never published that change, so that might be why I'm not fully able to reproduce what you see. It would be instructive to know if the above works (or is too wide), so I can publish a fix.

Sorry for the slow response -- I just tried the latest version and yes that works great. Thanks!

image