RyanSoper
RyanSoper

Reputation: 231

Use another classes JFrame

I tried searching for it, but I don't really know what terminology to use, so get no results. Basically, I have a JFrame created in one class that inherits nothing, But there are then 2 classes that feed into this, Farmer.java and SheepHandlerThread.java I want to use the JFrame created in the initial class and add some extra shapes and objects to it, as SheepHandlerThread handles the Sheep Grazing and Behaviour and Farmer.java controls a square which goes to the position of your mouse click.

Apologies if I asked an already answered question, but I've started to go around in circles and confuse myself, so I'm hoping to get some help here. Ask if you need any clarification.

Thanks, Ryan

Upvotes: 0

Views: 537

Answers (2)

Ketan
Ketan

Reputation: 324

I agree with above answer. In addition to that i putting example that will help you.

class FrameDemo{

    private JFrame frame;
    private SheepHandlerThread sheepHandlerThread;
    private Farmer farmer;

    public FrameDemo(){
       initComponents();
    }

    private void initComponents() {
        frame = new JFrame(...);
        /* Pass frame reference to the 
           Farmer and SheepHandlerThread
           class    
            */
      farmer = new Farmer(frame);
      sheepHandlerThread = new SheepHandlerThread(frame);   
    }

}

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691645

The two objects need to have a reference to the JFrame object in some way. When constructing the two objects, pass them the JFrame object. The constructor can then store the frame in an instance field, and do whatever they want to do with it whenever they want.

Or these objects shouldn't be concerned with the UI, and should only provide services to the frame. In this case, have the frame call these services and update itself with the result of those services.

Upvotes: 1

Related Questions