Search code examples
javaswing

Resize JTextArea with window


Hello i'm new to Java Swing and i decided to make text editor:

import java.awt.FlowLayout;  
import javax.swing.*;
public class main {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(500, 500);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().setLayout(new FlowLayout());
        JTextArea textArea = new JTextArea();
        JScrollPane scrollableTextArea = new JScrollPane(textArea);
        scrollableTextArea.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollableTextArea.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        f.pack()
  
        f.getContentPane().add(scrollableTextArea);
    }
}

but JTextArea is not resizing with window

I tried getting window size and resizing JTextArea by window size (i lost the code)

Please help!


Solution

  • Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the Laying Out Components Within a Container section.

    Study the rest of the tutorial later.

    Here's a GUI that illustrates a JTextArea that changes size when the JFrame changes size. I used Swing layout managers.

    Example

    All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

    A JFrame has a default BorderLayout. I used a BorderLayout for my JPanel.

    Get into the habit of placing Swing components in a JPanel.

    Use much more descriptive field names. Use multiple methods and classes. It makes the code easier for other people to read and understand.

    Here's the complete runnable code.

    import java.awt.BorderLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class JTextAreaExample2 implements Runnable {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new JTextAreaExample2());
        }
    
        @Override
        public void run() {
            JFrame frame = new JFrame("JTextArea Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.add(createTextAreaPanel(), BorderLayout.CENTER);
    
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private JPanel createTextAreaPanel() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    
            JTextArea textArea = new JTextArea(20, 60);
            JScrollPane scrollPane = new JScrollPane(textArea);
            panel.add(scrollPane, BorderLayout.CENTER);
    
            return panel;
        }
    
    }