Question

I have a Java assignment where we have to create a simple enough world clock application with the MVC design pattern.

The application involves allowing a user to view several times (or clocks) on their screen simultaneously. They select these clocks from a Jlist created from an array of cities(which have their relevant time differences separated with a comma eg. "Barcelona,+1.0". Then press an add button to display the clock/clock for these cities. There is also a remove city button which works in the same manner as the add button except obviously removes the clock from being displayed. The clocks and cities are displayed in two side be side Jscroll panes.

I am struggling with a few things.

  • I cannot figure out know how to connect the buttons to an action listener and then a selection listener through MVC?
  • I have been trying to figure out how to store the selected cities in an array list and then iterate through this list to display/hide relevant clocks. Is this the best way to do this? I can't help but feel there would be a more elegant solution.
  • I'm also unsure as to the best way to create multiple clocks? I tried for loops but kept running into problems here is the code I am using to create one clock.

    myClock = new JTextField(8);
    myClock.setFont(font);
    myClock.setEditable(false);
    myClock.setFocusable(false);
    myClock.setHorizontalAlignment(JTextField.CENTER);
    myClock.setBorder(BorderFactory.createTitledBorder(lineBorder, "City"));
    clockPanel.add(myClock);
    

Then the clock time is updated using this method.

    public void update() {
            myClock.setText(secondsAsTimeText(model.currentTime()));
        } 

Any help would be greatly appreciated I have been working on this for three days and have been getting no where. Please ask me to elaborate on anything if I have not been clear on. I'm also new to stack overflow so I hope I haven't made any rookie mistakes in my questions. Don't be afraid to call me out on them so I can learn from my mistakes :)

Was it helpful?

Solution

My advice :

  • store the diff time in a Map ( city's name,diffthecurrenttime) for example if you live in London and you need current time of New York city then you (NewYourk,-5) and you put all keys and its value when the model is initializing by loading a properties file and you don't have to rewrite the model class when you want to add or remove a city.

  • when the view is starting get all cities and put in the Jlist (the contllorer do this)

You do not have to iterate.. use the name of the cities as key to find the proper time as you can see in below example code.

Properties loading part:

Make file with .properties ex. like configuration.properties, and the content should be something like this:

NewYork=-5 Tokyo=8

and put the properties file in src folder.

Make a class file which will be load the information from properties file:

public class PropertiesLoader{

       public Map<String,Integer> loadCities(){
             Properties file=getProperties("configuration.properties");
             return convertPropertiesToMap(file); //it will be return empty Map if       unable to load properties

       }

       private Properties getProperties(String fileName){
              Properties prop = new Properties();
              InputStream input = null;

              try {
                   input = getClass().getClassLoader().getResourceAsStream(fileName);
                   if (input == null) {
                    //Unable to find file
                                return null;
                   }
                   prop.load(input);

              } catch (IOException ex) {
                           ex.printStackTrace();
              } finally {
                           if (input != null) {
                                  try {
                                        input.close();
                                  } catch (IOException e) {
                                        e.printStackTrace();
                                  }
                           }
          }
             return prop;
       }

       private Map<String,Integer> convertPropertiesToMap(Properties file){
              Map<String,Integer> result=new HashMap<String,Integer>();
                        if(file != null){
                               Enumeration<?> e = file.propertyNames();
                               while (e.hasMoreElements()) {
                                      String key = (String) e.nextElement();
                                      Integer value = Integer.parseInt(file.getProperty(key));
                                      result.put(key,value);
                               }
                    }     
              return result;
       }


}

You have to create the PropertiesLoader class in the model class and store the information in the model by calling loadCities() method.

MVC part: Your controller will connect them:

Here it is a example code:

   public class Controller {

       private final View view;

       private final Model model;

       public Controller(View aview, Model aModel){
              this.view=aview;
              this.model=amodel;
              registerListeners();
       }

      public void registerListener(){
             ActionListener actionListener= new ActionListener(){
                       public void actionPerformed(ActionEvent e) {    
                              String city=view.getSelectedCity();
                              view.swithClockToCity(model.getCityTime(city));                                 
                       }

             }; 
         view.addListenerToOkButton(actionListener);
      }


   }

Somewhere in View class should be this

public class View{

       private JList cityList;

       private JButton selectCityButton;
       //other stuff

       public String getSelectedCity(){
              cityList.getSelectedItem().toString();
       }

       public void addListenerToOkButton(ActionListener alistener){
               selectCityButton.addActionListener(alistener);
       }

       public void swithClockToCity(Long time){
         //change the clock panel or something...
         //I put here your code:
          myClock.setText(secondsAsTimeText(time));
       }


}

You have to write the remove button and its action in similar way. I hope I could help you! If you have quetions, don't hesitate to ask!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top