Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render JCombobox to the right problem

I want to let JComboBox be aligned from right to left and to render text from right to left I have set component orientation and also change the render as follows:

import java.awt.Component;
import java.awt.ComponentOrientation;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.SwingConstants;

public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        jComboBox1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);        
jComboBox1.setRenderer(new DefaultListCellRenderer() {
    public Component getListCellRendererComponent(JList jList, Object o,
    int i, boolean b, boolean b1) {
    JLabel rendrlbl = (JLabel) super.getListCellRendererComponent(jList, o, i, b, b1);
    rendrlbl.setHorizontalAlignment(SwingConstants.RIGHT);
    return rendrlbl;
    }
    });


    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.setEditable(true);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(55, 55, 55)
                .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 268, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(77, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(37, 37, 37)
                .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(236, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JComboBox jComboBox1;
    // End of variables declaration
}

enter image description here

As you may have seen in this screenshot the combobox and it's cursor is aligned to the left while the list is aligned to the right. How can I solve this?

like image 927
Feras Odeh Avatar asked Nov 13 '22 19:11

Feras Odeh


1 Answers

it's cursor is aligned to the left while the list is aligned to the right. How can I solve this?

Sounds like you have an editable combo box. Therefore I guess you also need to change the orientation of the editor component:

ComboBoxEditor editor = comboBox.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
textField.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);  
like image 187
camickr Avatar answered Jan 03 '23 14:01

camickr