Skip to main content

Help with calling jPanel from a jFrame

3 replies [Last post]
mstratmann
Offline
Joined: 2012-10-04

I want to call a jPanel using an if statement.

Code from main package:

/*
* 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                  
}

Code from one of the jPanels:

/*
* 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                  
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
superinventor
Offline
Joined: 2012-08-20

Where do you need to do that reference?
What are you trying to do with that reference?

mstratmann
Offline
Joined: 2012-10-04

On the jFrame where the user inputs their name I would like their name to call a jPanel built to meet the criteria stored in their payroll scheme.

superinventor
Offline
Joined: 2012-08-20

I might suggest storing the panel in a variable somewhere that the frame can reach and just referencing that variable from the frame so as to get the panel.