Results 1 to 3 of 3
  1. #1
    mstratmann is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default jFrame to jPanel using user input and if statements

    I want to use a if statement to check input into a text field and then lauch one of two jPanels from the main jFrame. Here is the code on the jFrame and one of the two jPanels.

    Main jFrame
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package payroll;
    
    /**
     *
     * @author SUSER2
     */
    public class pay extends javax.swing.JFrame {
    
        /**
         * Creates new form pay
         */
        public pay() {
            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() {
    
            name = new javax.swing.JLabel();
            hours = new javax.swing.JLabel();
            wage = new javax.swing.JLabel();
            calculate = new javax.swing.JButton();
            clear = new javax.swing.JButton();
            nameInput = new javax.swing.JTextField();
            hourInput = new javax.swing.JTextField();
            wageInput = new javax.swing.JTextField();
            resultTextField = new javax.swing.JTextField();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            name.setText("Name: ");
    
            hours.setText("How many hours did you work: ");
    
            wage.setText("What is your hourly wage: ");
    
            calculate.setText("Calculate");
            calculate.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    calculateActionPerformed(evt);
                }
            });
    
            clear.setText("Clear");
            clear.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    clearActionPerformed(evt);
                }
            });
    
            resultTextField.setEditable(false);
    
            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()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(name)
                            .addGap(138, 138, 138)
                            .addComponent(nameInput, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(hours)
                                .addComponent(wage))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(resultTextField)
                                .addComponent(hourInput)
                                .addComponent(wageInput))))
                    .addContainerGap(88, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addComponent(calculate)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(clear)
                    .addGap(28, 28, 28))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(50, 50, 50)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(name)
                        .addComponent(nameInput, 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(hours)
                        .addComponent(hourInput, 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(wage)
                        .addComponent(wageInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(40, 40, 40)
                    .addComponent(resultTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(calculate)
                        .addComponent(clear))
                    .addGap(31, 31, 31))
            );
    
            pack();
        }// </editor-fold>                        
    
        private void clearActionPerformed(java.awt.event.ActionEvent evt) {                                      
            nameInput.setText("");
            wageInput.setText("");
            hourInput.setText("");
            resultTextField.setText("");
        }                                     
    
        private void calculateActionPerformed(java.awt.event.ActionEvent evt) {                                          
    
            double othours, totalpay, pay, otwage, otpay;
    
            String num1Str = hourInput.getText();
            String num2Str = wageInput.getText();
            String totalpayStr;
    
            double num1 = Double.parseDouble(num1Str);
            double num2 = Double.parseDouble(num2Str);
    
            if (num1 <= 40) {
                totalpay = num1 * num2;
    
            } else {
                pay = 40 * num2;
                othours = num1 - 40;
    
                otwage = num2 * 1.5;
                otpay = otwage * othours;
                totalpay = otpay + pay;
            }
    
            double result = totalpay; //variable total pay might not have been initialized
            String resultStr = Double.toString(result);
    
            resultTextField.setText(resultStr);
        }                                         
    
        /**
         * @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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            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(pay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(pay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(pay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(pay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new pay().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JButton calculate;
        private javax.swing.JButton clear;
        private javax.swing.JTextField hourInput;
        private javax.swing.JLabel hours;
        private javax.swing.JLabel name;
        private javax.swing.JTextField nameInput;
        private javax.swing.JTextField resultTextField;
        private javax.swing.JLabel wage;
        private javax.swing.JTextField wageInput;
        // End of variables declaration                   
    }
    jPanel 1
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package payroll;
    
    /**
     *
     * @author Mike
     */
    public class mikePayGUI extends javax.swing.JPanel {
    
        /**
         * Creates new form mikePayGUI
         */
        public mikePayGUI() {
            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() {
    
            payTypeLabel = new javax.swing.JLabel();
            hoursLabel = new javax.swing.JLabel();
            commisionLabel = new javax.swing.JLabel();
            hoursTextField = new javax.swing.JTextField();
            commisionTextField = new javax.swing.JTextField();
            calculateButton = new javax.swing.JButton();
            clearButton = new javax.swing.JButton();
            weeklyRadioButton = new javax.swing.JRadioButton();
            commisionRadioButton = new javax.swing.JRadioButton();
            wageLabel = new javax.swing.JLabel();
            wageTextField = new javax.swing.JTextField();
            totalPayTextField = new javax.swing.JTextField();
    
            payTypeLabel.setText("What check is this? ");
    
            hoursLabel.setText("How many hours? ");
    
            commisionLabel.setText("Commision based off of?");
    
            calculateButton.setText("Calculate");
            calculateButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    calculateButtonActionPerformed(evt);
                }
            });
    
            clearButton.setText("Clear");
    
            weeklyRadioButton.setText("Weekly");
    
            commisionRadioButton.setText("Commision");
    
            wageLabel.setText("What is your hourly wage? ");
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(calculateButton)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(payTypeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(hoursLabel)
                                    .addComponent(commisionLabel)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(24, 24, 24)
                                    .addComponent(weeklyRadioButton)
                                    .addGap(18, 18, 18)
                                    .addComponent(commisionRadioButton)
                                    .addContainerGap(10, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(wageTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                                        .addComponent(clearButton)
                                        .addComponent(hoursTextField)
                                        .addComponent(commisionTextField))
                                    .addGap(59, 59, 59))))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(wageLabel)
                            .addGap(0, 0, Short.MAX_VALUE))))
                .addGroup(layout.createSequentialGroup()
                    .addGap(98, 98, 98)
                    .addComponent(totalPayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(46, 46, 46)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(payTypeLabel)
                        .addComponent(weeklyRadioButton)
                        .addComponent(commisionRadioButton))
                    .addGap(37, 37, 37)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(hoursLabel)
                        .addComponent(hoursTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(2, 2, 2)
                            .addComponent(wageLabel))
                        .addGroup(layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(wageTextField, 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(commisionLabel)
                        .addComponent(commisionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(totalPayTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(calculateButton)
                        .addComponent(clearButton))
                    .addGap(44, 44, 44))
            );
        }// </editor-fold>                        
    
        private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
            
            double totalpay;
    
            String num1Str = hoursTextField.getText();
            String num2Str = wageTextField.getText();
            String totalpayStr;
    
            double num1 = Double.parseDouble(num1Str);
            double num2 = Double.parseDouble(num2Str);
    
            totalpay = num1 * num2;
    
            double result = totalpay; //variable total pay might not have been initialized
            String resultStr = Double.toString(result);
    
            totalPayTextField.setText(resultStr);                             
        }                                               
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton calculateButton;
        private javax.swing.JButton clearButton;
        private javax.swing.JLabel commisionLabel;
        private javax.swing.JRadioButton commisionRadioButton;
        private javax.swing.JTextField commisionTextField;
        private javax.swing.JLabel hoursLabel;
        private javax.swing.JTextField hoursTextField;
        private javax.swing.JLabel payTypeLabel;
        private javax.swing.JTextField totalPayTextField;
        private javax.swing.JLabel wageLabel;
        private javax.swing.JTextField wageTextField;
        private javax.swing.JRadioButton weeklyRadioButton;
        // End of variables declaration                   
    }

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

    Default Re: jFrame to jPanel using user input and if statements

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: jFrame to jPanel using user input and if statements

    Wow, that is too much code to read at 4:00 after trying to debug my own code...
    Did you use the netbeans Swing maker?

    You could say this:
    Java Code:
    MikePayGUI mikepaygui = new MikePayGUI();
    OtherPanelGUI otherpanelgui = new OtherPanelGUI();  //?? Where is this one??
    
    If(TextInput.getText().equals("-whateverYouAreCheckingFor-")){
         MikePayGUI.setVisible(true);  //This should launch it in a new window
    }
    else{
         OtherPanelGUI.setVisible(true);
    }

Similar Threads

  1. Replies: 3
    Last Post: 04-09-2012, 11:12 AM
  2. how to get user input in JFrame
    By ashraf in forum JCreator
    Replies: 2
    Last Post: 09-27-2011, 04:22 PM
  3. Replies: 2
    Last Post: 02-22-2011, 03:07 AM
  4. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  5. If statements, input, and strings
    By evolvepwnz in forum New To Java
    Replies: 16
    Last Post: 10-22-2009, 09:58 PM

Posting Permissions

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