Why is my JScrollPane not showing with JTextArea when using null LayoutManager?

I try to show JTextArea in JScrollPane, but I just get an empty frame when I run my (simplified) program:

import java.awt.Container; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; 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"); scrollPane = new JScrollPane(resultsTA, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setPreferredSize(new Dimension(200, 100)); scrollPane.setLocation(100, 300); myCP.add(scrollPane); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { new ScrollPaneTest(); } } 

I use null LayoutManager to match the tutorial that I teach.

+4
source share
2 answers

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
+7
source

I have to agree with Cleopatra's comment on this.

Here is a variant of the Harry Joy code that uses layouts. It appears on the screen almost the same size as the original graphical user interface, but can be changed. It also easily adapts to various PLAFs, default font sizes, etc. (Although a different size may be displayed on the screen), while something with a null layout will not.

 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ScrollPaneTest extends JFrame { private Container myCP; private JTextArea resultsTA; private JScrollPane scrollPane; public ScrollPaneTest() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocation(100, 100); myCP = this.getContentPane(); resultsTA = new JTextArea("Blah blah", 28, 43); scrollPane = new JScrollPane(resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); myCP.add(scrollPane); setVisible(true); pack(); } public static void main(String[] args) { Runnable r = new Runnable() { public void run() { new ScrollPaneTest(); } }; SwingUtilities.invokeLater(r); } } 
+1
source

Source: https://habr.com/ru/post/1347039/


All Articles