Sizing an edit box

I want to size an edit box to exactly fit the text that it contains
with 2 buttons to the left & right of it


where X is a button, [moirafish] the editbox

I suppose what I need to ask is:
- how do I find the width required for an edit box to exactly fit the text that it contains
- what layout I need to use to keep the buttons left & right

can anyone help?…
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

get the length of the text as

String len = textBox.getText().length();

Once you have the lengthof the text, resize the size of the textbox by calling


This should solve your problem.
Note. Maybe you need to put these 2 buttons and textfield in BorderLayout with the  locations of the one button as "EAST" and other as WEST.
moirafishAuthor Commented:
It's not quite the right answer...
if i do setSize, with my edit box as BorderLayout.CENTER (so that the buttons stay left & right), the edit box always gets stretched & doesn't stay the size i want it
you need to have two child panels inside WEST and EAST of parent panel, One of buttons will go to the center of EAST panel, other in the center of WEST panel. textbox will go on the East or west of the east or west panel, wichever closer to the center of parent panel. East and west never get streched if Center is occupied
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

moirafishAuthor Commented:
This is also not what Im looking for...

I think my question is a bit confusing...

I want to size my edit box so that it exactly fits the texts contained in it, ie all text visible. I dont want it to stretch, so i cant use BorderLayout.

I've found a workaround using a GridBagLayout & not letting the user size the dialog, but I'ld still like to know how to find the size required for a text field so that it exactly fits a certain amount of text...

moirafishAuthor Commented:
sorry, my previous comment was sadly meant to be a rejection
Are you asking for limiting text characters in the text field, if so visit the^0@.ee779cd/0

moirafishAuthor Commented:
No, thanks, I dont want to limit the text, I want to size my text box so that it exactly fits the text that i initialise it with

Why don't u try using flow Layout instead of border layout
& get the length of the string u want to display....
this might solve ur problem
moirafishAuthor Commented:
Thanks... but
What do you mean
"get the length of the string u want to display"
how do i do that?
if i could know the length that the string would take in the edit box, i could size the edit box accordingly & it wouldnt matter what layout i used (i think)

Try this out...I think this will serve your purpose.Try initialising the text with different text...thanks

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class textlimit1 extends JTextField {
public textlimit1(String initialText){
      JFrame frame = new JFrame("Text Limitation");
      frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
      frame.setSize(400, 125);
      frame.getContentPane().setLayout(new FlowLayout());
public static void main(String arg[]){
            new textlimit1("welcome to india");


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
moirafishAuthor Commented:
You're absolutely right, it IS as easy as that!

I added a
d = getPreferredSize();
after the
which then gave me the actual size of the edit box,

having that meant I could size the actual frame accordingly so that it fitted,

so thanks a lot!!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.