Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6079467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:56:59+00:00 2026-05-23T10:56:59+00:00

I am trying to build a simple Java application with SWT using the MVC

  • 0

I am trying to build a simple Java application with SWT using the MVC pattern. I would like to be able to automatically update the view when something happens in the background so I am trying to use the Observer/Observable pattern, but it looks like my Observer is never notified when my Observable changes.

Here is the code:

Launcher.java (main class)

public class Launcher {

    public static void main(String[] args) {
        Application app = new Application();
        PenguinView v = new PenguinView(app);
        Controller api = new Controller(app, v);
    }

}

Application.java (The background application)

public class Application {
    private Penguin _myPenguin;

    public Application() {
        _myPenguin = new Penguin();
        new Thread(_myPenguin).start();
    }

    public Penguin getPenguin() {
        return _myPenguin;
    }
}

Penguin.java (The model, my Observable)

public class Penguin extends Observable implements Runnable {
    private String _color;
    private boolean _isHappy;
    private int _myRocks;

    public Penguin() {
        _color = "Black";
        _isHappy = true;
        _myRocks = 0;
    }

    public void paint(String color) {
        _color = color;
        System.out.println("My new color is " + _color);
        setChanged();
        notifyObservers();
    }

    public String getColor() {
        return _color;
    }

    public void upset() {
        _isHappy = false;
        setChanged();
        notifyObservers();
    }

    public void cheerUp() {
        _isHappy = true;
        setChanged();
        notifyObservers();
    }

    public boolean isHappy() {
        return _isHappy;
    }

    public void run() {
        // Penguin starts walking and find rocks!
        while(true) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            iFoundRock();
        }
    }

    private void iFoundRock() {
        _myRocks++;
        System.out.println("I now have " + _myRocks + " rocks");
        setChanged();
        notifyObservers();
    }

}

PenguinView.java (The SWT View, my observer)

public class PenguinView implements Observer {
    private Application _app;

    private Display _d;
    private Shell _s;

    private Label _penguinColor;
    private Label _penguinHumor;
    private Label _penguinRocks;
    private Button _upset;
    private Button _cheerUp;
    private Text _newColor;
    private Button _paint;


    public PenguinView(Application app) {
        _app = app;
        _d = new Display();
        _s = new Shell();

        RowLayout rl = new RowLayout();
        rl.marginWidth = 12;
        rl.marginHeight = 12;
        _s.setLayout(rl);

        new Label(_s, SWT.BORDER).setText("Penguin Color: ");
        _penguinColor = new Label(_s, SWT.BORDER);
        _penguinColor.setText(_app.getPenguin().getColor());

        new Label(_s, SWT.BORDER).setText(" Humor: ");
        _penguinHumor = new Label(_s, SWT.BORDER);
        String humor = _app.getPenguin().isHappy() ? "Happy" : "Sad";
        _penguinHumor.setText(humor);

        new Label(_s, SWT.BORDER).setText(" Rocks: ");
        _penguinRocks = new Label(_s, SWT.BORDER);
        _penguinRocks.setText(String.valueOf(_app.getPenguin().howManyRocks()));

        _upset = new Button(_s, SWT.PUSH);
        _upset.setText(":(");
        _upset.addListener(SWT.Selection, new Listener(){
            public void handleEvent(Event e) {
                _penguinHumor.setText("Sad");
            }
        });

        _cheerUp = new Button(_s, SWT.PUSH);
        _cheerUp.setText(":)");
        _cheerUp.addListener(SWT.Selection, new Listener(){
            public void handleEvent(Event e) {
                _penguinHumor.setText("Happy");
            }
        });

        _newColor = new Text(_s, SWT.BORDER);

        _paint = new Button(_s, SWT.PUSH);
        _paint.setText("Paint!");
        _paint.addListener(SWT.Selection, new Listener(){
            public void handleEvent(Event e) {
                //_penguinColor.setText(_newColor.getText());
                _app.getPenguin().paint(_newColor.getText());
            }
        });

        _s.pack();
        _s.open();
        while (!_d.isDisposed()) {
            if (!_d.readAndDispatch()) {
                _d.sleep();
            }
        }
    }

    public void update(Observable obs, Object obj) {
        System.out.println("I go here!");
        _penguinRocks.setText(String.valueOf(((Penguin)obs).howManyRocks()));
        if(obs.equals(_app.getPenguin())) {
            _penguinRocks.setText(String.valueOf(((Penguin)obs).howManyRocks()));
        }
    }

    public void update(Observable obs) {
        System.out.println("I go there!");
    }

Controller.java (The controller)

public class Controller {
    Application _app;
    PenguinView _v;

    public Controller(Application app, PenguinView v) {
        _app = app;
        _v = v;

        // Set observer
        _app.getPenguin().addObserver(_v);
    }

}

The output:

I now have 1 rocks
I now have 2 rocks
My new color is Blue
I now have 3 rocks
I now have 4 rocks

Do you have any idea of what I am doing wrong?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:56:59+00:00Added an answer on May 23, 2026 at 10:56 am

    From what I can tell, the while loop in the PenguinView constructor is blocking your main thread, so the Controller never gets instantiated and the observer is never added.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build an Java EE 6-application on GlassFish V3, using JSF 2.0,
I'm trying to build a simple HTTP Server using Java, using java.net.ServerSocket = new
I'm trying to build a simple AWT application in Java. I want all of
I am trying to build a simple app using google app engine, with java
I am trying to build a simple booking application for a school presented as
I am trying to build a simple nested html menu using HAML and am
I've been trying to build a simple prototype application in Django, and am reaching
I am trying to build a simple java program which creates a db file,
I try to build simple Java EE application that uses JPA + EJB3 and
I am trying to build a simple desktop application where the user enters the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.