2

How would one go about sharing an object between static methods? Is this even possible? Is there a better way?

In my code, I have one frame in one class. I have different static methods to do stuff with this frame in the same class. I call these methods in another class.

Is there a better way of doing this for swing? Essentially I want to create a Frame, in which I can edit the size, and colour, and contents through the methods of other classes (these classes would have Panels which I would use on this frame).

Class calling the static methods:

import javax.swing.*;

public class Main{

    // Display Login Window
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try
            {
                new loginFrames();
                loginFrames.showFrame("hello", 1200, 800);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        });
    }
}

Class with the static methods:

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

public class loginFrames{

    public JFrame appFrame;


    public void createFrame(String frameName, int frameW, int frameH) {

        // Create JFrame with frame name, width, and height
        appFrame = new JFrame(frameName);
        appFrame.setSize(frameW, frameH);

        // Get the content pane and set the background colour black
        Container absFrameContentPane = appFrame.getContentPane();
        absFrameContentPane.setBackground(Color.BLACK);

        // Show the frame
        appFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        appFrame.setVisible(true);
    }

    // Callable from Main class
    public static void showFrame(String frameName, int frameW, int frameH) {
        loginFrames applicationFrame = new loginFrames();
        applicationFrame.createFrame(frameName, frameW, frameH);
    }

    // Callable from Main class
    public static void appAddPanel(String frameName, int frameW, int frameH) {
    // How do I call the same object above, here?
    }
}
1

2 Answers 2

1

Your code violates various OO principles, first of all information hiding/encapsulation

Your variable appFrame should be declared private and accessed only from withing the loginFrames class.

Also: since the variable appFrame is not declared static so it cannot be accessed from the static methods in that class (directly).

The solution is to remove the static key word from the methods and access them via an object of the class (does that sound weired to use objects in an object oriented language?)

Sign up to request clarification or add additional context in comments.

Comments

0

For me, I prefer using a external file for your JFrame option. FileReader for getting the value from the file, loop and assign them either on array or variables. And PrintWriter for changing the option. Hope this help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.