Adding JTextArea to JScrollPane and JPanel issue's

So when I don’t add the text area to the scrollpane the update method works, however if I do all I get is a blank screen. The other problem is if I add any of this to a JPanl mainArea one below it becomes super doper tiny.

Any Help is appreciated thank-you.


public class Gui {
    JFrame frame;
    JPanel mainArea;
    JPanel choiceArea;
    JScrollPane scrollBar;
    JPanel charArea;//for later use
    JPanel mapArea;
    JTextArea textArea;
    
    
    public Gui(){
        
        frame = new JFrame("Escape");
        frame.setSize(700, 700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        mainArea = new JPanel();
        mainArea.setMaximumSize(new Dimension(680, 400));
        mainArea.setSize(680, 400);
        //mainArea.setPreferredSize(new Dimension(680, 400));
        
        choiceArea = new JPanel();
        choiceArea.setSize(700, 150);
        
        textArea = new JTextArea(35, 25);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        //textArea.setSize(680, 400);
        //textArea.setRows(35);
        
        scrollBar = new JScrollPane();
        scrollBar.setVerticalScrollBarPolicy(scrollBar.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollBar.setHorizontalScrollBarPolicy(scrollBar.HORIZONTAL_SCROLLBAR_NEVER);
        scrollBar.add(textArea);
        
        choiceArea.setLayout(new FlowLayout());
        //mainArea.add(scrollBar);
        
        
        
        
        frame.getContentPane().add(BorderLayout.CENTER, textArea);
        frame.getContentPane().add(BorderLayout.SOUTH, choiceArea);
        
        
        frame.validate();
        frame.setVisible(true);
        
    }
    
    public void addTextToTextArea(String s){
        textArea.append("\n" + s);
        textArea.repaint();
        textArea.revalidate();
        frame.repaint();
        frame.revalidate();
    }

To debug this sort of thing, I set all the components with a different background color. That makes it pretty unambiguous what is where. The screen won’t be blank!

At the moment your code only displays two components. I noticed if I change the number of rows, I don’t get a change in the vertical size of textArea when it is in the CENTER. However, changes to the row size does affect the relative dims when you swap SOUTH and CENTER in the current code. I’m not sure what that means though. Maybe there are rules as to “who wins” when two areas are contending for a space, rather than the frame expanding to hold both.

I’m also noticing that when I use setPreferredSize(new Dimension(750, 150)) on choiceArea, it can go in either CENTER or SOUTH and isn’t totally stomped on by textArea. I think setPreferredSize should be used rather than setSize.

Another tool to help understand what is going on: alternately use frame.pack() instead of frame.validate() at the end of the constructor, for debugging purposes. It will reduce the frame down to the minimum size based on the dimensions of the components, so that makes it easier to tell what is going on with the components before they get whacked by the BorderLayout logic.

It might take a bit to work out all the dimensions and rules as to “who wins”, so I’d recommend going piece by piece, testing with each added component.

For ScrollPane, I usually put the component to be held by the ScrollPane in the constructor as a parameter. I don’t know that it makes any difference though, compared to adding it later.

Hope some of this helps!

Thank-you for your reply I’ll look into your suggestions. Buttons are created in a different class which then calls a method to add them to the choice area.

The thing about packing is it would be changing multiple times due to the adding and subtracting of components.

again thanks I’ll look into those suggestions. Like I said if I don’t add the text area to the panel or the scrollpane everything works just fine -_-