Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3285440
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:16:02+00:00 2026-05-17T20:16:02+00:00

I’m trying to use a JButton as an editor within a JComboBox. On Mac

  • 0

I’m trying to use a JButton as an editor within a JComboBox. On Mac OS X this looks fine, but on Windows using the system look and feel, there is an ugly gap left between the JButton editor and the combo button itself:

image showing gap between JButton and combo button

This is the test code used to produce the dialog:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class ButtonEditorTest implements Runnable {

    String[] items = {"One", "Two", "Three"};

    ComboBoxModel model;

    ButtonEditorTest() {
        // our model, kept simple for the test
        model = new DefaultComboBoxModel(items);

        // create the UI on the EDT
        SwingUtilities.invokeLater(this);
    }

    // creates UI on the event dispatch thread
    @Override
    public void run() {
        JComboBox comboBox = new JComboBox(model);
        comboBox.setEditable(true);
        comboBox.setEditor(new ComboButtonEditor());

        JFrame frame = new JFrame("JComboBox with JButton editor test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(comboBox, BorderLayout.NORTH);
        frame.setSize(200, 100);
        frame.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        String lookAndFeelClassName = UIManager.getSystemLookAndFeelClassName();
        UIManager.setLookAndFeel(lookAndFeelClassName);
        new ButtonEditorTest();
    }


    class ComboButtonEditor implements ComboBoxEditor {

        private JButton button = new JButton();
        private Object item;

        @Override
        public void addActionListener(ActionListener arg0) {
            // not needed for UI test
        }

        @Override
        public Component getEditorComponent() {
            return button;
        }

        @Override
        public Object getItem() {
            return item;
        }

        @Override
        public void removeActionListener(ActionListener arg0) {
            // not needed for UI test
        }

        @Override
        public void selectAll() {
            // not needed for UI test
        }

        @Override
        public void setItem(Object item) {
            this.item = item;
            button.setText(item.toString());
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:16:03+00:00Added an answer on May 17, 2026 at 8:16 pm

    For some reason the Window LAF is overriding the default layout of the button. This results in the button being narrower. However, the width of the editor is not increased to account for the narrower button so the gap appears. Here is the code from the WindowsComboBoxUI:

    protected LayoutManager createLayoutManager() {
        return new BasicComboBoxUI.ComboBoxLayoutManager() {
        public void layoutContainer(Container parent) {
        super.layoutContainer(parent);
    
        if (XPStyle.getXP() != null && arrowButton != null) {
            Dimension d = parent.getSize();
            Insets insets = getInsets();
            int buttonWidth = arrowButton.getPreferredSize().width;
            arrowButton.setBounds(WindowsGraphicsUtils.isLeftToRight((JComboBox)parent)
          ? (d.width - insets.right - buttonWidth)
          : insets.left,
          insets.top,
          buttonWidth, d.height - insets.top - insets.bottom);
        }
        }
    };
    }
    

    A better layout might be something like:

    comboBox.setUI( new WindowsComboBoxUI()
    {
        @Override
        protected LayoutManager createLayoutManager()
        {
            return new BasicComboBoxUI.ComboBoxLayoutManager()
            {
                public void layoutContainer(Container parent)
                {
                    super.layoutContainer(parent);
    
                    System.out.println(editor.getBounds());
                    System.out.println(arrowButton.getBounds());
    
    //              if (XPStyle.getXP() != null && arrowButton != null)
    //              {
                        Dimension d = parent.getSize();
                        Insets insets = getInsets();
                        int buttonWidth = arrowButton.getPreferredSize().width;
                        boolean isLeftToRight = parent.getComponentOrientation().isLeftToRight();
    
                        arrowButton.setBounds(isLeftToRight
                        ? (d.width - insets.right - buttonWidth)
                        : insets.left, insets.top, buttonWidth, d.height - insets.top - insets.bottom);
    
                        System.out.println(editor.getBounds());
                        System.out.println(arrowButton.getBounds());
    
                        Dimension size = editor.getSize();
                        editor.setSize(arrowButton.getLocation().x - 1, size.height);
    //              }
    
                }
            };
        }
    });
    

    I added some output to show how the editor width changes before/after the XP adjustment. Also I don’t know how to check for the XP LAF since the XPStyle class is not public.

    The imports for the LAF:

    import javax.swing.plaf.basic.*;
    import com.sun.java.swing.plaf.windows.*;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.