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

Hi,
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     =========   \/

Thanks
Jamie
jamie_lynnAsked:
Who is Participating?
 
Dejan PažinConnect With a Mentor Head 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));
        panel.add(original);
        // 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("         ");
        panel.add(label);
        return panel;
    }
}

Open in new window

0
 
ksivananthCommented:
it would help us answer you better if you can tell us why you need that!
0
 
ksivananthCommented:
also you can post some sample code describing the issue!
0
All Courses

From novice to tech pro — start learning today.