Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Need looping help for my GUI.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need looping help for my GUI.

    This is my code:
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import javax.swing.*;
     
     
    public class Stapler extends Ink{
     
     
            String[] name = {"Stapler", "Pens", "Paper","Staples"};
    		int[] numberItem = {123456, 654321, 987654,963147};
    		int[] inStock = {123, 321, 987,721};
    		double[] price = {4.75, 2.50, 1.37,1.36};
                   double[] tut = {inStock[0] * price[0] * restockFee + inStock[0] * price[0], inStock[1] * price[1] * restockFee + inStock[1] * price[1], 
                       inStock[2] * price[2] * restockFee + inStock[2] * price[2],inStock[3] * price[3] * restockFee + inStock[3] * price[3] };
                   double every = (price[2]*inStock[2]+price[1]*inStock[1]+price[0]*inStock[0] * restockFee + 
    			price[2]*inStock[2]+price[1]*inStock[1]+price[0]*inStock[0] * stockfee + inStock[3] * price[3] * restockFee + inStock[3] * price[3] );
     
    private Stapler bob;
     
     
    	public String getnaM(){
    		return(sub);
    		 }       
     
    		public int getiN(){
    			return(numb);
    		 }         
     
     
    			public double geteach(){
    				return(pricE);
    		 }    
     
    public int getstocks(){
    	return(iNs);
    		 }
     
    	public double getaL(){
    		return(vaL);
    		 }    
     
    		public double getFee(){
    			return(stockfee);}
     
     
    			public String[] getnames(){
    			return(name);
    			}
    				public int[] getItemnumber(){
    				return(numberItem);
    				}
    					public double[] getprices(){
    					return(price);
    					}
    						public int[] getunits(){
    						return(inStock);
    						}	
     
    							public double[] getVolume() {
    							return (tut);
    							}
     
    			public double getEverything(){
    			return(every);}
     
    	Stapler(){
     
     
    		}
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
         	final Stapler stapler;
     
    			stapler = new Stapler();
     
    			final DecimalFormat f = new DecimalFormat("$0.00");
     
     
     
      final int[] be = {0,1,2,3};
     
     
    JPanel p = new JPanel();
     
    JFrame myFrame = new JFrame("Inventory Program");  
     
    		myFrame.setSize(295, 175);// width, height
     
     myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  
     
     myFrame.setVisible(true); 
     
     
     
     
     
     
     final JTextArea out = new JTextArea();
     out.setEditable(false);
     
     JButton btnFirst = new JButton("First");
     btnFirst.setActionCommand("First");
     
      JButton btnLast = new JButton("last");
     btnLast.setActionCommand("last");
     
      JButton btnNext = new JButton("Next");
     btnNext.setActionCommand("Next");
     
       JButton btnPrevious = new JButton("Prev");
     btnPrevious.setActionCommand("Prev");
     
     
    btnFirst.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
     
    out.setText("");
    out.append("Product name: "+stapler.getnames()[0]);
    out.append("\nProduct number: "+stapler.getItemnumber()[0]);
    out.append("\nPrice of one product: "+f.format(+stapler.getprices()[0]));
    out.append("\nNumber of units in stock: "+stapler.getunits()[0]);
    out.append("\nTotal value of product in stock: "+f.format(+stapler.getVolume()[0]));
     
                }
            });
     
     
     
     
     
        btnPrevious.addActionListener(new ActionListener() {
                private int b;
     
                @Override
                public void actionPerformed(ActionEvent e) {
            if(b>0)       {
                --b;
    out.setText("");
    out.append("Product name: "+stapler.getnames()[b]);
    out.append("\nProduct number: "+stapler.getItemnumber()[b]);
    out.append("\nPrice of one product: "+f.format(+stapler.getprices()[b]));
    out.append("\nNumber of units in stock: "+stapler.getunits()[b]);
    out.append("\nTotal value of product in stock: "+f.format(+stapler.getVolume()[b]));
            }else{out.setText("");
    out.append("Product name: "+stapler.getnames()[2]);
    out.append("\nProduct number: "+stapler.getItemnumber()[2]);
    out.append("\nPrice of one product: "+f.format(+stapler.getprices()[2]));
    out.append("\nNumber of units in stock: "+stapler.getunits()[2]);
    out.append("\nTotal value of product in stock: "+f.format(+stapler.getVolume()[2]));}
                }
            });  
          btnNext.addActionListener(new ActionListener() {
                private int i;
     
     
                @Override
     
                public void actionPerformed(ActionEvent e) {
           if(i < be.length -1)    {   
               i++; 
    out.setText("");
    out.append("Product name: "+stapler.getnames()[i]);
    out.append("\nProduct number: "+stapler.getItemnumber()[i]);
    out.append("\nPrice of one product: "+f.format(+stapler.getprices()[i]));
    out.append("\nNumber of units in stock: "+stapler.getunits()[i]);
    out.append("\nTotal value of product in stock: "+f.format(+stapler.getVolume()[i]));
               }else{
       out.setText("");
    out.append("Product name: "+stapler.getnames()[0]);
    out.append("\nProduct number: "+stapler.getItemnumber()[0]);
    out.append("\nPrice of one product: "+f.format(+stapler.getprices()[0]));
    out.append("\nNumber of units in stock: "+stapler.getunits()[0]);
    out.append("\nTotal value of product in stock: "+f.format(+stapler.getVolume()[0]));    
    }
              }
            });
     
     
        btnLast.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
     
    out.setText("");
    out.append("Product name: "+stapler.getnames()[2]);
    out.append("\nProduct number: "+stapler.getItemnumber()[2]);
    out.append("\nPrice of one product: "+f.format(+stapler.getprices()[2]));
    out.append("\nNumber of units in stock: "+stapler.getunits()[2]);
    out.append("\nTotal value of product in stock: "+f.format(+stapler.getVolume()[2]));
     
                }
            });
     p.add(out);
     myFrame.add(p);
     p.add(btnFirst);
     p.add(btnPrevious);
     p.add(btnLast);
     p.add(btnNext);
     
     
     
     btnFirst.setLayout(new BorderLayout());
     p.add(btnFirst, BorderLayout.SOUTH);
     
     btnPrevious.setLayout(new BorderLayout());
     p.add(btnPrevious, BorderLayout.SOUTH);
     
      btnNext.setLayout(new BorderLayout());
     p.add(btnNext, BorderLayout.SOUTH);
     
     btnLast.setLayout(new BorderLayout());
     p.add(btnLast, BorderLayout.SOUTH);
    }
     
      }

    My problem is i cannot get my next button to continure to loop around. After the else statment is true it is stuck on the else until the program is restarted. Also i need help with my previous button logic. The previous button goes straight to the last array of information. How do i get my next button to work compltly and how do i fix my previous button to work. Thank you.

    ink:

    public class Ink {
     
    String sub = ("Staples"); 
    	int numb = (963147);
    		double pricE = (1.36);
    int iNs = (721);
    	double restockFee = (.05);
    		double vaL = (iNs * pricE);
    double stockfee = (vaL * restockFee + vaL);
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    it is stuck on the else until the program is restarted
    Can you change the values of the variables used in the if statement so the if will be true next time or when you want it to be true?

    The previous button goes straight to the last array of information
    Is that wrong?
    What is it supposed to do?


    The posted code does NOT compile without errors, missing class: Ink

    Also the code is very poorly formatted. The indentations of nested statements is not correct.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    sorry about the bad formatting im still learning that. Also i know it doesnt compile i have class ink. If you are trying to get it to run i can post that code to. And the previous button should be exactly like the next button. It should go backwords from whhatever information you are seeing all the way without stoping and the next button should do the same but go forwards.
    ps i posted the ink file.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    Try debugging the code by adding some println statements to print out the values of variables that control what is displayed when the buttons are pressed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    I dont know how to do that. i know how to add a println statment but thats it. How do i make one tell me whats wrong?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    How do i make one tell me whats wrong?
    It can take lots more than one println.
    Using println statements will show you what the computer see when it executes. If you know what the code is supposed to do and how it should change the values of the variables as it executes, you should be able to recognize when the code is not doing what you expected.

    Can you describe what you do when you execute the code?
    Describe what it does when it doesn't respond the way you want.
    When I press the buttons, the display changes. Then what?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    O ok. so the println should be inside the if statment to see if its doing what i need it to? if i get the println statment to work correclty i can just implement that into the gui code? That seems reletivly easy but will this always work?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    I use calls to the println() method all the time to debug code and see what it is doing.

    Putting println() calls inside of an if statement means it will only print if the statement is true. Some times you want to see what the values of the variables are that are used in the if which means the println must be before the if.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    can i get a short example to fully understand what you mean. I do understand but having trouble seeing how to implement this into my own code to use println as a debugger.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    use println as a debugger.
    To see what the value of b is print it:
     System.out.println("b="+b);
     if(b>0)
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    Thank you. veriable b is not changing value and i dont know why. It is equal to my array[0]. I guess my logic is wrong. My next button seems to stop at my last array but how do i get an infinite loop to go around my arrays using the next button.
    Also would that logic be similar with the previous button just opposite?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    veriable b is not changing value
    Look at the code and see why. What will make it change value?

    how do i get an infinite loop to go around my arrays using the next button.
    What variable controls what is shown when the next button is pressed? How does the value of that variable change? What values would that variable need to have "loop around"?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    Ok to do all of that i need to figure out how to get the code to know what loops its at right? ok lets get some pseudocode:
    if currentArray[] +1
    output
    else at end of array start at 0.

    so how do i get the code to relize what array its on?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    how do i get the code to relize what array its on?
    How many arrays does the code have to chose from?
    I'm not sure I understand what you are asking. The programmer is the one that determines what array the code will use.
    Are you talking about the value of the index to the array? The code can look at the index's current value to see what element in the array it is working with.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    their are 4 array's. The reason i ask is when i use the next button it doesnt matter which array im on it goes from the second integer to the first one then stops. That is hwy im asking.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    then stops
    Do you mean the code doesn't change some variable's value?
    What variable is not changing?
    What values should the variable change to?

    Copy the contents of the console that shows the values the variable has as the code is executed.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    This is the code from beggining to end were it stops. After the last output it repeats that same output. What it should do after it reaches that last variable is go to the first one and repeat the code over and over until i shut the program down.
    Product name: Pens
     
    Product number: 654321
     
    Price of one product: $2.50
     
    Number of units in stock: 321
     
    Total value of product in stock: $842.62
    Product name: Paper
     
    Product number: 987654
     
    Price of one product: $1.37
     
    Number of units in stock: 987
     
    Total value of product in stock: $1419.80
    Product name: Staples
     
    Product number: 963147
     
    Price of one product: $1.36
     
    Number of units in stock: 721
     
    Total value of product in stock: $1029.59

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    Is what you posted printed out with println statements to the console?
    What prints out from the println statements that you added for debugging?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    I fixed my next button with adding a i=0 to restart form the beggining when the else is activated.
    but now i cannot get my previous button else to activate when it reaches 0 it throws an error saying willnot go -1. How do i get the else to trigger when going in revers.

    When the next reaches the end of the array the else is triggered but when the previous button reaches the beginning it does not trigger the else statment.
    and yes my println statments outcome for next is correct but not for the previous button now.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    get my previous button else to activate when it reaches 0
    Consider how the values of the index changes when using the previous button.
    For example: If the index starts at 2, the next value will be 1 and the next 0 and the next will be to end of the array.
    The code needs to check the value of the index and when going back from the value of 0 it must go to the index for the last element in the array.


    What do the println statements show for the value of the index used by the previous button?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    it shows 3,2,1,0 so its going thru all of them but then it throws an error of -1. like this:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    The code needs to test when the value of the index goes below 0 and change the index's value to wrap around to the last element in the list.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    Thats what i tried to do with the else statment but the else will not trigger for that to happen.
            btnPrevious.addActionListener(new ActionListener() {
                private int b = 4;
     
                @Override
                public void actionPerformed(ActionEvent e) {
     
     
                    if (b < be.length  +1) {
                        b--;
                        out.setText("");
                        out.append("Product name: " + stapler.getnames()[b]);
                        out.append("\nProduct number: " + stapler.getItemnumber()[b]);
                        out.append("\nPrice of one product: " + f.format(+stapler.getprices()[b]));
                        out.append("\nNumber of units in stock: " + stapler.getunits()[b]);
                        out.append("\nTotal value of product in stock: " + f.format(+stapler.getVolume()[b]));
                        System.out.println("Test number is " + stapler.getbe()[b]);
     
                    } else {
                        b = 4;
     
     
                        out.setText("");
                        out.append("Product name: " + stapler.getnames()[b]);
                        out.append("\nProduct number: " + stapler.getItemnumber()[b]);
                        out.append("\nPrice of one product: " + f.format(+stapler.getprices()[b]));
                        out.append("\nNumber of units in stock: " + stapler.getunits()[b]);
                        out.append("\nTotal value of product in stock: " + f.format(+stapler.getVolume()[b]));
                        System.out.println("Else triggered ");
                    }
                }
            });
    This is the section of code. The else statment is never reached is their a mistake in my code. If not how can i tell the if statment to go to else when 0 is reached?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need looping help for my GUI.

    Try doing it another way:
    First: Compute the index to use
    Second: Use the index to get the data for the textarea

    There is no need to have two sections of code that are identical: the calls to setText() and append() look the same to me. If the index is correctly computed, you only need one set of those statements.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need looping help for my GUI.

    I understand that but that is what im having trouble with. I cannot find the correct logic for the loop i know im close but cannot find the one i need. The logic i have right now is working only halfway. The only thing i need it to do now is go back to the 3rd array when it reaches array 0. If i cna do that i will have no problems. But i have no clue on how to do so and have tried everything i can think of.

Page 1 of 2 12 LastLast

Similar Threads

  1. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 05:11 AM
  2. Need help with looping!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 04:09 PM
  3. Help in looping
    By endframe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 28th, 2010, 03:24 PM
  4. Replies: 3
    Last Post: February 1st, 2010, 12:24 AM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM