> I need help with align to right: JTextArea
Don't think you can with a JTextArea (without writing your own View). But you can with a JTextPane. This example uses center alignment but can easily be changed to right alignment:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=342068
> and JComboBox
The renderer is a JLabel so you just need to create your own renderer and set the alignment of the label text to be right aligned.
this might start you off
import javax.swing.*;
import java.awt.*;
class Testing extends JFrame
{
public Testing()
{
setLocation(300,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JTextArea ta = new JTextArea();
ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JScrollPane sp = new JScrollPane(ta);
sp.setPreferredSize(new Dimension(200,150));
JComboBox cbo = new JComboBox(new String[]{"abc","123"});
cbo.setEditable(true);
cbo.setRenderer(new RightAlignRenderer());
//cbo.getEditor().getEditorComponent().setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
((JTextField)cbo.getEditor().getEditorComponent()).setHorizontalAlignment(JTextField.RIGHT);
//above two lines for editable combo box, seems to be the same either way
getContentPane().add(sp,BorderLayout.CENTER);
getContentPane().add(cbo,BorderLayout.SOUTH);
pack();
}
public static void main(String[] args){new Testing().setVisible(true);}
}
class RightAlignRenderer extends DefaultListCellRenderer
{
public Component getListCellRendererComponent(JList list,Object value,
int index,boolean isSelected,boolean cellHasFocus)
{
JLabel lbl = (JLabel)super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus);
lbl.setHorizontalAlignment(JLabel.RIGHT);
return lbl;
}
}
Interesting. I did my test like this and the text originally displayed on the left. When I typed the first character it then shifted to the right.
JTextArea textArea = new JTextArea(5, 40);
textArea.setText("Some\n text");
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
But it works fine like this:
JTextArea textArea = new JTextArea(5, 40);
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
textArea.setText("Some\n text");