0

When i add an scroll to my textarea then the textarea isnt visible in the application:

Code:

    JPanel panel = new JPanel();
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    panel.setLayout(null);

    final JTextArea textArea = new JTextArea(); 
    textArea.setBounds(15, 112, 689, 310);
    JScrollPane scrollPane = new JScrollPane( textArea ); 
    panel.add( scrollPane );
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Danny D
  • 33
  • 2
  • 8
  • I tried, but i dont understand why. Don't i have to give the textarea coordinates where it should be displayed? It didn't work though. – Danny D Oct 03 '15 at 12:47
  • you shouldn't use **null layout** .but `panel.add(scrollPane);` is the problem.since panel has null layout you should set bounds to the scrollPane but not to textarea – Madhawa Priyashantha Oct 03 '15 at 12:51
  • Alright, ive set bounds to the Scrollpane and it worked, thanks. But i don't get why you shouldnt set null layout since it just messes up my design if i delete it. – Danny D Oct 03 '15 at 13:06
  • `panel.setLayout(null);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 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 same applies to setting bounds.) – Andrew Thompson Oct 03 '15 at 13:08
  • 1
    *"..it just messes up my design.."* What you get with `null` layout is not a 'design' but a fragile mess of code that will break on the next computer. Learn how to use layouts if coding Swing or AWT GUIs, there is really no way around it. – Andrew Thompson Oct 03 '15 at 13:09

1 Answers1

3

Here is how to do what the code seems to be trying to do, using sizing hints for the text area, layouts and padding. Adjust numbers to need.

See further comments in code:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ScrollingTextArea {

    private JComponent panel = null;

    ScrollingTextArea() {
        initUI();
    }

    public void initUI() {
        if (panel != null) {
            return;
        }

        panel = new JPanel(new BorderLayout());
        // adjust numbers to need
        panel.setBorder(new EmptyBorder(32, 15, 32, 15));
        // adjust rows & cols to need
        final JTextArea textArea = new JTextArea(20,80); 
        JScrollPane scrollPane = new JScrollPane(textArea);
        panel.add(scrollPane);
    }

    public JComponent getUI() {
        return panel;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ScrollingTextArea o = new ScrollingTextArea();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433