1

I'd like to add to window starting from x, y position JTextArea with scrolls. Problem is that when I do visualise code, it does fill 500x500 window and I do want fill only 200x200 of full at x, y pos.

What am I doing wrong? Here's my code:

public static void main(String args[])
{
    JFrame window = new JFrame();
    window.setSize(500, 500);
    window.setResizable(false);

    JTextArea ta = new JTextArea("Default message");
    ta.setEditable(true);

    JScrollPane scroll = new JScrollPane(ta);
    scroll.setBounds(10,10,200,200);
    window.add(scroll);

    window.setVisible(true);
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1768615
  • 281
  • 1
  • 5
  • 12
  • @AndrewThompson, for a robust GUI, the most reasonable move is using layout managers? Arent there any other options? – user1768615 Jan 07 '14 at 23:53

2 Answers2

4

Don't use setBounds() to set the size of a component. Swing uses layout managers to size a component.

By default a JFrame uses a BorderLayout and you are adding the component to the CENTER so the component takes up all the space available.

Instead do something like:

JTextArea text Area = new JTextArea(5, 40);
JScrollPane scrollPane = new JScrollPane( textArea );
frame.add(scrollPane, BorderLayout.NORTH);
frame.pack();
frame.setVisible();

When you display the frame the text area will take up the entire space, but if you resize the frame larger then the text area will only be displayed in the NORTH and there will be empty space in the CENTER.

camickr
  • 321,443
  • 19
  • 166
  • 288
3

Padded Text Area

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

public class PaddedTextArea {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                // adjust as needed
                gui.setBorder(new EmptyBorder(20,10,20,10));

                // better way to size a text area is using columns/rows 
                // in the constructor
                JTextArea ta = new JTextArea(3,40);
                JScrollPane sp = new JScrollPane(ta);
                gui.add(sp);

                JFrame f = new JFrame("Padded Text Area");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}

More generally: Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433