• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change jtextfield size dynamically?

 
Ishani Mehta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to our GUI forum.
 
Ishani Mehta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishani Mehta wrote:
Can anyone please help me in this?
Thanks


You where asked to post a SSCCE.
 
Ishani Mehta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes did it through trial and error..

Thanks for such useful suggestions..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic