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

[563 byte] By [beanymanuka] at [2007-11-27 1:03:39]
# 1
you can do it many ways that depends on your requirement and architecture for example 1. create static variable in main class and assign the value for that from the dialog class2. pass the main object to the dialog class and control from the dialog class etc
infyscreena at 2007-7-11 23:38:44 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

KathyMcDonnella at 2007-7-11 23:38:44 > top of Java-index,Java Essentials,Java Programming...
# 3

yeh i have done this already

int value = Integer.parseInt(theWardenRank.getText());

now i just need to use "value" in my testGUI file

so i can use it in this code

if(value >=3)

{

numRankWardens++;

}

beanymanuka at 2007-7-11 23:38:44 > top of Java-index,Java Essentials,Java Programming...
# 4

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

beanymanuka at 2007-7-11 23:38:44 > top of Java-index,Java Essentials,Java Programming...
# 5
I'm afraid you're trying to run before you can walk. Good luck.
KathyMcDonnella at 2007-7-11 23:38:44 > top of Java-index,Java Essentials,Java Programming...
# 6
Thankz for the luck i need it. Just need solve some little problems thats all.
beanymanuka at 2007-7-11 23:38:44 > top of Java-index,Java Essentials,Java Programming...