Reputation: 367
Im trying to get a value from a text field (entered by the users) to use for processing. But no matter what I do it will not get the value that was entered it seem to remain empty. Could some one please tell me why it will not get the value from the Text field.
this is the method which initialy created the text field which was named writeStrings
public void chooseEmpToAdd()
{
JTextArea EmpDetails = new JTextArea(5,20);
JTextField writeStrings = new JTextField(20);
JLabel enterIDno = new JLabel("Please enter The Employye ID number that you wish to assign to a department: ");
JButton submit = new JButton (" Submit") ;
ButtonListenerEmp Listener2 = new ButtonListenerEmp();
submit.addActionListener(Listener2);
JFrame frameAllEmps = new JFrame();
frameAllEmps.setSize( 150, 140 );
frameAllEmps.pack();
frameAllEmps.setVisible(true);
//layout
frameAllEmps.setLayout(new FlowLayout());
frameAllEmps.add(enterIDno);
int x = 0;
System.out.println("ALL Emps from the tree map");
for(int key:employeeMap.keySet())
{
Employee dEmp = employeeMap.get(key);
System.out.println("Employe no :" +x+": "+dEmp);
EmpDetails.setText(EmpDetails.getText()+" "+dEmp);
frameAllEmps.add(EmpDetails);
x++;
}
frameAllEmps.add(new JScrollPane(EmpDetails));
frameAllEmps.add(writeStrings);
frameAllEmps.add(submit);
frameAllEmps.pack();
}
And this is the action listener which should take the value from the Text box and print it to the console, but it does not work.
private class ButtonListenerEmp implements ActionListener
{
public void actionPerformed (ActionEvent e )
{
String ID ;
int dID;
ID = writeStrings.getText();
System.out.println("start of try b4 changes: "+ID);
}
}
Upvotes: 1
Views: 255
Reputation: 2348
One thing that comes to my mind from the code presented is that you create the new JTextField
and store it in a local variable called writeStrings
maybe that is not the same you try to read from lateron.
Upvotes: 0
Reputation: 160170
The listener implementation shouldn't have access to the local variable writeStrings
, I'm not even sure how that would compile--is the code you posted accurate?
Oh, you probably have both a local variable writeStrings
, and an instance variable writeStrings
, although it's hard to say since you didn't post the rest of the code. Try not declaring writeStrings
in the chooseEmpToAdd
method; use the class variable instead.
Upvotes: 3
Reputation: 6516
The variable ID might not have been initialized (i.e. String ID = "";
)
Also there is a compile error at the line writeStrings.getText()
because the variable writeStrings
doesn't exist outside of the chooseEmpToAdd()
method. Try declaring JTextField writeStrings = new JTextField(20);
before the method instead.
Upvotes: 2
Reputation: 117579
Because you're declaring the text field as a local variable of chooseEmpToAdd
method, so it is not being seen by ButtonListenerEmp
class. To solve this problem, declare the text field as a class field and make it public or pass the text of the text field as an argument of ButtonListenerEmp
constructor.
Upvotes: 2