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: Need help with GUI

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with GUI

    need help with this GUI. Can't get the calculate button to actually calculate. When I press it nothing happens. Only button that works is the exit button.






    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import java.awt.event.*;
    import java.awt.*;
    import java.text.NumberFormat;
    import javax.swing.text.JTextComponent;
    import murach.business.InvoiceCalculations;
    import murach.forms.SwingValidator;
    import java.awt.event.ActionEvent;
     
    public class InvoiceApp extends javax.swing.JFrame {
        private SwingValidator sv;
     
        /**
         * Creates new form InvoiceApp
         */
        public InvoiceApp() {
            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() {
     
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jLabel5 = new javax.swing.JLabel();
            calculateButton = new javax.swing.JButton();
            exitButton = new javax.swing.JButton();
            DiscountPercentTextField1 = new javax.swing.JTextField();
            DiscountjTextField2 = new javax.swing.JTextField();
            InvoiceTotaljTextField3 = new javax.swing.JTextField();
            jLabel6 = new javax.swing.JLabel();
            customerTypejTextField1 = new javax.swing.JTextField();
            subTotaljTextField1 = new javax.swing.JTextField();
     
            jLabel1.setText("jLabel1");
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jLabel2.setText("Subtotal:");
            jLabel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
     
            jLabel3.setText("Discount Percent:");
     
            jLabel4.setText("Discount:");
     
            jLabel5.setText("Invoice Total:");
     
            calculateButton.setText("Calculate");
            calculateButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    calculateButtonActionPerformed(evt);
                }
            });
     
            exitButton.setText("Exit");
            exitButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    exitButtonActionPerformed(evt);
                }
            });
     
            DiscountPercentTextField1.setEditable(false);
     
            DiscountjTextField2.setEditable(false);
     
            InvoiceTotaljTextField3.setEditable(false);
     
            jLabel6.setText("Enter Customer type:");
     
            customerTypejTextField1.setText("\n");
            customerTypejTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyTyped(java.awt.event.KeyEvent evt) {
                    customerTypejTextField1KeyTyped(evt);
                }
            });
     
            subTotaljTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    subTotaljTextField1ActionPerformed(evt);
                }
            });
            subTotaljTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyTyped(java.awt.event.KeyEvent evt) {
                    subTotaljTextField1KeyTyped(evt);
                }
            });
     
            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(168, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(DiscountjTextField2)
                                .addComponent(InvoiceTotaljTextField3)
                                .addComponent(customerTypejTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                                .addComponent(DiscountPercentTextField1)
                                .addComponent(subTotaljTextField1))
                            .addGap(20, 20, 20))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(calculateButton)
                            .addGap(37, 37, 37)
                            .addComponent(exitButton)))
                    .addGap(46, 46, 46))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(40, 40, 40)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(customerTypejTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(subTotaljTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(DiscountPercentTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(DiscountjTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(InvoiceTotaljTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(calculateButton)
                        .addComponent(exitButton))
                    .addGap(27, 27, 27))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            System.exit(0);
        }                                          
     
        private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
             if (isValidData()){
     
     
     
     
                  InvoiceCalculations FinancialCalculations = new InvoiceCalculations ();
     
            double d = Double.parseDouble(subTotaljTextField1.getText());
            String customerType = (customerTypejTextField1.getText());
     
            double fv = InvoiceCalculations.calculateDiscountPct( customerType, d );
            NumberFormat currency = NumberFormat.getCurrencyInstance();
     
            InvoiceTotaljTextField3.setText(currency.format(fv));
            DiscountjTextField2.setText(currency.format(fv));
            DiscountPercentTextField1.setText(currency.format(fv));
     
     
     
     
     
     
     
        }
        }                                               
     
        private void subTotaljTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                    
            // TODO add your handling code here:
        }                                                   
     
        private void subTotaljTextField1KeyTyped(java.awt.event.KeyEvent evt) {                                             
            // TODO add your handling code here:
        }                                            
     
        private void customerTypejTextField1KeyTyped(java.awt.event.KeyEvent evt) {                                                 
     
        }                                                
     
        /**
         * @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(InvoiceApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(InvoiceApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(InvoiceApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(InvoiceApp.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
     
                    InvoiceApp frame = new InvoiceApp();
     
     
                    frame.setResizable(false);
                    frame.setSize(400, 400);
     
                    frame.setVisible(true);
                    frame.setLocationRelativeTo(null);
                    frame.setTitle("Invoice Total Calculator");
     
     
     
     
     
     
                }
            });
        }
     
     
     
     
     
     
     
        // Variables declaration - do not modify                     
        private javax.swing.JTextField DiscountPercentTextField1;
        private javax.swing.JTextField DiscountjTextField2;
        private javax.swing.JTextField InvoiceTotaljTextField3;
        private javax.swing.JButton calculateButton;
        private javax.swing.JTextField customerTypejTextField1;
        private javax.swing.JButton exitButton;
        private javax.swing.JLabel jLabel1;
        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.JTextField subTotaljTextField1;
        // End of variables declaration                   
     
        private boolean isValidData() {
     
           return
            sv.isPresent(customerTypejTextField1, "C") &&
            sv.isPresent(subTotaljTextField1, "Subtotal")&&
            sv.isDouble(subTotaljTextField1, "Subtotal") &&
            sv.isInteger(subTotaljTextField1, "Subtotal");
     
     
     
     
     
        }
    }


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

    Default Re: Need help with GUI

    For me personally you would have to include the murach library files, to help you at least.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help with GUI

    Hi,
    My guess would be that isValidData is not returning true.
    private void calculateButtonActionPerformed(java.awt.event.Acti onEvent evt) {
    if (isValidData()){

    You should probably put some sort of a pop up dialog if the data isn't valid.

    Ilan

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with GUI

    Here's the swing class.



    package murach.forms;
     
    import javax.swing.*;
     
    public class SwingValidator
    {
        public boolean isPresent(JTextField c, String title)
        {
            if (c.getText().length() == 0)
            {
                showMessage(c, title + " is a required field.");
                c.requestFocusInWindow();
                return false;
            }
            return true;
        }
     
        public boolean isInteger(JTextField c, String title)
        {
            try
            {
                int i = Integer.parseInt(c.getText());
                return true;
            }
            catch (NumberFormatException e)
            {
                showMessage(c, title + " must be an integer.");
                c.requestFocusInWindow();
                return false;
            }
        }
     
        public boolean isDouble(JTextField c, String title)
        {
            try
            {
                double d = Double.parseDouble(c.getText());
                return true;
            }
            catch (NumberFormatException e)
            {
                showMessage(c, title + " must be a valid number.");
                c.requestFocusInWindow();
                return false;
            }
        }
     
        private void showMessage(JTextField c, String message)
        {
                JOptionPane.showMessageDialog(c, message, "Invalid Entry",
                    JOptionPane.ERROR_MESSAGE);
        }
    }


    --- Update ---

    Heres the invoice calculations class

    package murach.business;
     
    public class InvoiceCalculations
    {
        public static double calculateDiscountPct(String customerType, double subtotal)
        {
                double discountPercent = 0;
                if (customerType.equalsIgnoreCase("R"))
                {
                    if (subtotal < 100)
                        discountPercent = 0;
                    else if (subtotal >= 100 && subtotal < 250)
                        discountPercent = .1;
                    else if (subtotal >= 250)
                        discountPercent = .2;
                }
                else if (customerType.equalsIgnoreCase("C"))
                {
                    if (subtotal < 250)
                        discountPercent = .2;
                    else
                        discountPercent = .3;
                }
                else
                    discountPercent = .1;
     
                return discountPercent;
        }
    }


    --- Update ---

    Hey Ilan, Your right the isValid data isn't working. Once I removed that, It worked. I understand now also why the isvalid data wasn't working. I added a sv.isPresent methods without declaring a new swing validator within the method. Thanks so much Guys!

Similar Threads

  1. GUI HELP
    By Kshahzada in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2012, 08:05 PM
  2. Please help me with my GUI!
    By decgaid06 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 16th, 2011, 06:49 AM
  3. GUI
    By Simohammed in forum AWT / Java Swing
    Replies: 0
    Last Post: March 25th, 2010, 06:34 PM
  4. Replies: 3
    Last Post: February 1st, 2010, 12:24 AM
  5. GUI help
    By ravjot28 in forum AWT / Java Swing
    Replies: 1
    Last Post: January 31st, 2010, 10:19 PM