Home > Enterprise >  I can't understand why JScrollpane won't be added to JTextArea in my TextEditor
I can't understand why JScrollpane won't be added to JTextArea in my TextEditor

Time:10-16

I can't understand why the JScrollpane won't be added to JTextArea, is this because of some sort of layout problem or something? This is a text editor made by my friend, he initially made it with only AWT but I then replaced AWT TextArea with swing's JTextArea to wrap text. Btw this is my 1st question ever posted on this site. If u need some more details or want me to edit something, just lemme know. Thanks!

Output: image

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class CodePad {

    public static void main(String[] args) {
        EventQueue.invokeLater(new CodeEditor()::create);
    }

    private static final class CodeEditor {

        JMenuBar bar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenu edit = new JMenu("Edit");
        JTextArea textArea = new JTextArea("Some text…", 8, 36);

        public void create() {
            JFrame f = new JFrame("CodePad");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            bar.add(file);
            bar.add(edit);
            f.setJMenuBar(bar);

            textArea.setForeground(Color.BLUE);
            textArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 16));
            textArea.setLineWrap(true);
            JScrollPane scrollArea = new JScrollPane(textArea);
            f.add(scrollArea); // default BoderLayout.CENTER

            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
}
  • Related