How to align a JTextField that it is always on the far right side of the window?

41 Views Asked by At

I have a normal content pane in Java (IDE: Eclipse) and a JTextField.

Now I want the text field to always be on the far right, even if the size of the window changes. Is this somehow possible?

1

There are 1 best solutions below

0
camickr On

One way to is nest panels with different layout manager.

The default layout manager of the content pane of the frame is a BorderLayout (unless Eclipse changes it, in which case you should set it back to a BorderLayout.

Then you can:

  1. create a JPanel with a FlowLayout that is right aligned.
  2. add the text field to this panel.
  3. add this panel to the BorderLayout.PAGE_START of the content pane.
  4. add you main panel containing other components to the BorderLayout.CENTER.