1

I am adding a JTextArea to my JPanel and setting it to a specific place but for some reason it does not move at all. I have used both setBounds() and setLocation(), but to no avail. Here is what I stopped on:

JTextArea name_field=new JTextArea(1,10);
name_field.setBackground(color);
name_field.setBounds(100,100,600,420);
name_field.setLineWrap(true);
add(name_field);

It keeps creating the text field at the same spot: at the top of the screen in the middle. The only thing I managed to do is change it's width by adding name_field.setLineWrap(true) which only confused me even further. If this for some reason isn't supposed to work, is there another way of moving and possible resizing my JTextArea?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
seMikel
  • 246
  • 2
  • 14
  • 1
    @Fast Snail I forgot about that part, but it's not just a JPanel it's a class that extends JPanel and since I haven't set the layout it's whatever the dafault is. – seMikel Jul 09 '15 at 13:10
  • 3
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Jul 09 '15 at 13:17

1 Answers1

1

default layout of a jpanel is flowlayout .in order to work setBounds() it should be null layout.but it's highly discourage to use null layout[no layout].you should use layouts there are lot of layouts flow,grid,box,..etc.you first decide appropriate layout for your panel and then use it.

if you set layout to null then your code should work .[but dooont!]

setLayout(null); //change jpanel layout to null
JTextArea name_field=new JTextArea(1,10);
name_field.setBackground(color);
name_field.setBounds(100,100,600,420);
name_field.setLineWrap(true);
add(name_field);
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • Thank you, that helped. Can I ask why is it not recommended to set the layout to null? – seMikel Jul 09 '15 at 13:12
  • @seMikel read this one http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing and http://www.leepoint.net/GUI/layouts/nulllayout.html – Madhawa Priyashantha Jul 09 '15 at 13:13
  • 3
    *"it's highly recommended to use layout .don't use null"* - Then without more context to the question, I'd avoid suggesting it at all ;) – MadProgrammer Jul 09 '15 at 13:18
  • and with JScrollPane please – mKorbel Jul 09 '15 at 13:29
  • @mKorbel sorry .i don't get it – Madhawa Priyashantha Jul 09 '15 at 13:30
  • *"i don't get it"* Answer like this, even when prefaced by *"it's highly recommended to use layout .don't use null."* tend to have newbies leaping on 'but it worked!' then 3 more questions about '..but I'd like to fix.. (all the things that null layouts break)'. Anyone that so much as mentions `null` layouts has earned a -1 from me. By the time newbies hear about that possibility, they should ideally already be familiar with laying out a GUI.. – Andrew Thompson Jul 09 '15 at 21:57