How can I put space between the field and the arrow icon for JComboBox?

How can I put space between the field and the arrow icon for JComboBox?
So instead of the down arrow next to the field,                =========\/
there is some space between the arrow and the field     =========   \/

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dejan PažinHead of SW DevelopmentCommented:

Use custom ListCellRenderer for that. Below is the code for the renderer. Use it on your combobox:

mybox.setRenderer(new SpacedListCellRenderer());
import java.awt.*;
import javax.swing.*;
public class SpacedListCellRenderer extends DefaultListCellRenderer {
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Component original = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        // The label will serve as a space between the list and the button. Put as many spaces as you need on the label:
        JLabel label = new JLabel("         ");
        return panel;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it would help us answer you better if you can tell us why you need that!
also you can post some sample code describing the issue!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.