0

Scrollpane with Textarea doesn't show up. (I have gif running in the background too). I've tried to add it to another frame alone it worked but somehow i can't add it inside the container

static class UserGUI extends JFrame implements ActionListener { 
    PlaceHolder p1, p2 ,p3 ,p4, p5, p6;
    // Components of the Form 
    private Container c; 
    private JLabel title; 
    private JTextArea textarea1;

public UserGUI () throws IOException { 

        setTitle("User Panel"); 
        setBounds(300, 90, 1000, 1000); 
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        
        c = getContentPane(); 
        c.setLayout(null); 

        textarea1 = new JTextArea(); 
        textarea1.setFont(new Font("Arial", Font.PLAIN, 15)); 
        textarea1.setSize(300, 400); 
        textarea1.setLocation(500, 100); 
        textarea1.setLineWrap(true); 
        textarea1.setEditable(false); 
        textarea1.setVisible(true);
           

        JScrollPane scroll = new JScrollPane (textarea1);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        add(scroll);   
        c.add(textarea1); 

        }
}
hexstorm
  • 318
  • 4
  • 14
Vlad
  • 19
  • 2
  • 3
    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). **One undesirable aspect (of many) of using `null` layouts is that scroll panes will cease to function as intended.** – Andrew Thompson Mar 15 '21 at 15:55
  • A Swing component can only have a single parent. You add the text area to the scroll pane, but you remove it when you add it to the content pane. Don't add the text area to the content pane. Read the section from the Swing tutorial on [How to Use Text Fields](https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html). The `TextDemo` is a working example showing how to use layout managers with both a JTextField and JTextArea/JScrollPane. – camickr Mar 15 '21 at 16:37
  • i tried to follow your steps and saw that demo but still aint working some how – Vlad Mar 16 '21 at 03:59
  • 2
    Tip: Add @camickr (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Mar 16 '21 at 06:58
  • 4
    @Vlad *but still aint working some how* - you mean the demo code isn't working? The point of the comment is to download the "working demo" code and then modify it for your requirement. All you need to do is remve the text field and you have a working class!!! The working code will even show you how to better structure your class so you don't: 1) extend JFrame and 2) create the components on the Event Dispatch Trhead (EDT). Start by learning Swing basics with Swing examples from the tutorial. – camickr Mar 16 '21 at 14:48

1 Answers1

2

When you extend from JFrame and call add(), the component is added to the JFrame's ContentPane, so your code makes no sense.

Container c = getContentPane();
c.add(); //Same as add()

As @AndrewThompson suggested in the comments, you should always use layouts (as your GUI will have to work on different screen sizes, OS', etc.). So preferably never use setLayout(null).

A correct example for what you want to do would be:

public class UserGUI extends JFrame{

    private JTextArea textArea;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new UserGUI();
            }
        });

    }

    public UserGUI(){
        textArea = new JTextArea();
        textArea.setFont(new Font("Arial", Font.PLAIN, 15));
        textArea.setLineWrap(true);
        //textArea.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(textArea); //Add the textArea to the scrollPane
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        add(scrollPane); //Add the scrollPane to the JFrame's Content Pane

        setTitle("User Panel");
        setSize(500, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }

}
hexstorm
  • 318
  • 4
  • 14
  • that actually works but it adds the scroll pane to the whole app and my textarea gets enlarged to fit the whole app somehow .. im using it inside action listener – Vlad Mar 16 '21 at 13:55
  • 1
    @Vlad ActionListener is not a component or a panel at all. You can't put things inside it, it's something totally different. Check the Java [official tutorials](https://docs.oracle.com/javase/tutorial/uiswing/index.html) for Swing, they're really good. – hexstorm Mar 16 '21 at 14:55