Is there any way to right align the text in a JCombobox
Asked Answered
F

3

10

I want to have a JComboBox with right align. how can I do that? someone before said "You can set a renderer to the JComboBox which can be a JLabel having JLabel#setHorizontalAlignment(JLabel.RIGHT)" but I don't know how can I do that?

Fake answered 28/10, 2013 at 17:36 Comment(0)
R
18

someone before said "You can set a renderer to the JComboBox which can be a JLabel having JLabel#setHorizontalAlignment(JLabel.RIGHT)"

Yes, the default renederer is a JLabel so you don't need to create a custom renderer. You can just use:

((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.RIGHT);
Rase answered 28/10, 2013 at 18:48 Comment(2)
+1, your answer is better than mine, thanks for this information.Undress
Idk...I think a better answer still would be a combination of your two answers. In particular, your answer is more useful to me because I am using my own custom renderer. On the other hand, I can see how if you are just using a plain array of Strings in the Model (or the toString() for the objects in the combobox is good enough), then it would be more useful to do this.Checkroom
U
7

Well, you can do with ListCellRenderer, like this:

import java.awt.Component;
import java.awt.ComponentOrientation;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.SwingUtilities;

public class ComboboxDemo extends JFrame{
    public ComboboxDemo(){
        JComboBox<String> comboBox = new JComboBox<String>();
        comboBox.setRenderer(new MyListCellRenderer());
        comboBox.addItem("Hi");
        comboBox.addItem("Hello");
        comboBox.addItem("How are you?");

        getContentPane().add(comboBox, "North");
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private static class MyListCellRenderer extends DefaultListCellRenderer {
        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            component.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            return component;
        }
    }

    public static void main(String [] args){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ComboboxDemo().setVisible(true);
            }
        });
    }
}
Undress answered 28/10, 2013 at 17:59 Comment(1)
i cant choose orientation CENTER !! only left to right and right to leftActon
A
0

This worked for me nice and short

comboFromDuration.setRenderer(new DefaultListCellRenderer() {
            @Override
            public void paint(Graphics g) {
                setHorizontalAlignment(DefaultListCellRenderer.CENTER);
                setBackground(Color.WHITE);
                setForeground(Color.GRAY);  
                setEnabled(false);
                super.paint(g);
            }
        });

To avoid the setters on every paint(Graphics) call, you could also use an anonymous constructor block:

comboFromDuration.setRenderer(new DefaultListCellRenderer() {
    {
        setHorizontalAlignment(DefaultListCellRenderer.CENTER);
        setBackground(Color.WHITE);
        setForeground(Color.GRAY);  
        setEnabled(false);
    }
});
Acton answered 10/5, 2015 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.