Search code examples
javaswingjpaneljtextarea

Centre JTextArea in JPanel


I can't believe there are no answers for this... I want to simply centre a JTextArea in a JPanel. I am using BoxLayout for this. When I run my program, the JTextArea takes up the whole screen. Why is this?

public class BLayout extends JFrame implements ActionListener {

    public BLayout() {
        super("GUI Testing");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel choosePanel = new JPanel();
        choosePanel.setLayout(new BoxLayout(choosePanel, BoxLayout.X_AXIS));
        choosePanel.setBackground(Color.BLUE);

        JTextArea text = new JTextArea(1, 10);
        text.setLineWrap(true);
        text.setEditable(false);
        text.setText("Welcome to Library Search.\n\n"
                + "Choose a command from the \"Commands\""
                + " menu above for adding a reference, "
                + "searching references, or quitting the program.");
        choosePanel.add(text);
        add(choosePanel);
    }

How can I make the text area just sit in the middle of the panel without taking up the whole screen?


Solution

  • I am using BoxLayout for this. When I run my program, the JTextArea takes up the whole screen. Why is this?

    Because that's how BoxLayout works. You could use a GridBagLayout instead, which centers the components within the container by default, for example

    Example layout

    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class BLayout extends JFrame {
    
        public BLayout() {
            super("GUI Testing");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLayout(new GridBagLayout());
    
    
            JTextArea text = new JTextArea(7, 40);
            text.setLineWrap(true);
            text.setWrapStyleWord(true);
            text.setEditable(false);
            text.setText("Welcome to Library Search.\n\n"
                    + "Choose a command from the \"Commands\""
                    + " menu above for adding a reference, "
                    + "searching references, or quitting the program.");
            add(text);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    BLayout frame = new BLayout();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }