Link to home
Start Free TrialLog in
Avatar of moirafish
moirafish

asked on

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

ie
X[moirafish]X

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?…
Avatar of sameerjoshi
sameerjoshi

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

textBox.setSize(len,someHeight);

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.
Avatar of moirafish

ASKER

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
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...

Moira
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 foll.site...

http://forum.java.sun.com/forum?13@36.AyV2agIGefk^0@.ee779cd/0

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

cheers,
Moira
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
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)

cheers,
Moira
ASKER CERTIFIED SOLUTION
Avatar of shaveri
shaveri

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You're absolutely right, it IS as easy as that!

I added a
d = getPreferredSize();
after the
frame.setVisible(true);
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!!

cheers,
Moira