This discussion is archived
8 Replies Latest reply: Oct 8, 2010 1:22 AM by jduprez RSS

MVC cocoa design issue

803354 Newbie
Currently Being Moderated
Hello,

I found this tutorial on the oracle website http://www.oracle.com/technetwork/articles/javase/mvc-136693.html and of course the sourcecode works. Then I tried myself to apply the design to a simpler application. But my code doesn't work, the only difference is that my view is not generated by netbeans but I made it myself.
Could you help me ?

Source code : http://www.megaupload.com/?d=KBTWAN35
Sorry if I should upload it elsewhere but I didn't know where.

Best regards,

rXp>!<
  • 1. Re: MVC cocoa design issue
    jduprez Pro
    Currently Being Moderated
    Source code : http://www.megaupload.com/?d=KBTWAN35
    Sorry if I should upload it elsewhere but I didn't know where.
    You'll find out that most people here won't follow links to external sites to download code (myself included).
    Instead, you should try to reduce the code to a short, self-contained, compilable and executable example ([url http://sscce.org]SSCCE); that will help you find the problem yourself, and if it doesn't, then you can post it here (between code tags).
    But my code doesn't work
    That's not a problem description.
    Please try to describe what doesn't work (actual vs expected behavior).
    the only difference
    That what I always say too, until I analyze the problem piece by piece, and discover a difference that does matter!... :o)
    is that my view is not generated by netbeans but I made it myself.
    Then there is a bug in the code you made yourself. Presumably, this is more a "Java programming" or "Swing" problem than an "OO design and pattern" problem. Feel free to post more details about it anyway, so that we can help.
    Could you help me ?
    Gladly so. Please see above, help us to help you.

    Regards,

    J.
  • 2. Re: MVC cocoa design issue
    803354 Newbie
    Currently Being Moderated
    Okey :)

    So first my problem with a little more information ;)
    My view has : 1 jtextfield, 1jcombobox, 1 jtextfield to show the result.
    The first jtextfield should change the text in the last and the combobox should enable or disable the component.
    When I make changes, nothing happend in my jtextfield that should show the change. The code execute itself correctly but it get "lost" at this line :

    propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); // in the AbstractModel

    After this line nothing appends in my java files, that's why I said "it get lost" (eclipse protects the sourcecode).

    So I don't really know what file to show you.

    Best regards;

    rXp>!<
  • 3. Re: MVC cocoa design issue
    jduprez Pro
    Currently Being Moderated
    The first jtextfield should change the text in the last and the combobox should enable or disable the component.
    So, you have added a DocumentListener, or ActionListener, or some other listener kind, to the first JTextField, and listener code is supposed to update the second one accrodginly?
    propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); // in the AbstractModel

    After this line nothing appends in my java files
    So, you probably haven't added a PropertyListener... Have you added any listener at all?
    So I don't really know what file to show you
    I didn't say you had an SSCCE available off the top of your disk: you have to construct it, either by removing anythig secondary in your code, or by creating code from scratch.
    I estimate that this sentence "The first jtextfield should change the text in the last and the combobox should enable or disable the component" (excluding any other feature not described here) can be implemented in a 50-lines class.

    We'll gladly help (but I stand convinced that you have a Java/Swing problem merely than a misunderstanding of the MVC "pattern").

    Edited by: jduprez on Oct 6, 2010 1:38 PM
  • 4. Re: MVC cocoa design issue
    796367 Explorer
    Currently Being Moderated
    My view has : 1 jtextfield, 1jcombobox, 1 jtextfield to show the result.
    The first jtextfield should change the text in the last and the combobox should enable or disable the component.
    RXP, I suggest a more practical version of your app using more appropriate Swing components. For example,
    1. Use a JTextField for text input.
    2. Use a JLabel to display the text input.
    3. Use a JCheckBox to enable/disable the component.
    Also, I suggest because this is such a simple GUI, to type it n a plain text editor -- not an IDE. Now I have a question, when do we get some cocoa with that?
  • 5. Re: MVC cocoa design issue
    803354 Newbie
    Currently Being Moderated
    Okey, I'm back. (sorry very busy days)
    So the abstract class are the same as the ones given by oracle on the tutorial page.
    now the code (first time I try to "sum up" my code like that, so be gentle :p)

    /* ----------------- MAIN class ------------------------------------------- */
    [...]
    DefaultModel model = new DefaultModel();

    DefaultController controller = new DefaultController();

    MainView view = new MainView(controller);

    controller.addModel(model);
    controller.addView(view);

    model.initDefaultState();

    [...]

    /* ------------------- Default Model class ----------------------------- */
         
         [...]
         
         /* reset to default state */
         public void initDefaultState(){
              this.text = "Test Default";
              this.enable = true;
         }
         
         public String getText(){
              return this.text;
         }
         
         public void setText(String text){
              String oldText = this.text;
              this.text = text;
              
              firePropertyChange(DefaultController.TEXT_PROPERTY, oldText, this.text);
         }
         
         [...]
         // same for the boolean enable
         
         /* ------------------ Default Controller --------------------------------------- */
         [...]
         
         public static final String TEXT_PROPERTY = "Text";
         
         public static final String ENABLE_PROPERTY = "Enable";
         
         public void changeTextProperty(String newText){
              setModelProperty(TEXT_PROPERTY, newText);
         }
         
         // same for the boolean enable
         [...]
         
         /* ------------------------ MainView ----------------------------------------------- */
         
         @Override
         public void modelPropertyChange(final PropertyChangeEvent event) {


    if (event.getPropertyName().equals(DefaultController.TEXT_PROPERTY)) {

    String newStringValue = event.getNewValue().toString();

    if (!finalText.getText().equals(newStringValue))
    finalText.setText(newStringValue);

    }
    else if [...] // same for the checkbox
         }
         
         [...] //setting of the view
         
         public class TextActionListener implements ActionListener{

              @Override
              public void actionPerformed(ActionEvent arg0) {
                   controller.changeTextProperty(text.getText());
              }
              
         }
         
         // same for the checkbox but with a MouseListener
         [...]
         
         /*------------------------------------------------------------------------------------------ */

    Edited by: rXp on Oct 8, 2010 12:44 AM
  • 6. Re: MVC cocoa design issue
    jduprez Pro
    Currently Being Moderated
    rXp wrote:
    now the code (first time I try to "sum up" my code like that
    I take it that you didn't read the [url http://sscce.org]SSCCE article (N.B.: this is a link). This is not about "summing up" your code (indeed your summary leaves 50% of the code out, and who know whether the problem lies within the public 50% or the hidden 50%?). I encourage you to read it.
    so be gentle :p)
    I hope I was...
    Yourself, please be gentle too, and take the endeavour to read our replies. Note that I also mentioned using code tags, this enables to post properly formatted code like this (reply and quote my post to see the markup):
    public class XyZ extends Whatever {
        public void method(String arg) {
            ...
        }
    }
    Now, from a cursory look at your code:
           public class TextActionListener implements ActionListener{
    
              @Override
              public void actionPerformed(ActionEvent arg0) {
                   controller.changeTextProperty(text.getText());
              }
              
         }
         
    1) Do you add this listener to the JTextField? I see no addActionListener(..) in the code extract...
    2) Do you need to see the changes as soon as the user types a character, or only when he presses enter? A JTextField's ActionListener is only called back when the user has finished editing (as described in the [url http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html]tutorial), not at each keystroke. If you want Keystroke-level update, use a DocumentListener.
          // same for the checkbox but with a MouseListener
    You usually don't handle user actions on a Checkbox using a MouseListener, you merely use an ItemListener.

    The more you disclose and the more I believe your problem is related to Swing and not to MVC. I encourage you to read the [url http://download.oracle.com/javase/tutorial/uiswing/index.html]Swing tutorial, and learn how to program using the Swing API, before attempting to use elaborate "frameworks" over it.
  • 7. Re: MVC cocoa design issue
    803354 Newbie
    Currently Being Moderated
    I updated my JAVA and then my code worked :s
  • 8. Re: MVC cocoa design issue
    jduprez Pro
    Currently Being Moderated
    Another forum etiquette advice then: be accurate and open about what was wrong and what you did to correct it, in case someone else has a related problem and finds this topic later. This is a general advice, in this particular case since you haven't described what your problem was it's unlikely that anyone thinks "hay I have the same problem".

    Regards and much luck with Java, Swing, and patterns.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points