0

I have a textarea which will contain a lot of input lines, and i need a scroll. i don't know why isnt it showing, there are no errors.

  JFrame main_GUI = new JFrame(); //main frame
  JPanel body = new JPanel(null);
  main_GUI.setLayout(null);  
  main_GUI.setSize(600, 600); //size
  
  body.setBounds(150, 50, 400, 500);
  main_GUI.add(body);


  JTextArea contacts_field = new JTextArea(); //the textarea that will contain a lot of lines
  JScrollPane scroll = new JScrollPane(contacts_field);

  body.add(contacts_field);
  body.add(scroll);

  main_GUI.setResizable(false);
  main_GUI.setLocationRelativeTo(null);
  main_GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  main_GUI.setVisible(true);

Pulse
  • 57
  • 1
  • 7
  • 3
    in short: use `LayoutManager`s - using `null` LayoutManager is mostly always a problem - in this case, the JScrollPane is missing a size, so it is 0x0, so nothing is displayed (still other problems are expected) – user16320675 Jan 08 '22 at 15:52
  • 2
    Yes, you can set the JScrollPane's seize and location and then add it to the null-layout-using container, but as @user16320675 states, it's almost *always* wrong to use a null layout. While it might seem easier to create GUI's as a newbie using these, the GUI created will probably work on only one person's screen, and trying to maintain them is a nightmare. Much better to learn and use the layout managers. Also, you will want to set the column and row property for the JTextArea, usually by passing 2 ints into its constructor. This will set the scrollpane's preferred size. – Hovercraft Full Of Eels Jan 08 '22 at 15:56

0 Answers0