Adding a condition in GUI
I have a main GUI file which is like the test file and I have an AddwardenDialog file i need to get the value entered in theWardenRank from AddWardenDialog to use in my main GUI file to determine a condition relating to something else.
So just want to know how I can copy this value from AddwardenDialog to main testing GUI file
add (new JLabel("Enter Warden Rank:"));
theWardenRank =new JTextField(20) ;
add (theWardenRank);
Thankyou
You can get the value out of the text box by using getText():
String value = theWardenRank.getText();
What comes out of the textbox is String (since the user could have put anything there).
To convert it into an integer, use Integer.parseInt:
int value = Integer.parseInt(theWardenRank.getText());
Note, the parseInt() method will throw an exception if the user did not type in a legal integer.
So you have to catch the NumberFormatException.
noone got any idea's of how to do this then? HELP lol
I've got another problem as well adding a search method in GUI
non GUI version
Prison.java
public void SearchPrisonersFOR(JTextArea displayArea)
[code]{
for(Person nextPrisoner : thePersons)
{
if (nextPrisoner instanceof Prisoner)
{
Prisoner p = (Prisoner) nextPrisoner;
{
if (p.getPrisonerID().equals(id))
{
displayArea.append("\n" + p);
}
}
}
}
}
TestPrison.java
//Search for Prisoner by ID
case 6:
{
System.out.println("Enter ID to search");
id = kybd.next();
HMEssex.SearchPrisoners(id);
break;
}
GUI
SearchPrisonersDialog.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SearchPrisonersDialog extends JDialog implements ActionListener
{
private JButton ok;
private JButton cancel ;
private JTextField id;
public SearchPrisonersDialog(String sTitle, JFrame owner)
{
super (owner, sTitle, true);
setLayout(new FlowLayout());
setSize(400,250);
add (new JLabel("Enter Prisoner ID to search for:"));
id = new JTextField(10) ;
add (id);
ok = new JButton("Search");
ok.addActionListener(this);
add(ok);
cancel = new JButton("Cancel");
cancel.addActionListener(this);
add(cancel);
}
//public Prisoner getPrisoner()
//{
//return thePrisoner ;
//}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == ok)
{
return id;
//SearchPrisonersFOR(id);
}
else if (e.getSource() == cancel)
{
// data entry cancelled
id = null;
}
dispose();
}
}
how do I add a method into Main GUI TestPrisonGUI.java, so that my search method will work
2 problems to solve now
Thankyou