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.

Results 1 to 4 of 4

Thread: Help with Jbutton code

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help with Jbutton code

    What I am trying to do, is have the receipt cycle to the next item each time the next button is clicked. The program works fine right now except that it will not cycle. here is the code.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package MarketFormGUI;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.NumberFormat;
    import java.util.Locale;
    import java.util.Arrays;
    /**
    /**
     *
     * @author David
     */
    public class MarketGUIForm extends javax.swing.JFrame 
    {
        //to format variables in class
     NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
     
     // variable to test next i with button
     
         private static  int i = 0;
        /**
         * Creates new form MarketGUIForm
         */
        public MarketGUIForm() {
            initComponents();
        }
         /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            jTextField1 = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jTextField2 = new javax.swing.JTextField();
            jTextField3 = new javax.swing.JTextField();
            jLabel5 = new javax.swing.JLabel();
            jLabel6 = new javax.swing.JLabel();
            jTextField4 = new javax.swing.JTextField();
            jLabel7 = new javax.swing.JLabel();
            jTextField5 = new javax.swing.JTextField();
            jButton1 = new javax.swing.JButton();
            jTextField6 = new javax.swing.JTextField();
            jLabel8 = new javax.swing.JLabel();
            jLabel9 = new javax.swing.JLabel();
            jTextField7 = new javax.swing.JTextField();
            jLabel10 = new javax.swing.JLabel();
            jTextField8 = new javax.swing.JTextField();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jTextField1.setText(String.valueOf(testProduce));
            jTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField1ActionPerformed(evt);
                }
            });
     
            jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
            jLabel1.setText("Produce Item:");
     
            jLabel2.setText("Thank You For Shopping at David's Farmers Market!!!");
     
            jLabel3.setText("All of our produce is Locally Grown.");
     
            jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
            jLabel4.setText("SKU:");
     
            jTextField2.setText(String.valueOf(testSKU));
            jTextField2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField2ActionPerformed(evt);
                }
            });
     
            jTextField3.setText(format.format(testCost));
     
            jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
            jLabel5.setText("Cost:");
     
            jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
            jLabel6.setText("Amount Ordered:");
     
            jTextField4.setText(String.valueOf(testAmount));
            jTextField4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextField4ActionPerformed(evt);
                }
            });
     
            jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
            jLabel7.setText("SubTotal:");
     
            jTextField5.setText(format.format(testSubTotal));
     
            jButton1.setText("Press to cycle through the items");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
     
            jTextField6.setText(String.valueOf(testTax));
     
            jLabel8.setText("Tax Rate:");
     
            jLabel9.setText("Tax Amount:");
     
            jTextField7.setText(format.format(testTaxAmount));
     
            jLabel10.setText("Grand Total:");
     
            jTextField8.setText(format.format(testGrandTotal));
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel3)))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(38, 38, 38)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(18, 18, 18)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jTextField2)
                                        .addComponent(jTextField1)
                                        .addComponent(jTextField3)
                                        .addComponent(jTextField5)
                                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel3)
                    .addGap(35, 35, 35)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel7)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel10))
                    .addGap(23, 23, 23))
            );
     
            jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel4, jLabel5, jLabel6, jLabel7});
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(27, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>
     
        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
     
        }
     
        private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
     
        private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
     
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     
     
     
     
     
          int ti = i;
          ti = ti++;
          i = ti;
        if (i > 5)  
        {
            i = 0;
        }//end if
     
     
     
        }
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(MarketGUIForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(MarketGUIForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(MarketGUIForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(MarketGUIForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
      //inserting main here      
               //test insertion
     
                      NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
     
           StorageClass[] mStore = new StorageClass[5];//create array of objects
           mStore[0] = new StorageClass ("Apples", 23456789, 12,12.00);
           mStore[1] = new StorageClass ("Bananas", 29384756, 12,10.00);
           mStore[2] = new StorageClass ("Lettuce", 12345678, 10,12.00);
           mStore[3] = new StorageClass ("Tomato", 87654321, 13, 10.00);
           mStore[4] = new StorageClass ("Zuchinni", 98765432, 14, 10.00);
     
       //Create a method to calculate inventory total
           double totalsales = calculateTotalSales(mStore);
     
     
     
     
           //to create access to new subclass constructor
           StorageClassImpl em = new StorageClassImpl("test",12345678,100,10.00);
     
     
      //display mStore data with a loop of the type for
     
           System.out.print("Thank you for shopping at "
                   +  em.getMarketName()  
                   +"\nAll our produce is locally grown\n\n");
           for (int i = 0;i<mStore.length;i++)
           {
               System.out.print("Produce type: "+mStore[i].getsName()
                       +"\nSKU: "+ mStore[i].getsBarcode()
                       +"\nCases Ordered: "+ mStore[i].getsAmount()
                       +"\nPrice per case: "+format.format(mStore[i].getsCost())
                       +"\nSubtotal: "
                  +format.format(mStore[i].getsAmount() * mStore[i].getsCost() )
                       +"\n\n");
     
           }
                 System.out.print("PreTax Order Total: "+format.format(totalsales));   
                 System.out.print("\nTax Rate: "+em.getTax()   
                    +"%\nTax Totsl: "+format.format(totalsales*em.getTax())
                    +"\nGrand Total: "
                    + format.format((totalsales*em.getTax())+ totalsales) +"\n\n");
     
     
            //declare vaiables to move info into class test
     
            String tProduce = mStore[i].getsName();
            MarketGUIForm.testProduce = tProduce;
     
            long tSKU = mStore[i].getsBarcode();
            MarketGUIForm.testSKU = tSKU;
     
            double tCost = mStore[i].getsCost();
            MarketGUIForm.testCost = tCost;
     
            double tAmount = mStore[i].getsAmount();
            MarketGUIForm.testAmount = tAmount;
     
            double tSubTotal = (mStore[i].getsAmount() * mStore[i].getsCost());
            MarketGUIForm.testSubTotal = tSubTotal;
     
            double TTax = em.getTax();
            MarketGUIForm.testTax = TTax;
     
            double tTaxAmount =(totalsales*em.getTax());
            MarketGUIForm.testTaxAmount = tTaxAmount;
     
            double tGrandTotal = (totalsales*em.getTax()+ totalsales);
            MarketGUIForm.testGrandTotal = tGrandTotal;
     
     
     
     
     
      //end of insertion      
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new MarketGUIForm().setVisible(true);
     
     
     
                }//end run body
            });//end java event
     
     
     
     
     
     
     
     
     
        }//end main
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel10;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JLabel jLabel8;
        private javax.swing.JLabel jLabel9;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        private javax.swing.JTextField jTextField2;
        private javax.swing.JTextField jTextField3;
        private javax.swing.JTextField jTextField4;
        private javax.swing.JTextField jTextField5;
        private javax.swing.JTextField jTextField6;
        private javax.swing.JTextField jTextField7;
        private javax.swing.JTextField jTextField8;
        // End of variables declaration
     
        //test for pasing info
       private static double testTax;
       private static String testProduce; 
       private static long testSKU;
       private static double testCost;
       private static double testAmount;
       private static double testSubTotal;
       private static double testTaxAmount;
       private static double testGrandTotal;
     
     
    //inserting methods needed from main
     //method to calculate total sales
        private static double calculateTotalSales(StorageClass[] mStore) {
            double total = 0; //for running total
     
            for(int i = 0;i< mStore.length; i++)
            {
                double cost = mStore[i].getsAmount()*mStore[i].getsCost();
                total += cost;//makes total equal cost
     
            }
          return total;  
        }//end calculate method
     
     
     
     
     
     
     
     
    //end of insert methods from main
     
    }//end class


    I thought about moving this section[code = java] String tProduce = mStore[i].getsName();
    MarketGUIForm.testProduce = tProduce;

    long tSKU = mStore[i].getsBarcode();
    MarketGUIForm.testSKU = tSKU;

    double tCost = mStore[i].getsCost();
    MarketGUIForm.testCost = tCost;

    double tAmount = mStore[i].getsAmount();
    MarketGUIForm.testAmount = tAmount;

    double tSubTotal = (mStore[i].getsAmount() * mStore[i].getsCost());
    MarketGUIForm.testSubTotal = tSubTotal; [/code] into the jbutton section
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     
     
     
     
     
          int ti = i;
          ti = ti++;
          i = ti;
        if (i > 5)  
        {
            i = 0;
        }//end if
     
     
     
        }
    with its own loop but I do not think the button is realizing it has been clicked. Can someone help?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Help with Jbutton code

    This is kind of unusual:

          int ti = i;
          ti = ti++;
          i = ti;

    Have you placed println's to see what this does to the i, ti variables? why not simply do, i++ and be done with it?

    Note that just changing i (once you've successfully changed it) will not automatically have no effect on your program. How are you testing to see if the button has been pressed? Where is it changing anything visualized in your GUI?

    Finally, please work on posting only well formatted code with nice uniform indentation (3 spaces works well). Yours is a bit all over the place making it vary hard for us to understand your code.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Jbutton code

    Well unfortunately the code is hardcoded by the Form builder in the Netbeans program. There is no way for me to change it. The portions I wrote are indented correctly.

    The problem is that once I press run the gui pops up with the text field populated by the first row of the array. I do not know if clicking the program would cycle to the next row of the array even if it worked properly because the array gets dumped at the end of the run and before someone clicks the button. Correct? if that is the case there would be no way to retrieve the values at i=x.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Help with Jbutton code

    What I meant by println's is if you changed your code above to this:

          int ti = i;
          System.out.println("ti: " + ti);
          ti = ti++;
          System.out.println("after ti = ti++, ti: " + ti);
          i = ti;
          System.out.println("i: " + i);
          if (i > 5) {
             i = 0;
          }

    You'll see 1) if pressing your JButton will cause the ActionListener code to be called (it does), and 2) whether your code above will result in i being incremented (it doesn't). If you ran this code and pressed the button, you'd see a result like this:

    ti: 0
    after ti = ti++, ti: 0
    i: 0

    ti: 0
    after ti = ti++, ti: 0
    i: 0

    ti: 0
    after ti = ti++, ti: 0
    i: 0

    ti: 0
    after ti = ti++, ti: 0
    i: 0

    So why is i not being incremented? It's because this: foo = foo++; does nothing. Instead just do foo++ and that's it.

    But regardless, changing i will not change the values held in the JTextFields, because *you* have to write code that uses the value held in i to change these displayed values.

Similar Threads

  1. JButton
    By usherlad in forum AWT / Java Swing
    Replies: 1
    Last Post: August 10th, 2012, 12:02 PM
  2. Help with jButton
    By ls7897 in forum AWT / Java Swing
    Replies: 6
    Last Post: March 13th, 2011, 03:22 PM
  3. JButton help
    By tabutcher in forum Java Applets
    Replies: 4
    Last Post: March 9th, 2010, 06:04 PM
  4. JButton Help
    By ravjot28 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 17th, 2010, 10:38 AM
  5. JButton...
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 27th, 2009, 10:39 AM

Tags for this Thread