0

For some reason, when I try to use the following code,

Java Code:

idiomText.setPreferredSize(new Dimension((int)width, 24));

I can't change the width of the text box, but I can change the height. Can anyone tell me what I'm doing wrong??

If you need more details please let me know

I am using the drag and drop swing gui designer

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gary
  • 67
  • 1
  • 8
  • 2
    See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) The size of a text field is best specified by the number of columns and font size. *"I am using the drag and drop swing gui designer"* My condolences. We typically only recommend them for people who already understand layouts and component sizing. – Andrew Thompson May 04 '16 at 01:45
  • I am definitely a newbie, and although I've gotten a pretty good handle on the GUI builder, there are MANY things that I don't know. I appreciate your response. Is there any alternative you would recommend? – Gary May 04 '16 at 01:52
  • What layout manager are you using? –  May 04 '16 at 01:53
  • I believe group layout. I am using the netbeans IDE gui builder – Gary May 04 '16 at 01:55
  • `JTextField#setColumns` – MadProgrammer May 04 '16 at 02:08
  • not sure why the moderators here can destroy a thought process that was going well. If I needed to most that chat I would've. no need to delete it – Gary May 04 '16 at 02:21
  • @Gary I can re add it if you want. But I think we already established null layout isnt the way to go haha – DarkV1 May 04 '16 at 02:38
  • My apologies. Im just VERY frustrated. This seems like something that would have a simple solution, yet I've been working on it all day and just can't do it – Gary May 04 '16 at 02:52
  • i can make y change like it's nothing. x will not change no matter what I put in it – Gary May 04 '16 at 03:12

1 Answers1

0

Here is a link that helps: (It talks about problems with setting a components size: It helped me quite a bit a while back)

https://docs.oracle.com/javase/tutorial/uiswing/layout/problems.html

EDIT:

Another way you can do it is the

setSize(int width, int height); method // although it doesn't give you much leeway.

But the layout must be set to null in order for this method to work.

setLayout(null);
DarkV1
  • 1,776
  • 12
  • 17
  • Thank you. I actually read that earlier and had trouble making sense of it. Since I am using a GUI builder(the netbeans IDE), can I edit the size of components if I want to? This article doesn't quite answer that question – Gary May 04 '16 at 01:42
  • Thank you. I've seen this answer as well. May I ask how I can set the layout null? – Gary May 04 '16 at 01:51
  • This is for a homework assignment by the way. I am assigned to, upon the press of a button, empty a textfield and resize it's width randomly – Gary May 04 '16 at 01:57
  • double width=(Math.random()+30)*12; This is what width equals – Gary May 04 '16 at 01:59
  • Yeah. Paste that into the setSize Parameters – DarkV1 May 04 '16 at 01:59
  • setLayout(null); idiomText.setSize((int)(Math.random()+30)*12, 30); – Gary May 04 '16 at 02:04
  • setLayout(null); idiomText.setSize((int)(Math.random()+30)*12, 30); – Gary May 04 '16 at 02:06
  • that's all I've added. If you need anything specific please ask. Thanks for all your help so far by the way – Gary May 04 '16 at 02:06
  • `null` layouts are never the answer. Since `revalidate` calls `invalidate` and `validate` and are intended to be used as part of the layout management API, it's pointless in calling it if you're going to use `null` layouts – MadProgrammer May 04 '16 at 02:07
  • @MadProgrammer. Yeah they arent. PreferredSize and pack() is the way to go....but the first attempt was a fail and this method is usually " recommended to people who know how to use layouts and such" -Andrew – DarkV1 May 04 '16 at 02:09
  • Based on testing it yeah null layouts don't work. Do you have any alternatives? – Gary May 04 '16 at 02:10
  • @Gary i would suggest PrefferedSize() and pack() and revalidate() – DarkV1 May 04 '16 at 02:10
  • `JTextField` actually provides a method by which you can modify the "preferred" size in a platform independent manner, but because `GroupLayout` does some "interesting" things, it may not work, best to either modify the properties of the layout or using a different layout manager – MadProgrammer May 04 '16 at 02:11
  • @Gary I'd recommend a different layout manager and maybe have a look at `JTextField#setColumns` – MadProgrammer May 04 '16 at 02:11