JTextArea and JComboBox

Hi,I need help with align to right: JTextArea and JComboBoxthank :)Yael
[99 byte] By [yael800a] at [2007-10-3 8:31:52]
# 1

> 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.

camickra at 2007-7-15 3:39:04 > top of Java-index,Desktop,Core GUI APIs...
# 2

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;

}

}

Michael_Dunna at 2007-7-15 3:39:04 > top of Java-index,Desktop,Core GUI APIs...
# 3

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");

camickra at 2007-7-15 3:39:04 > top of Java-index,Desktop,Core GUI APIs...
# 4
Do you know maybe how can I check the local languge os?then I whant to do:if(local == "Hebrew")then alighn to right JText area..;)Yael
yael800a at 2007-7-15 3:39:04 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thank you very much!!!!!It is workYael :)
yael800a at 2007-7-15 3:39:04 > top of Java-index,Desktop,Core GUI APIs...