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
posted
0
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
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: 1786
2
posted
0
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.
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: 17
posted
0
@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..
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: 17
posted
0
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: 1786
2
posted
0
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?
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.