Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    wld4ubabay is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default Help with Buttons

    Hey I am working on an assignment and I can't seem to make my next and previous buttons work. They are supposed to go forward and backwards and also when they get to the first or last item in the array they are supposed to go back to the beginning/end (respectively)

    Here is my code so far:
    Java Code:
    if (e.getActionCommand() == "Next")                
                    {               
                        itemnameField.setText(item[ArrayIndex].getItemname());
                        itemnumField.setText(String.valueOf(item[ArrayIndex].getItemnum()));
                        itemstockField.setText(String.valueOf(item[ArrayIndex].getItemstock()));
                        priceField.setText(String.valueOf(item[ArrayIndex].getPrice()));
                        ModelNameField.setText(String.valueOf(item[ArrayIndex].getModelName()));
                        TotalValueField.setText(String.valueOf(item[ArrayIndex].getTotalValue()));
                        computeRestockFeeField.setText(String.valueOf(item[ArrayIndex].getcomputeRestockFee()));
                        item[ArrayIndex].calculateAll(item[ArrayIndex].getPrice());
                        
    
                        ArrayIndex = ArrayIndex + 1;                   
    
                    }//end if
                
    
                if (e.getActionCommand() == "Previous")
                    {
                        itemnameField.setText(item[ArrayIndex].getItemname());
                        itemnumField.setText(String.valueOf(item[ArrayIndex].getItemnum()));
                        itemstockField.setText(String.valueOf(item[ArrayIndex].getItemstock()));
                        priceField.setText(String.valueOf(item[ArrayIndex].getPrice()));
                        ModelNameField.setText(String.valueOf(item[ArrayIndex].getModelName()));
                        TotalValueField.setText(String.valueOf(item[ArrayIndex].getTotalValue()));
                        computeRestockFeeField.setText(String.valueOf(item[ArrayIndex].getcomputeRestockFee()));
                        item[ArrayIndex].calculateAll(item[ArrayIndex].getPrice());
    
    
                        ArrayIndex = ArrayIndex - 1;
                    }//end if

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Never use == to compare Strings. Instead use the equals or equalsIgnoreCase methods.

    For example, I'd change this:
    Java Code:
    if (e.getActionCommand() == "Next")

    to this:
    Java Code:
    if (e.getActionCommand().equals("Next"))

    Why is this important? == checks to see if one String object is the same as another, which we really don't care about here while equals(...) checks to see if both Strings contain the same contents in their char array -- do they in fact hold the same letters/punctuation/etc -- which is what we care about.
    Last edited by curmudgeon; 05-16-2010 at 06:32 PM.

  3. #3
    wld4ubabay is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    OK well that's how my instructor wants us to write that part...So I'm keeping it. I figured out how to get it go go up and down, I am now having a hard time getting it to go back to the first array when it gets to the end and back to the end when it gets to the first now.
    Here is my new code:
    Java Code:
    if (e.getActionCommand() == "Next")
                    if (ArrayIndex < 3)ArrayIndex = ArrayIndex + 1;
                        {
                            itemnameField.setText(item[ArrayIndex].getItemname());
                            itemnumField.setText(String.valueOf(item[ArrayIndex].getItemnum()));
                            itemstockField.setText(String.valueOf(item[ArrayIndex].getItemstock()));
                            priceField.setText(String.valueOf(item[ArrayIndex].getPrice()));
                            ModelNameField.setText(String.valueOf(item[ArrayIndex].getModelName()));
                            TotalValueField.setText(String.valueOf(item[ArrayIndex].getTotalValue()));
                            computeRestockFeeField.setText(String.valueOf(item[ArrayIndex].getcomputeRestockFee()));
                        }//end if
                              
    
                if (e.getActionCommand() == "Previous")
                    if (ArrayIndex > 0)ArrayIndex = ArrayIndex - 1;
                        {
                            itemnameField.setText(item[ArrayIndex].getItemname());
                            itemnumField.setText(String.valueOf(item[ArrayIndex].getItemnum()));
                            itemstockField.setText(String.valueOf(item[ArrayIndex].getItemstock()));
                            priceField.setText(String.valueOf(item[ArrayIndex].getPrice()));
                            ModelNameField.setText(String.valueOf(item[ArrayIndex].getModelName()));
                            TotalValueField.setText(String.valueOf(item[ArrayIndex].getTotalValue()));
                            computeRestockFeeField.setText(String.valueOf(item[ArrayIndex].getcomputeRestockFee()));
                        }//end if

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by wld4ubabay View Post
    OK well that's how my instructor wants us to write that part
    Tell your instructor s/he is a moron.

    kind regards,

    Jos

  5. #5
    wld4ubabay is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Oh believe me I think she is way more than that but I need to make 100% on this and the next assignment to pass and graduate....I'm never even going to use Java again its just a required class for the degree.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    It looks like the {} are in the wrong place for both if groups.

    Re ==
    Many times with Strings it will work. BUT its a poor code. The equals() method checks that the contents of two objects are equal which is what you want. == tests that the two objects referred to are the same. That is do the two pointers point to the same object.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by wld4ubabay View Post
    Oh believe me I think she is way more than that but I need to make 100% on this and the next assignment to pass and graduate....I'm never even going to use Java again its just a required class for the degree.
    But you can't compare the contents of two Strings with the == operator; you have to use the String.equals( ... ) method for that.

    kind regards,

    Jos

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by wld4ubabay View Post
    OK well that's how my instructor wants us to write that part...So I'm keeping it.
    To join the chorus of the others, what you've posted above is just plain wrong, and I have to wonder if you're somehow misinterpreting what you instructor told you. Regardless, use the equals method as has been already been recommended. Period.

  9. #9
    wld4ubabay is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Regardless of if it is better or worse...the function that I want it to retain is working and I really do not want to mess with that especially because the examples our instructor gave us to code buttons uses the == not the equals code. I am failing the class bad enough I do not need to miss points for something so trivial.

    This is really not the issue at hand I need help making my next button go back to the first item in the array once it has gone all the way through to the end.

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by wld4ubabay View Post
    Regardless of if it is better or worse...the function that I want it to retain is working and I really do not want to mess with that especially because the examples our instructor gave us to code buttons uses the == not the equals code.
    I'm sorry, but I can't believe that the examples use == with Strings. Please post one such example to remove all doubt. This is not a matter of "better" or "worse" but rather of right vs. wrong. Using == for Strings is just plain wrong, and you shouldn't be debating this. Seriously.

    I am failing the class bad enough I do not need to miss points for something so trivial.
    but doing it the way you are doing is not trivial and will lose you points.


    This is really not the issue at hand I need help making my next button go back to the first item in the array once it has gone all the way through to the end.
    For this you need to use either the mod operator or an if block to change your ArrayIndex (this should be arrayIndex, by the way) back to 0 if it reaches the maximum.
    Last edited by curmudgeon; 05-16-2010 at 07:33 PM.

  11. #11
    wld4ubabay is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    OK Here is my instructors example on how to code our buttons:
    Java Code:
    class ButtonListener implements ActionListener
    
          {
    
                public void actionPerformed(ActionEvent e)
    
                {
    
                      // Parameters for the sort method are the girls array and an instance of my FirstNameComparator class
    
                      Arrays.sort(person, new FirstNameComparator()); // Sort the girls array
    
                      ageTotalField.setText(String.valueOf(person[ArrayIndex].getTotalAge()));  // display the sum of the girl's age
    
     
    
                      //add button functions
    
                      if (e.getActionCommand() == "First") // if First button is clicked
    
                      {                     
    
                          nameField.setText(person[ArrayIndex].getName()); // get the person's name and assign it the name text field.
    
                          ageField.setText(String.valueOf(person[ArrayIndex].getAge())); // get the persons age and assign it the age text field.
    
                          ageInTenYearsField.setText(String.valueOf(person[ArrayIndex].computeAgeInTenYears(person[ArrayIndex].getAge())));  // pass in the person's age to the method that returns the age in ten years
    
                          hairColorField.setText(String.valueOf(person[ArrayIndex].getHairColor()));    // get the persons hair color and assign it the hair color text field.             
    
                                 
    
                    }
    
     
    
                      if (e.getActionCommand() == "Next") // if Next button is clicked
    
                      {
    
                            // code here
    
                      }
    
     
    
                      if (e.getActionCommand() == "Previous") // if Previous button is clicked
    
                      {
    
                            // code here
    
                      }
    
     
    
                      if (e.getActionCommand() == "Last") // if Last button is clicked
    
                      {
    
                            // code here
    
                      }
    
     
    
                } // end actionPerformed
    
     
    
        
    
          } // end class ButonListerner

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Did the OP miss that the {} are in the wrong place in his last posted code?
    The indentation does make the code in the {} part of the if.

  13. #13
    wld4ubabay is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    BTW I haven't lost any points for coding using the == signs which is why I am not going to change it because I am afraid she will take off points for me changing the code. This has been an ongoing project that we have added to each week. Last week we only had to have a next button, this week we have to have First, Next Previous and Last...

  14. #14
    wld4ubabay is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Did the OP miss that the {} are in the wrong place in his last posted code?
    The indentation does make the code in the {} part of the if.
    Yes I am sorry. Where should my indentations be? I am using netbeans and it auto indents for me so I don't change it. Could the indentations being wrong be the reason it tells me I can't put an else statement in? I tried to put in an else statement to tell it else go back to array[0], and it told me 'else' without 'if'.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    What I was refering to:
    Java Code:
    if (e.getActionCommand() == "Next")
                    if (ArrayIndex < 3)ArrayIndex = ArrayIndex + 1;
                        {
    vs
    Java Code:
    if (e.getActionCommand() == "Next") {
                    if (ArrayIndex < 3)ArrayIndex = ArrayIndex + 1;

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Re ==
    Does the jre optimize the use of Strings so that == is usually/always true as used above?

    Can anyone come up with an example where == fails for Strings?

    Poor technique but working code.

  17. #17
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    To the OP: my apologies: your instructor looks to be giving you bad advice. Sorry for doubting.

    Quote Originally Posted by Norm View Post
    Re ==
    Does the jre optimize the use of Strings so that == is usually/always true as used above?
    Can anyone come up with an example where == fails for Strings?
    Poor technique but working code.
    We've all seen it happen when a String is created using the new operator (of course), and I've seen it happen quite a bit without new String(...) in these and other fora, where when I've tested the code, == doesn't work but equals(...) does, but I haven't saved any of the offending code to post though. :(


    Again regarding the roll-over, two ways to solve:

    // next block
    Java Code:
    ArrayIndex++; // or ArrayIndex = ArrayIndex + 1; as you have it
    
    // then use the mod operator, %.  This assumes that item is the array;
    ArrayIndex %= item.length;  // same as doing ArrayIndex = ArrayIndex % item.length;
    The other way to do the next block is to use an if condition: if the ArrayIndex is greater than or equal to the length of the array, set it to 0:

    // next block
    Java Code:
    ArrayIndex++; // or ArrayIndex = ArrayIndex + 1; as you have it
    
    if (ArrayIndex >= item.length) {
      ArrayIndex = 0;
    }


    for the previous block:
    Java Code:
    ArrayIndex--;  // or ArrayIndex = ArrayIndex - 1;
    
    // if ArrayIndex is less then 0, set it to the max
    if (ArrayIndex < 0) {
      ArrayIndex = item.length - 1;
    }

  18. #18
    wld4ubabay is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    This didn't work :( Thanks anyways everyone :D I enrolled in the class again for next block even though I will have to pay for it out of pocket, now that I know how to do everything up to this point I can be making a good enough grade at this point next block to not need to pass this part of the project to still pass the class.

    Quote Originally Posted by curmudgeon View Post
    Again regarding the roll-over, two ways to solve:

    // next block
    Java Code:
    ArrayIndex++; // or ArrayIndex = ArrayIndex + 1; as you have it
    
    // then use the mod operator, %.  This assumes that item is the array;
    ArrayIndex %= item.length;  // same as doing ArrayIndex = ArrayIndex % item.length;
    The other way to do the next block is to use an if condition: if the ArrayIndex is greater than or equal to the length of the array, set it to 0:

    // next block
    Java Code:
    ArrayIndex++; // or ArrayIndex = ArrayIndex + 1; as you have it
    
    if (ArrayIndex >= item.length) {
      ArrayIndex = 0;
    }


    for the previous block:
    Java Code:
    ArrayIndex--;  // or ArrayIndex = ArrayIndex - 1;
    
    // if ArrayIndex is less then 0, set it to the max
    if (ArrayIndex < 0) {
      ArrayIndex = item.length - 1;
    }

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Thanks to curmudgeon
    Here's a simple pgm where == and equals() return different results:
    Java Code:
    // Test == vs equals() for String
    
    public class TestStringEquals {
    
       public static void main(String[] args) {
          String s1 = "XXXX";
          String s2 = new String("XXXX");
          System.out.println("== test gives: " + (s1 == s2) + ", equals() gives: " + s1.equals(s2));  }
    }
    
    /*  Output:
    Running: "C:\Program Files\Java\j2re1.4.2_08\bin\java.exe" -cp D:\JavaDevelopment;. TestStringEquals
    
    == test gives: false, equals() gives: true
    
    0 error(s)
    */

  20. #20
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Using the == operator to compare Strings will also fail if one or both of the Strings is read from input: from a file / for System.in / from getText on a GUI component.

    That said, Strings which are declared in the source code as literals are interned in the String pool, resulting in the success of an == comparison. Such String comparison using == can be seen in the JDK code, particularly in some of the Swing classes (can't remember which) and dates from an era when JDK developers were trying to squeeze every last bit of performance from the code to improve response in the days of JVMs that were much less optimized than they are today, running on computers that had less computing power than today's mid-range cellphones.

    IMO the only safe way to use == to compare Strings is when the String references in question have been assigned from a reference to the same constant. Example (not compiled, may have typos):
    Java Code:
    public class StringEqualTest {
      
      static final String TEST = "test";
      String test1;
      
      public static void main(String[] args) {
        StringEqualTest stringEqualTest = new StringEqualTest()
        stringEqualTest.test1 = TEST;
        stringEqualTest.showTest(TEST)
      }
      
      private void showTest(String test2) {
        System.out.println("test1 == test2 ? " + (test1 == test2));
      }
    }
    db
    Last edited by DarrylBurke; 05-17-2010 at 09:08 AM. Reason: added parentheses

Page 1 of 2 12 LastLast

Similar Threads

  1. Next and Previous Buttons
    By kbgriggs in forum New To Java
    Replies: 2
    Last Post: 08-12-2009, 03:08 PM
  2. Where To Get Pop Up Buttons
    By hitmen in forum AWT / Swing
    Replies: 7
    Last Post: 03-26-2009, 05:05 AM
  3. Applet buttons
    By h3ckf1r3 in forum Java Applets
    Replies: 6
    Last Post: 09-22-2008, 10:15 PM
  4. How to use SWT Buttons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:44 PM
  5. Next and Previous Buttons
    By JavaNewb in forum New To Java
    Replies: 1
    Last Post: 05-09-2008, 02:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •