Is there a way to resize a control, a JTextfield for example, at runtime in java? I want my textfield to have the resize cursors (just like when you point your cursor on the corner of a window) and will be able to resize on runtime. Ive read on the internet that vb6 and C# have those capabilities, is there anything for java? A sample code or a link to a good tutorial will be very much appreciated. Thank you.
3 Answers
It sounds like you are trying to implement a component editor, such as the GUI editors available in popular programing IDEs. The essential feature is a handle, a graphical object that can be selected and dragged to change the geometry. GraphPanel
is a simple example of an object drawing program that illustrates many of the required techniques.

- 203,806
- 29
- 246
- 1,045
That depends on the Layout
of the JTextField
's container. A good tutorial is available at http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

- 8,484
- 1
- 41
- 75
-
thank you for the quick reply. actually im talking about the controls being resized by the user and not by the program itself. It's more like the GUI builder of netbeans, but in my case i want to do it on my program runtime. – John Jul 08 '12 at 21:17
-
@user1349213: Larry's talking about the same thing. If you use a correct combination of layouts, your JTextField's size will vary with the size of the GUI. Please read the tutorials he's linked to to learn more. 1+ – Hovercraft Full Of Eels Jul 08 '12 at 21:18
-
thank you again! Im pretty much familiar with some of those layouts. I want it to be resized by mouse pressed and mouse released listeners. Can you please tell me what specific layout do I need to use? Or is it really a combination of layouts? Sorry, I just dont know what to do. – John Jul 08 '12 at 21:26
-
@user1349213: Have you read the link that Larry provided? If so, then you're a fast reader! Focus on the BorderLayout for one, but yes, often you need to nest JPanels, each one using its own layout. – Hovercraft Full Of Eels Jul 08 '12 at 21:29
-
can you please give me a very simple code for that? thank you very much! – John Jul 09 '12 at 04:08
For a quick and cheap solution you could use a JSplitPane
component, with the JTextField
to be resized in the left side, and an empty JPanel
in the right side. By default a JSplitPane
is decorated with a border and a divider, but you can remove these by setting an empty border.

- 1,407
- 1
- 11
- 18
-
this will definitely work, but the problem is that I need the controls to be movable on other location not just resizable. Im just doing it step by step. thanks for this answer btw. – John Jul 09 '12 at 04:07