?
Solved

Sizing an edit box

Posted on 1999-10-26
11
Medium Priority
?
185 Views
Last Modified: 2012-05-04
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?…
0
Comment
Question by:moirafish
11 Comments
 
LVL 1

Expert Comment

by:sameerjoshi
ID: 2160294
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.
0
 
LVL 1

Author Comment

by:moirafish
ID: 2160733
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
0
 

Expert Comment

by:chochomhaloimed
ID: 2161120
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 1

Author Comment

by:moirafish
ID: 2172450
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
0
 
LVL 1

Author Comment

by:moirafish
ID: 2184395
sorry, my previous comment was sadly meant to be a rejection
0
 
LVL 2

Expert Comment

by:shaveri
ID: 2185210
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

0
 
LVL 1

Author Comment

by:moirafish
ID: 2187203
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
0
 
LVL 1

Expert Comment

by:vivexp
ID: 2196297
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
0
 
LVL 1

Author Comment

by:moirafish
ID: 2198915
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
0
 
LVL 2

Accepted Solution

by:
shaveri earned 400 total points
ID: 2199087
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){
      
      super(initialText);
     
      JFrame frame = new JFrame("Text Limitation");
      frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e)
                   {System.exit(0);}
            });
      frame.getContentPane().add(this);
      frame.setSize(400, 125);
      frame.getContentPane().setLayout(new FlowLayout());
      frame.setVisible(true);
}
public static void main(String arg[]){
            new textlimit1("welcome to india");
}


}
0
 
LVL 1

Author Comment

by:moirafish
ID: 2204074
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
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month6 days, 22 hours left to enroll

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question