Question about buttons...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: Question about buttons...

  1. #1
    Join Date
    Mar 2005
    Posts
    1

    Question about buttons...

    information removed
    Last edited by soccerbum84; 08-09-2010 at 04:08 PM. Reason: no longer need

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    the if statements for the radio buttons should be in the Gui class as it is counted as display logic.

    The shipping class should receive a flag, or have a method called based on which radio button was selected.

    e.g.

    calculate.setShippingMethod(Taxcalculation.GROUND);
    calculate.setShippingMethod(Taxcalculation.AIR);

    define the flags as

    public class Taxcalculation
    {
    final static int GROUND = 1;
    final static int AIR = 2;
    }
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

  3. #3
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    Ok, if i'm understanding your problem corrctly you actually don't look too far off. I'm assume this calculate method is a part of your GUI class and is called when a button is clicked? Yes, then, you are right that you are doing to much of the calulation work in your GUI, but the solution to this problem is rather straight forward, so stick with me here. In your claculation class you should have the following data members:

    Code:
    double amount;
    String city, state;
    boolean ground, air;
    static final taxRate;
    Then you will have a public method called calulate in this class that uses these values to claulate a total price (although city and state seem to have nothing to do with it, i guess its just their for fluff??) and returns it as type double. So all that (amount+shipping method)*taxRate stuff will be in this method. Also, at the begining of this method you may want to use a statement like this:

    Code:
    double shipCost;
    if(ground)
    shipCost = 5.0;
    else
    shipCost = 15.0;
    Also you will need a calculate constructor that takes all of thoose data members as arguments, like so:

    Code:
    public Claculate(String city, String state, double amount, boolean ground, boolean air){
    //Set fields here
    }
    Now for the second part. In you GUI class i imagine you have a button labeled something like "Calulate" or whatever. You need to add an action listener to this button (which im sure you already have) and inside the listener do something like this:

    Calculate c = new Calculate(city.getText(), state.getText(), Double.parseDouble(amount.getText()),ground.isSelected(),air.isSelected());

    double totalAmount = c.calulateTotal();//or whatever you call the method


    In you code you use something like this.getJTextFieldState().getText(). That baffels me a little. If your label is called state, then you can simply say state.getText(). Another point, you can actually elimnate the air boolean in the calulate class and it will still work, but i would leave it in for your professor, especially if he makes you update it at a later point, then the variable may be needed.

    I hope you understand what i just wrote and i hope you get your project in on time!
    Java has 99 problems but a pointer ain't one

  4. #4
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    Quote Originally Posted by JGRobinson
    Hi,

    the if statements for the radio buttons should be in the Gui class as it is counted as display logic.

    The shipping class should receive a flag, or have a method called based on which radio button was selected.

    e.g.

    calculate.setShippingMethod(Taxcalculation.GROUND);
    calculate.setShippingMethod(Taxcalculation.AIR);

    define the flags as

    public class Taxcalculation
    {
    final static int GROUND = 1;
    final static int AIR = 2;
    }

    Yea he could also do this, and i would imagine this would look better then a bunch of booleans (if it actually is i don't know). However, i would keep the class immutable and just set the shipping method through the constructor. That way if down the line he has to modify this program to store multiple Calculations there won't be a chance of class instances getting corrupted. But i guess thats a moot point if this is just for a programming class.
    Java has 99 problems but a pointer ain't one

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    The value/state of a component can be picked up by any class that is set up to
    listen to it, I thought I should just mention that, as an option.
    eschew obfuscation

  6. #6
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    "The value/state of a component can be picked up by any class that is set up to
    listen to it, I thought I should just mention that, as an option."

    NOTE: I tried to send you a PM but it said the message was too long and i didn't want to break it up.

    Could you please elaborate further on this for me. I have a problem in one of my current projects and i think what your describing is what i need to solve. For example, lets say i have a GUI class with all the GUI code and such. Now lets say i also have another class that is performing a specific operation (the operation is running in a Thread too if that matters), but this operation relies on inpt from the GUI. To be more specific, if i click a certain menu item in my GUI class, it spawns a new thread, and in this thread i am performing certain operations that rely on a do/while loop. However, when i reach the end of each iteration (each pass through the loop) i want it to wait for the user to press the letter "C" on the keyboard while my JFrame currently has focus. I just need a way to determine if the user hit C while im still inside of this loop. Here is a pesuedo code example if my explination is not all that clear:

    Code:
    class GUI{
    if ButtonClicked
       Start new thread of otherCLass
    }
    
    class otherClass{
        run(){
            do{
                //Do a bunch of stuff
                Did the user hit C?
            }while(condition)
    }
    I can think of ways to do it, one using a global variable, but i don't like bad programing practices such as that. I would like to know the proper way to handle such a situation, and its kind of a tricky one to search for because i don't know the right wording for it. Any help would be much appreciated!

    Thanks,
    Ryan
    Java has 99 problems but a pointer ain't one

  7. #7
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    take a look at the observer / observable design pattern, it is described
    at http://jamesthornton.com/eckel/TIPat...l/TIPatt14.htm with
    what looks like some decent source code.

    I think it covers what you want to do.

    Cheers
    Graham
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

  8. #8
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    Thanks for the the reply. I only gave it a quick look so far but it does look like this might be the answer to my problem. sjalle, you're very knowledgable on the proper ways to do do stuff. Do you have anything to add, would you suggest the method Robinson perscribed?
    Java has 99 problems but a pointer ain't one

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Yes, it does. Observer, listener, user, all these nouns show up when I study
    your case.

    Gobals need not be so bad, if used with notification methods and a proper
    way of passing the variables. I system I designed recently had so many
    parameters and conditions (hundreds) that trying to solve it through a 'one
    size firs all' approach (e.g. interfaces and inheritance) was troublesome,
    and I had to start designing/coding.

    I ended up with a setup of basically one hashtable extension class that was
    instantiated as application static and a 'wire' interface for receiving information of updated properties in the 'tank'. The various classes in the
    system could then all access this static class through its set method and
    also add themselves to the 'tank' listener list.

    The invokation of the set method triggered a broadcast to all the listeners
    that had registered interest in the specific value that was set.

    Adding a listener to this info-tank could be done in two ways:

    1: addListener(TankListener theListener)
    2: addListenet(TankListener theListener, int informationSegmentID)

    The first would make the tank send notification of all value updates,
    the last method enabled filtering of what info-updates the listeners got.

    The benefit I got from that was that it was very 'generic' (not sure if I
    like that word), like when I made a new class and wanted to hook it into
    the flow of the system I just made it implement the TankLstener interface,
    implemented the callback method and added the instance to the static
    class' listeners.

    It sounds like more work than it actually is, and if you try it out I think you
    may want to synchronize the exchange.
    eschew obfuscation

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    NOTE: I tried to send you a PM but it said the message was too long and i didn't want to break it up.
    Well, here is a driver example of my Tank class.
    The source for the Tank system is attached.

    Code:
    /**
     * Driver program for the Tank data junction class.
     * In this example four classes 'communicate' data value
     * updates, insertions and removals.
     * Suitable for scenarios where several objects (components) have to be
     * notified of data changes, - without using constructor or
     * pointers and class methods parameters.
     */
    public class TankDriver {
      static Tank tank=new Tank(); // the one and only tank for the whole app.
      public TankDriver() {
      }
      public static void main(String[] args) {
        TankDriver tD = new TankDriver();
        ClassA classA=new ClassA();
        ClassB classB=new ClassB();
        ClassC classC=new ClassC();
    
        classC.doSomething();
        classA.doSomethingElse();
    
        AnyClass anyClass=new AnyClass();
      }
    }
    
    
    // this class gets all messages
    class ClassA implements TankListener {
      int age=55;
      public ClassA () {
        TankDriver.tank.addTankListener(this);
      }
      public void doSomethingElse () {
        System.out.println("ClassA working -------------------");
        TankDriver.tank.setValue("age",new Integer(age),this);
        TankDriver.tank.setValue("year",new Integer(2006),this);
      }
      public void valueChanged(TankEvent tE) {
        System.out.println("ClassA was notified:"+tE);
    
      }
    }
    
    // this class gets only messages of ID 25 (a number I made up...)
    class ClassB implements TankListener {
      public ClassB () {
        TankDriver.tank.addTankListener(this, 25);
      }
      public void valueChanged(TankEvent tE) {
        System.out.println("ClassB was notified:"+tE);
      }
    }
    
    // this class gets all messages
    class ClassC implements TankListener {
      public ClassC () {
        TankDriver.tank.addTankListener(this);
      }
      public void doSomething() {
        System.out.println("ClassC working -------------------");
        TankDriver.tank.setValue("aWord","hello",this,25);
        TankDriver.tank.setValue("year",new Integer(2005),this);
      }
      public void valueChanged(TankEvent tE) {
        System.out.println("ClassC was notified:"+tE);
      }
    }
    
    // this class gets no messages
    class AnyClass {
      public AnyClass() {
        System.out.println("AnyClass working");
        TankDriver.tank.setValue("year",new Integer(2007));
        TankDriver.tank.setValue("aWord","Summer",null,25);
        TankDriver.tank.setValue("age",new Integer(55),null);
        TankDriver.tank.removeValue("age", null);
      }
    }
    and here is the output:
    Code:
    ClassC working -------------------
    
    ClassB was notified:TankEvent [INSERT] for [aWord] value: hello
    
    ClassA was notified:TankEvent [INSERT] for [year] value: 2005
    
    ClassB was notified:TankEvent [INSERT] for [year] value: 2005
    
    ClassA working -------------------
    
    ClassB was notified:TankEvent [INSERT] for [age] value: 55
    
    ClassC was notified:TankEvent [INSERT] for [age] value: 55
    
    ClassB was notified:TankEvent [UPDATE] for [year] value: 2006 old value: 2005
    
    ClassC was notified:TankEvent [UPDATE] for [year] value: 2006 old value: 2005
    
    AnyClass working
    
    ClassA was notified:TankEvent [UPDATE] for [year] value: 2007 old value: 2006
    
    ClassB was notified:TankEvent [UPDATE] for [year] value: 2007 old value: 2006
    
    ClassC was notified:TankEvent [UPDATE] for [year] value: 2007 old value: 2006
    
    ClassB was notified:TankEvent [UPDATE] for [aWord] value: Summer old value: hello
    
    ClassA was notified:TankEvent [REMOVE] for [age] old value: 55
    
    ClassB was notified:TankEvent [REMOVE] for [age] old value: 55
    
    ClassC was notified:TankEvent [REMOVE] for [age] old value: 55
    Attached Files Attached Files
    eschew obfuscation

  11. #11
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    Well, to follow up i FINALLY came around to solving my problem. I ended up using the Observer interface and the Oberverable class to do my dirty work for me. Sjalle, while im positive your method would have worked it seemed too complicated for the little bit i needed to do, it seems your approach is more of a custom and "roll-your-own" type of approach and i didn't need all that power. Anyways it was as easy as making a nested class inside my GUI that extends the Observable class. Then overwriting the notifyObservers method passing it a KeyEvent as argument. And then implementing the Observer interface with my class that had its code executed in the thread. This class has to implement a method called update(Observable, Object). Inside this class i had a private KeyEvent, and everytime the update listener was notified i would change this data member to reflect the latest KeyEvent on the GUI. It works perfect and i couldn't be happier, thanks guys!
    Java has 99 problems but a pointer ain't one

  12. #12
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174

    Smile

    No problem - happy to help.
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center