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);
}
}
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 variablewriteStrings, although it’s hard to say since you didn’t post the rest of the code. Try not declaringwriteStringsin thechooseEmpToAddmethod; use the class variable instead.