This will work:
public class ScrollPaneTest extends JFrame { private Container myCP; private JTextArea resultsTA; private JScrollPane scrollPane; public ScrollPaneTest() { setSize(500, 500); setLocation(100, 100); myCP = this.getContentPane(); myCP.setLayout(null); resultsTA = new JTextArea("Blah blah"); resultsTA.setBounds(10, 10, 150, 30); scrollPane = new JScrollPane(resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(200, 100)); scrollPane.setBounds(0, 0, 500, 500); myCP.add(scrollPane); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { new ScrollPaneTest(); } }
If you use a null layout, you must specify the boundaries.
Edit
setBounds() method covers the task of the setLocation() method.
for example, setBounds(x,y,w,h);
first 2 will set the x / y location of this component relative to its container. the second 2 (w / h) sets the size of this component.
In other words: -
- setBounds (int x, int y, int witdh, int height) - Sets the size and position of components
- setLocation (int x, int y) - sets the location of components
source share