Tuesday, December 7, 2010

JComboBox Text Alignment

Today someone asked me how can we make the text in a JComboBox align to the right instead of the left? Here's your answer :)

DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
 //DefaultListCellRenderer.LEFT;
 //DefaultListCellRenderer.CENTER;
 //DefaultListCellRenderer.RIGHT;
 dlcr.setHorizontalAlignment(DefaultListCellRenderer.RIGHT);
 jComboBox1.setRenderer(dlcr);

Import this,
import javax.swing.DefaultListCellRenderer;
and your done.


5 comments:

  1. I was looking for something like this. thks

    ReplyDelete
  2. Thank you for this simple and short code. It works perfectly and is much shorter and easier to use than a new render for the JComboBox.

    ReplyDelete
  3. That's what I needed. And very simple. Thank you!

    ReplyDelete
  4. I was looking for this...thanks a lot

    ReplyDelete
  5. Simple and easy. tnx

    ReplyDelete