Bee
Bee

Reputation: 12502

How to have a dynamic width for a JTextField?

I have a JFrame with a JTextField. I want to get the width of the JTextField increased/decreased accordingly when the JFrame is maximized/restored.

How can I do that?

Upvotes: 3

Views: 3650

Answers (2)

Seyf
Seyf

Reputation: 889

Set a component listener to the JFrame. It provides some methods to run when the component is resized, moved, hidden or shown. Here is how to do it.

Upvotes: 0

Mark Peters
Mark Peters

Reputation: 81054

You need to use a LayoutManager for the parent component which instructs the JTextField to fill up available space.

The default layout manager for a JFrame will do this however. If you simply call frame.add(new JTextField()) and try to resize the window, the text field will fill the available space. You must be putting the JTextField into an intermediate panel which has a different layout manager. You would need to give more context for us to help you with your specific problem.

Upvotes: 7

Related Questions