File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to change jtextfield size dynamically? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to change jtextfield size dynamically?" Watch "How to change jtextfield size dynamically?" New topic
Author

How to change jtextfield size dynamically?

Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21

Hello,

I am trying to change jtextfield size according to total number of characters inserted in it..I have tried setColums(), setMaximumsize(), setPreferredSize(), but didnt work. I want to change it at runtime..

Can anyone help me or suggest me?

Thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a lot depends on the layout manager used.

post a small runnable example so we can see what you're doing

i.e. just a frame with a textfield and button, button to change the size of the textfield
Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21
private void txtSelFolderMouseClicked(java.awt.event.MouseEvent evt) {

JFileChooser fc=new JFileChooser();
fc.setFileSelectionMode(fc.DIRECTORIES_ONLY);
fc.showOpenDialog(frmMain);
//Set path string in textfield.
txtSelFolder.setText(fc.getSelectedFile().toString());
//Change textfield size according to path string.
// txtSelFolder.setSize(txtSelFolder.getSize());

}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
and what line of the posted code shows which layout manager you are using?

I don't like playing '20 guesses' games, so I'll leave this thread for those that do.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Moving to our GUI forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21

I am using NetBeans...and NetBeans uses GroupLayout as a default layout manager for GUI swing components..

Can anyone please help me in this?

Thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
Ishani Mehta wrote:
Can anyone please help me in this?
Thanks


You where asked to post a SSCCE.
Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21
What I want is when I browse any file using FileChooser and set that particular path in textfield, I want to increase the size of that textfield according to the total number of characters of that path. Below is the code, its actually short but due to designing of that frame form, generated code has also been pasted here..

--------------------------

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
We don't care about your real application.

Did you not read Michael's comment about creating a form with a text field and a button to do a simple test?

Also the code is not formatted, so I'm not going to take the time to read it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Ishani Mehta wrote:
What I want is when I browse any file using FileChooser and set that particular path in textfield, I want to increase the size of that textfield according to the total number of characters of that path.


I would suggest that is not a goal worth pursuing. For one thing it's quite possible that this would require a text field which is wider than the screen.

Besides it would be a lot simpler to just provide a text field which takes up the whole width of the form.
Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21

@Rob Camick: Didnt you get any idea about my query after reading my questions? Instead of telling me to do this and that, I was asking for suggestion regarding my query..and I didnt tell you to read whole code..You didnt get what I was asking, so posted the whole thing, so that you can run it at your end(without thinking what is actually in it) and I thought you might get that way whats the actual problem is..Anyways thanks..

@Paul Clapham:Really thankful to you..Thanks to help beginners..

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Ishani Mehta wrote:
@Rob Camick: Didnt you get any idea about my query after reading my questions? Instead of telling me to do this and that, I was asking for suggestion regarding my query..and I didnt tell you to read whole code..You didnt get what I was asking, so posted the whole thing, so that you can run it at your end(without thinking what is actually in it) and I thought you might get that way whats the actual problem is..Anyways thanks..

@Paul Clapham:Really thankful to you..Thanks to help beginners..



Actually Rob Camick's suggestion was more helpful than mine. If you want to get something working, just write a small bit of code which does that one thing and nothing else. The code you posted didn't do that, it had a lot of things in it which weren't related to the question at all. That just distracts people from the problem and, worse still, the irrelevant code might even be the source of the problem. Stripping your code down to the simplest possible test program is helpful for you too. Do try to apply that idea the next time you're trying something new.
Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21
Actually I was posting the sample code of related problem only but before submitting the newly changed post, my net got disconnected..That was the only problem with that big post..I have posted before, small code on click event of textfield which was working,but I didnt know how to change its size dynamically so was asking for that remaining thing..And I thought atleast few can suggest without that sample code..

Anyways keep in mind next time..

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2172
    
    7
Anyways keep in mind next time..


The standard way of posting a question is to include you SSCCE so we can see exactly what you are doing so we don't have to make any guesses. Have you read the link provided so you understand why a SSCCE should be posted?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Ishani Mehta wrote:Anyways keep in mind next time..


If you mean you will keep the recommendations from this thread in mind next time you have a problem, then that's a good thing.
Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21
For those who come across this thread one day for the same problem:

txtSelFolder.setSize(new Dimension(7*fc.getSelectedFile().toString().length(),txtSelFolder.getHeight()));

//txtSelFolder = jtextfield
//fc=FileChooser

Its working in my case.(Change the size of textfield according to the selected file's full path.)

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

Ishani Mehta wrote:For those who come across this thread one day for the same problem:

... please don't use the shoddy 'solution' posted by Ishani. Start your own thread, with a SSCCE, and you're sure to get good guidance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Agreed. It has several flaws. I can name two without even thinking:
1) setSize won't work unless you have a null LayoutManager which is a bad idea in 99.99999999% of the cases.
2) the 7 is probably a number found through trial and error; use a different font and it won't work anymore. If you ever need the size of a String then FontMetrics should be used.
Ishani Mehta
Greenhorn

Joined: Oct 16, 2006
Posts: 21
Yes did it through trial and error..

Thanks for such useful suggestions..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change jtextfield size dynamically?