Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Right Alignment of JTextField

Status
Not open for further replies.

dcusick

Technical User
Aug 9, 2000
271
US
Hey everyone! I'm just starting with Swing and actually am moving along pretty well.. I've gone forward and created a page using GridBagLayout. I've also created my own text field class, that extends JTextField. The field comes up fine, and works like it's supposed to, however, I can't place it correctly in my GridBagLayout... I cannot seem to right-justify the text box. If I add it in with no fill, it will be flushed to the left hand side of the allocated space. If I add horizontal fill, the text box fills the entire allocated space. I cannot get the text box to have right-alignment that is flushed against the right hand side of the allocated space. This code is in the constructor of my created class....

Code:
    setHorizontalAlignment(JTextField.RIGHT);
    setAlignmentY(JTextField.RIGHT_ALIGNMENT);
    setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

Any suggestions would be greatly appreciated!! Thanks in advance...

Doug
 
c.anchor = c.GridBagConstraints.EAST ;// c is your GridBagConstraints
// c.fill = GridBagConstraints.BOTH; incorrect setting for anchor
// c.fill = GridBagConstraints.VERTICAL; correct setting
// c.fill = GridBagConstraints.HORIZONTAL; correct setting
setConstraints(yourcomponent, c);
add(yourComponent); // add your own jtextfield
c.anchor = GridBagConstraints.CENTER; //reset to default
Look at the page for anchor and values
 
first line should be
c.anchor = GridBagConstraints.EAST ;// c is your GridBagConstraints
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top