Results 1 to 2 of 2
- 04-08-2010, 04:25 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 1
- Rep Power
- 0
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);
}
}
}
- 04-08-2010, 05:01 PM #2
Similar Threads
-
Exception Class for class that compares objects variables. Help Please.
By darkblue24 in forum New To JavaReplies: 1Last Post: 01-03-2010, 09:48 PM -
Accessing non-static public variables from another class
By ribbs2521 in forum New To JavaReplies: 4Last Post: 10-22-2009, 05:45 PM -
accessing variables
By emp in forum New To JavaReplies: 3Last Post: 04-23-2009, 04:36 AM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
accessing instance variables from static methods
By ravian in forum New To JavaReplies: 7Last Post: 03-01-2009, 10:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks