Search code examples
javaswingjlabeljtextareaboxlayout

JLabel moves with text entered into JTextArea


I am creating a swing file editor. I have a JLabel, and a JTextArea in my JFrame. When I type in the JTextArea, I notice the JLabel moving. Here is a Screencastify of this: Screencastify

Is this related to the BoxLayout I am using? Can you please help me explain this behavior, and possibly solve it?

Here is my code:

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter;

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileNotFoundException;

public class Editor2 extends JFrame{
    private static final long serialVersionUID = 1L;
    private static final String appName="JEdit: ";
    Container c;
    JMenuBar menubar;
    JMenu filemenu,edit,optionsmenu;
    JMenuItem save,saveas,
        newfile,openfile,close,
        find,clearfind,
        textcolor;
    JLabel filetitle;
    JTextArea filecontent;
    WriteFile out;
    ReadFile in;
    JFileChooser jfc;
    File f;
    Document filecontentdoc;
    boolean upToDate;
    Highlighter h;
    DefaultHighlightPainter dhp;
    public Editor2() throws FileNotFoundException {
        super(appName+"Untitled");
        f=null;
        upToDate=true;
        c=getContentPane();
        c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        filetitle = new JLabel("Editing Untitled");
        c.add(filetitle);
        menubar=new JMenuBar();
        setJMenuBar(menubar);
        filemenu = new JMenu("File");
        edit = new JMenu("Edit");
        optionsmenu = new JMenu("Options");
        menubar.add(filemenu);
        menubar.add(edit);
        menubar.add(optionsmenu);
        save=new JMenuItem("Save");
        saveas=new JMenuItem("Save As");
        newfile=new JMenuItem("New File");
        openfile=new JMenuItem("Open File");
        close=new JMenuItem("Close");
        filemenu.add(save);
        filemenu.add(saveas);
        filemenu.add(newfile);
        filemenu.add(openfile);
        filemenu.add(close);
        find=new JMenuItem("Find");
        clearfind=new JMenuItem("Clear Highlights");
        edit.add(find);
        edit.add(clearfind);
        textcolor=new JMenuItem("Text Color");
        optionsmenu.add(textcolor);
        filecontent = new JTextArea(50,50);
        c.add(filecontent);
        filecontentdoc=filecontent.getDocument();
        filecontentdoc.addDocumentListener(new DocumentListener() {
            @Override public void removeUpdate(DocumentEvent e) {}
            @Override
            public void insertUpdate(DocumentEvent e) {
                upToDate=false;
            }
            @Override public void changedUpdate(DocumentEvent e) {}
        });
        h = filecontent.getHighlighter();
        dhp = new DefaultHighlightPainter(Color.YELLOW);
        //pack();
        setSize(1000, 1000);
        this.addWindowListener(new WindowListener() {
            @Override public void windowOpened(WindowEvent e) {}
            @Override public void windowIconified(WindowEvent e) {}
            @Override public void windowDeiconified(WindowEvent e) {}
            @Override public void windowDeactivated(WindowEvent e) {}
            @Override public void windowClosing(WindowEvent e) {
                dispose();
                System.exit(0);
            }
            @Override public void windowClosed(WindowEvent e) {
                dispose();
                System.exit(0);
            }
            @Override public void windowActivated(WindowEvent e) {}
        });
    }
    public static void main(String[] args) throws FileNotFoundException {
        Editor2 ef = new Editor2();
        ef.setVisible(true);
    }
    public void setVisible(boolean b){
        super.setVisible(b);
    }
}

Solution

  • You forgot to place the JTextArea in a JScrollPane. Change this:

    c.add(filecontent);
    

    to this:

    c.add(new JScrollPane(filecontent));
    

    Also, you probably should use a BorderLayout instead of a BoxLayout, and you should put that JScrollPane in the center of the BorderLayout. Note that a JFrame’s content pane already uses a BorderLayout by default, so you don’t have to change the layout at all, just specify the correct BorderLayout constraints when adding components to it.