Render JCombobox for the right task

I want the JComboBox to align from right to left and display the text from right to left. I set the component orientation and also changed the rendering 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 can see in this screenshot, the combobox and its cursor are aligned to the left, and the list is aligned to the right. How can i solve this?

+5
source share
1 answer

the cursor is aligned to the left, and the list is aligned to the right. How can i solve this?

It looks like you have an editable combo box. Therefore, I think 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);  
+1
source

All Articles