Results 1 to 2 of 2
  1. #1
    Sterling is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Accessing Variables From a Different Class?

    Hey Everyone,
    I am fairly new to the world of Java and a student studying computer science. In class, I have a program that I have to write that is fairly simple. I have to make a payroll program that uses a GUI which has an array value used to store Employee ID numbers. The program first asks the user to input the Employee ID number and if the number is wrong (ID number is not stored in the array) the output reads 'Invalid ID Number' and if correctly entered (an ID number already stored in the array.)When correctly entered, it transfers to another GUI window which asks the user to enter their payrate and how many hours they worked. Then obviously the program displays the payrate, hours worked, and the amount to pay the employee. The output message is also supposed to display their employee ID Number and this is the part that I can't seem to figure out. I have everything else written and working correctly but I can't seem to get the variable employeeidTextField(input from the employee ID number) from my first class method to my second class method. Please someone help me out and tell me if there is even a way to do this or if I have just written the whole program wrong. THANKS FOR ANY HELP IN ADVANCE! =D


    HERE IS THE CODE:
    METHOD 1:

    import javax.swing.*;
    import java.awt.event.*;
    import java.text.*;

    public class PayrollClass extends JFrame
    {
    private JPanel panel;
    private JLabel messageLabel;
    private JTextField employeeidTextField;
    private JButton enterButton;
    private JButton clearButton;
    private final int WINDOW_WIDTH = 230;
    private final int WINDOW_HEIGHT = 120;
    public int [] ids = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
    public String id;


    public PayrollClass()
    {
    setTitle("Employee ID");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    add(panel);
    setVisible(true);
    }

    private void buildPanel()
    {
    messageLabel = new JLabel("Employee ID: ");
    employeeidTextField = new JTextField(10);

    enterButton = new JButton("Enter");
    enterButton.addActionListener(new EnterButtonListener());

    clearButton = new JButton("Clear");
    clearButton.addActionListener(new ClearButtonListener());

    panel = new JPanel();

    panel.add(messageLabel);
    panel.add(employeeidTextField);
    panel.add(enterButton);
    panel.add(clearButton);
    }

    private class EnterButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {

    int results;
    id = employeeidTextField.getText();

    results = sequentialSearch(ids, Integer.parseInt(id));

    if (results == -1)
    {
    JOptionPane.showMessageDialog(null, "You Have Entered An Invalid ID Number");
    }

    else
    {
    PayRollMethod prm = new PayRollMethod();
    }

    }



    public int sequentialSearch(int[] array, int value)
    {
    int index;
    int element;
    boolean found;

    index = 0;

    element = -1;
    found = false;

    while (!found && index < array.length)
    {
    if (array[index] == value)
    {
    found = true;
    element = index;
    }
    index++;
    }

    return element;
    }
    }


    private class ClearButtonListener implements ActionListener
    {

    public void actionPerformed(ActionEvent e)
    {
    employeeidTextField.setText(null);
    }
    }



    public static void main(String[] args)
    {
    PayrollClass pc = new PayrollClass();
    }

    }











    METHOD 2:

    import javax.swing.*;
    import java.awt.event.*;
    import java.text.*;



    public class PayRollMethod extends JFrame
    {
    private JPanel panel;
    private JLabel messageLabel1;
    private JLabel messageLabel2;
    private JTextField payrateTextField;
    private JTextField hoursTextField;
    private JButton enterButton;
    private JButton clearButton;
    private final int WINDOW_WIDTH = 300;
    private final int WINDOW_HEIGHT = 130;
    double pr;
    double hours;
    String id;

    public PayRollMethod()
    {
    setTitle("Pay Rate");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    add(panel);
    setVisible(true);
    }

    private void buildPanel()
    {

    messageLabel1 = new JLabel("Pay Rate: ");
    payrateTextField = new JTextField(10);

    messageLabel2 = new JLabel ("Hours Worked: ");
    hoursTextField = new JTextField(10);

    enterButton = new JButton("Enter");
    enterButton.addActionListener(new EnterButtonListener());

    clearButton = new JButton("Clear");
    clearButton.addActionListener(new ClearButtonListener());

    panel = new JPanel();

    panel.add(messageLabel1);
    panel.add(payrateTextField);
    panel.add(messageLabel2);
    panel.add(hoursTextField);
    panel.add(enterButton);
    panel.add(clearButton);
    }


    private class EnterButtonListener implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {



    pr = Double.parseDouble(payrateTextField.getText());

    hours = Double.parseDouble(hoursTextField.getText());


    JOptionPane.showMessageDialog(null, "Employees ID Number: "+id+"\nEmployee's Payrate: $"+pr+" Per Hour\nEmployee's Hours Worked: "+hours+"\nEmployee's Earnings: $"+pr*hours+"");
    }
    }


    private class ClearButtonListener implements ActionListener
    {

    public void actionPerformed(ActionEvent e)
    {
    payrateTextField.setText(null);
    hoursTextField.setText(null);
    }
    }


    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Sterling View Post
    Please someone help me out and tell me if there is even a way to do this or if I have just written the whole program wrong.

    i don't see any instantiation of your METHOD 2, but one solution could be to pass the employee id as argument in the constructor of METHOD 2. but this solution depends where and how you instantiate your methods.

Similar Threads

  1. Replies: 1
    Last Post: 01-03-2010, 09:48 PM
  2. Replies: 4
    Last Post: 10-22-2009, 05:45 PM
  3. accessing variables
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 04:36 AM
  4. Replies: 8
    Last Post: 03-21-2009, 09:34 AM
  5. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10:09 PM

Tags for this Thread

Posting Permissions

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