0

If I create a main method in my GUI class, and make an object, the program runs and works fine. However, when I try creating an object of this class in another file, the window quickly disappears.

I'm not sure why this is the case, I tried setting the frame to be visible and that doesn't work, I also tried creating a method that allows one to explicitly call the set visibility method and that didn't work either. I don't know what I'm doing wrong, any help is greatly appreciated.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class BoxGUI extends JFrame{

    private JFrame frame;
    public BoxGUI(String text1, String text2) {

        //components 
        String title = "Test";
        frame = new JFrame(title);

        Container content = frame.getContentPane();
        content.setLayout(new GridLayout(0,2, 3, 4)); //Creates Grid layout 

        JTextArea leftTextArea = new JTextArea();
        leftTextArea.append("i am on the left");
        leftTextArea.append(text1);

        JTextArea rightTextArea = new JTextArea() {
            public boolean isManagingFocus() {
                return false;
            }
        };
        rightTextArea.append("i am on the right");
        rightTextArea.append(text2);

        content.add(leftTextArea);
        //leftTextArea.paste();

        content.add(rightTextArea);     
        //rightTextArea.paste();

        frame.pack();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);// closes frame on exit 
    }

    public void displayGUI() {
        this.frame.setVisible(true);
    }
    public void CreateTextAreaComponents(String text1,String text2) {
        String title = "Test";
        frame = new JFrame(title);

        Container content = frame.getContentPane();
        content.setLayout(new GridLayout(0,2, 3, 4)); //Creates Grid layout 

        JTextArea leftTextArea = new JTextArea();
        leftTextArea.append("i am on the left");
        leftTextArea.append(text1);

        JTextArea rightTextArea = new JTextArea() {

            public boolean isManagingFocus() {
                return false;
            }
        };
        rightTextArea.append("i am on the right");
        rightTextArea.append(text2);

        content.add(leftTextArea);
        //leftTextArea.paste();

        content.add(rightTextArea);     
        //rightTextArea.paste();

        frame.pack();
        frame.setVisible(true); 
        content.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);// closes frame on exit 
    }

    public static void main (String []alex) {
        /* 
        String text1, text2;
         text1 = "\nhi, i amm on the left";
         text2 = "\nhi, i am on the right!";
         BoxGUI test = new BoxGUI(text1, text2);
         test.displayGUI();
         */
    }
}

my other file.

import javax.swing.*;
import java.util.*;
import java.io.*;

public class Project1{
    public static void main(String []args){

        String text1, text2;
        text1 = "\nhi, i amm on the left";
        text2 = "\nhi, i am on the right!";
        BoxGUI test = new BoxGUI(text1, text2);
        test.displayGUI(); 
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • Shall we properly format the code, or can you do this on your own? – user unknown Mar 07 '18 at 17:29
  • You have to `pack()` the frame to lay it out, then call `setVisible(true);` Try that, if it doesn't work add the lines to the code you have posted and let us know what the new problem is. – markspace Mar 07 '18 at 17:30
  • 1
    Your code works fine in my computer. Either having the main method in `BoxGUI` class or `Project1` class. However you can safely remove `extends JFrame` as you're not chaging he behavior of it (and never make use of this `JFrame`. And instead have `JFrame.EXIT_ON_CLOSE` in your `setDefaultCloseOperation` calls. `CreateTextAreaComponents` is never called so you can remove it as well (at least for this example). Related to `extends JFrame` read [this question](https://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program) – Frakcool Mar 07 '18 at 17:50
  • Also please follow [Java naming conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html): `firstWordLowerCaseVariable`, `firstWordLowerCaseMethod()`, `FirstWordUpperCaseClass` and `ALL_WORDS_UPPER_CASE_CONSTANT` so your code becomes easier to read and understand for you and us. Also be sure to indent your code correctly so it's easier to follow the flow of the program. – Frakcool Mar 07 '18 at 17:52
  • @Frakcool thank you so much, although i'm still not sure why it doesn't work on my comp. Maybe its this new mac OS update. I'm using eclipse. i'm wondering if that may be a factor. – alxanderapollo Mar 07 '18 at 18:10
  • I have a Mac at home with High Sierra, but I won't be able to test until 7 hours from now... however I don't think it has something to do with OS or IDE in general. Have you tried making a full project clean? Or perhaps creating a new project from scratch and paste the file's contents into it? There are times that those "hacks" solve the issue. – Frakcool Mar 07 '18 at 18:11
  • I was just in the process of doing that @Frakcool – alxanderapollo Mar 07 '18 at 18:12
  • Tell me how it goes :) – Frakcool Mar 07 '18 at 18:13

0 Answers0