Search code examples
javaswingjframelayout-managerjtextarea

JTextArea Won't Update Unless I Resize JFrame


Whenever I run my program my JTextArea does not follow the dimension that I have given it, but if I resize my JFrame it updates and sets its size to what I put.

What is the issue?

    public ControlPanel() {
    // create our list of players
    list = new JList(model);

    // create our scroll panes
    userspane = new JScrollPane(list);
    consolepane = new JScrollPane(console);

    // set sizes
    userspane.setSize(100, 500);
    jta.setSize(100, 500);
    list.setSize(100, 500);
    consolepane.setSize(100, 500);
    console.setSize(100, 500);

    // add to panel
    panel.add(userspane, BorderLayout.CENTER);
    panel.add(kick);
    panel.add(ban);
    panel.add(info);
    panel.add(consolepane, BorderLayout.CENTER);

    // set frame properties
    setTitle("RuneShadows CP");
    setSize(280, 400);
    //setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setContentPane(panel);
    setVisible(true);
}

Solution

  • Don't set the sizes to anything.

    • For JTextArea you can use the constructor JTextArea(int rows, int charSpaces)
    • Just pack() the JFrame and it will respect all the preferred sizes of the components inside.
    • Also instead of setting the content pane to the panel, just add the panel. That will respect the preffered size of the panel when pack() is called

    I'm not exactly sure what variable was what (or the sizes you wanted the), so I assumed text areas, and others as well. See this example where I just used the JTextArea constructor I mentioned and just packed.

    EDITED with no sizes set

    import java.awt.BorderLayout;
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import static javax.swing.JFrame.EXIT_ON_CLOSE;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class ControlPanel extends JFrame {
    
        JScrollPane userspane;
        JList list;
        DefaultListModel model = new DefaultListModel();
        JScrollPane consolepane;
        JTextArea console = new JTextArea(20, 50);
        JTextArea jta = new JTextArea(6, 50);
        JPanel panel = new JPanel();
    
        JButton kick = new JButton("Kick");
        JButton ban = new JButton("Ban");
        JButton info = new JButton("Info");
    
        public ControlPanel() {
            // create our list of players
            list = new JList(model);
    
            // create our scroll panes
            userspane = new JScrollPane(list);
            consolepane = new JScrollPane(console);
    
            // add to panel
            panel.add(userspane, BorderLayout.CENTER);
            panel.add(kick);
            panel.add(ban);
            panel.add(info);
            panel.add(consolepane, BorderLayout.CENTER);
    
            add(panel);
            pack();
            setTitle("RuneShadows CP");
            //setResizable(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    new ControlPanel();
                }
            });
        }
    }
    

    UPDATE - with positioning

    Keep in mind also, with BorderLayout you need to specify a position for every component you add or else it will default to CENTER and each position an only have one component. I noticed you trying to add two components to the CENTER

    import java.awt.BorderLayout;
    import javax.swing.DefaultListModel;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import static javax.swing.JFrame.EXIT_ON_CLOSE;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class ControlPanel extends JFrame {
    
        JScrollPane userspane;
        JList list;
        DefaultListModel model = new DefaultListModel();
        JScrollPane consolepane;
        JTextArea console = new JTextArea(20, 50);
        JTextArea jta = new JTextArea(6, 50);
        JPanel panel = new JPanel(new BorderLayout());
    
        JButton kick = new JButton("Kick");
        JButton ban = new JButton("Ban");
        JButton info = new JButton("Info");
    
        public ControlPanel() {
            // create our list of players
            list = new JList(model);
    
            // create our scroll panes
            userspane = new JScrollPane(list);
            consolepane = new JScrollPane(console);
    
            // add to panel
            panel.add(userspane, BorderLayout.SOUTH);
            JPanel buttonPanel = new JPanel();
            buttonPanel.add(kick);
            buttonPanel.add(ban);
            buttonPanel.add(info);
            panel.add(buttonPanel, BorderLayout.CENTER);
            panel.add(consolepane, BorderLayout.NORTH);
    
            add(panel);
            pack();
            setTitle("RuneShadows CP");
            //setResizable(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    new ControlPanel();
                }
            });
        }
    }