Currently I have this JComboBox
, how can I get to center the content inside it?
String[] strs = new String[]{"15158133110", "15158133124", "15158133458"};
JComboBox com = new JComboBox(strs);
By default, items in the JCombobox are left-aligned, we can also change to center alignment by using the setHorizontalAlignment(DefaultListCellRenderer.
A JComboBox is a component that displays a drop-down list and gives users options that we can select one and only one item at a time whereas a JList shows multiple items (rows) to the user and also gives an option to let the user select multiple items.
u can make ur jcombobox uneditable by calling its setEnabled(false). A JComboBox is unEditable by default. You can make it editable with the setEditable(boolean) command. If you are talking about enabled or disabled you can set that by the setEnabled(boolean) method.
You need to create a Renderer then apply it to the JComboBox
DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
dlcr.setHorizontalAlignment(DefaultListCellRenderer.CENTER);
com.setRenderer(dlcr);
also import this,
import javax.swing.DefaultListCellRenderer;
((JLabel)jComboBox1.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);
Try This
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With