0

I'm making a text editor in Java and I want to have a JFrame with a JPanel in it, and then a JTextArea and a JToolBar inside of the JPanel. The frame shows up how I want it, and the toolbar does although it's empty right now, but the JTextArea doesn't even appear. I thought I might need to specify a size but even when I did that it didn't make it appear.

I have three classes, the Main class, the GUI class, and the Screen class.

Here's the code in my Main class

public class Main {
    // Attributes:
    private static Screen screen = new Screen();
    private static GUI ui = new GUI();

    // Behaviors:
    public static void main(String[] args) {
        // Creates the JFrame with its preset attributes
        screen.createFrame();
        ui.addGUI();
        Screen.frame.pack();
        Screen.frame.setVisible(true);
    }

Here's the code in my Screen class:

public class Screen {
    // Attributes / Instance Variables:
    private final int WIDTH = 854;
    private final int HEIGHT = 480;
    private final static String TITLE = "Lightweight Java Text Editor";
    public static JFrame frame = new JFrame(TITLE);
    private Dimension dimensions = new Dimension(WIDTH, HEIGHT);

    // Behaviors / Methods:

    /*
     * Generates the frame to have a certain size, allows it to be resized, centers
     * it on the screen, terminates Java when the window is closed, and makes it
     * visible.
     * 
     * The width and height are ALWAYS initially 854x480!
     */
    public void createFrame() {
        frame.setPreferredSize(dimensions);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        // frame.pack(); makes the three
        // lines above actually show up
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    // Getters and Setters for private variables that may be needed
    public Dimension getDimensions() {
        return dimensions;
    }

    public void setDimensions(Dimension dimensions) {
        this.dimensions = dimensions;
    }

    public int getWIDTH() {
        return WIDTH;
    }

    public int getHEIGHT() {
        return HEIGHT;
    }

    public String getTITLE() {
        return TITLE;
    }
}

And finally, here's the code in my GUI class

public class GUI {
    // Attributes / Instance Variables:
    // The container
    JPanel panel = new JPanel();
    // The area where the user types
    JTextArea textEditingField = new JTextArea(600,200);
    // The tool-bar on the left side
    JToolBar tools = new JToolBar();

    public void addGUI() {
        Screen.frame.add(panel);
        Screen.frame.add(textEditingField);
        Screen.frame.add(tools);
    }
}

What have I done wrong?

VArchive
  • 13
  • 3
  • 1
    A JFrame uses BorderLayout by default (actually, it's content pane, but this is where components are added). When you add components to a BorderLayout-using container in a default fashion (you use no BorderLayout constraints), the components are added to the BorderLayout.CENTER position, and only the last one added will be displayed. Solution: 1) Learn to use the layout managers and nested JPanels to allow you to see multiple components added. 2) side recommendation to avoid over-use of static fields as your program does. This can increase the risk of bugs. – Hovercraft Full Of Eels May 01 '20 at 21:35
  • 1
    Side issue #2) regarding `new JTextArea(600,200);`, that is one flipping *huge* JTextArea. I applaud your using row and column properties and not setting bounds, but those values that you're passing in look like you're thinking in terms of pixels (bad) and not rows and columns (the correct way to do this). Also, you will want to put the JTextArea into a JScrollPane in case you need scrolling functionality. – Hovercraft Full Of Eels May 01 '20 at 21:38
  • @HovercraftFullOfEels thanks for the solution! I assumed it autoformatted them for some reason, and thanks for the tip on using static. EDIT: Yes I was thinking of pixels, thanks for correcting me. I was planning on adding a scoll pane after I got the area to display. – VArchive May 01 '20 at 21:38
  • Good luck with your project and you're welcome – Hovercraft Full Of Eels May 01 '20 at 21:39

0 Answers0