0

I am quite new to Java GUI, and I have a question about JScrollPane:

Why does my scroll_pane not show up?

package good_Package;

public class Class_Operation
{
    public static void main(String[] args)
    {
        Class_Frame frame = new Class_Frame();
        frame.setVisible(true);
    }//END MAIN
}//END CLASS

And the frame class is below.

package good_Package;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Class_Frame extends JFrame
{
    JTextArea text_area;
    JScrollPane scroll_pane;
    
    public Class_Frame()
    {
        //Basics
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("This is a title.");
        getContentPane().setPreferredSize(new Dimension(500, 500));
        pack();
        setResizable(false);
        setLayout(null);//This is a null Layout
        getContentPane().setBackground(Color.GREEN);//frame has a different color from the scroll_pane
        //END Basics
        
        text_area = new JTextArea(20, 20);
        scroll_pane = new JScrollPane(text_area);
        scroll_pane.setBackground(Color.CYAN);//scroll_pane has a different color from the frame
        scroll_pane.setVisible(true);
        getContentPane().add(scroll_pane);
        
    }//END CONSTRUCTOR
}//END CLASS

Thank you for all suitable answers come from the future.

Frakcool
  • 10,540
  • 9
  • 46
  • 78
  • (1) Yes. (2) No, you shouldn't. What you should do is either create the scrollpane and passing the component it should display in the constructor like `JScrollPane pane = new JScrollPane(componentToDisplay);`, or set the component like `pane.setViewportView(componentToDisplay);` after you already created the scrollpane. You should not have to change the layout of your scrollpane itself. (3) It doesn't matter in which type of layout you place your scrollpane. Maybe check out [How to use Scroll Panes](https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) for more info. – maloomeister Jul 17 '20 at 11:10
  • 2
    `setLayout(null);` ← Remove that line. Layouts are important and necessary. They do all the work of making sure child components appear as they should. – VGR Jul 17 '20 at 14:25
  • 1
    And get rid of the setPreferredSize() statement. Swing components will determine their own preferred size. i.e. the (20, 20) parameter to the text area will allow it to determine its size. Also, you add all the components to the frame first BEFORE invoking packe() and setVisible(...). – camickr Jul 17 '20 at 14:34
  • I removed setLayout(null), then scroll pane and its component text area show up now, but what I want is not just show up, what I want is both of them show up at a certain location with a limited size, I could use setSize() and setLocation and setBounds for panel and its component button, but I cannot apply the same things/methods into scroll pane and its component text area effectively. – Deer Lawson Jul 17 '20 at 15:33
  • Stop thinking of using `setBounds()` method, it's [harmful](https://stackoverflow.com/questions/42520492/jtable-not-showing-up-on-jframe-java/42521097#42521097) for your design. You need to learn how to use the various [layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). You can combine them (one per `JPanel`). – Frakcool Jul 17 '20 at 17:01
  • So... instead of a null layout, what layout do you suggest me to use for a scroll pane and its component like text area? I would like a custom layout that I can insert/add my scroll pane at a certain location with given size into my frame. I thought a null layout means a custom layout since I used that for panels and buttons and labels, and I could easily use setSize setLocation setBounds for them to be at a certain location with given size. – Deer Lawson Jul 18 '20 at 10:04

0 Answers0