GUI Help

Iv'e made a non GUI version of my Programme and I now making i into a GUI version. I am however stuck on how to implement my search method, as its different from adding and displaying things in my array.

Below is my code

GUI

publicclass SearchPrisonersDialogextends JDialogimplements 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 ;

}

publicvoid actionPerformed(ActionEvent e)

{

if (e.getSource() == ok)

{

(id.getText());//Errors not a statement

HMESSEX.SearchPrisonersFOR(id);

}

elseif (e.getSource() == cancel)

{

// data entry cancelled

id =null;

}

dispose();

}

}

GUI

elseif (e.getSource() == SearchPrisoner)

{

SearchPrisonersDialog dlg =new SearchPrisonersDialog("Search for Prisoner Dialog",this);

dlg.setVisible(true);

Prisoner id = dlg.getPrisoner();

if ( id !=null)

{

displayArea.setText ( p.toString() );

HMEssex.SearchPrisoners(id);

}

else

displayArea.setText("No Prisoner Searched for" );

}

GUI

publicvoid SearchPrisonersFOR(JTextArea displayArea)

{

for(Person nextPrisoner : thePersons)

{

if (nextPrisonerinstanceof Prisoner)

{

Prisoner p = (Prisoner) nextPrisoner;

{

if (p.getPrisonerID().equals(id))

{

displayArea.append("\n" + p);

}

}

}

}

}

Before converting for GUI

publicvoid SearchPrisoners(String id)//search for prisoner by ID

{

System.out.println("Prisoners:");

for (Person x: thePersons)

{

//System.out.println("Found person: " + x.toString());

if (xinstanceof Prisoner)

{

Prisoner p = (Prisoner) x;

{

if (p.getPrisonerID().equals(id))//Checks if there is a matching ID within the enetered

{

System.out.println(p.toString());//Prints Prisoners Details

}

else

{

System.out.println("Prisoner with wrong id: "+p.getPrisonerID());//Prints Id/s that did not match

}

}

}

}

}

Thankyou for all your help in advance any more code required to solve my problem just ask

[5771 byte] By [beanymanuka] at [2007-11-27 1:01:08]
# 1

Problem is, your original design was not correct. Your SearchPrisoners method should have returned a List of Prisoners, or maybe an array of them. Having it just dump the info to the console was wrong.

If you had written it that way, then your GUI code could have called it and used the returned list to update the GUI. As it is your SearchPrisoners method is entangled with the context in which you use it, and you should try not to do that.

DrClapa at 2007-7-11 23:35:58 > top of Java-index,Java Essentials,Java Programming...
# 2
What my method does is search the prisoners ID and if it finds a match it prints out all of that prisoners information.
beanymanuka at 2007-7-11 23:35:58 > top of Java-index,Java Essentials,Java Programming...
# 3

> What my method does is search the prisoners ID and if

> it finds a match it prints out all of that prisoners

> information.

Yes, I know. And I said that was a bad design -- or at least not a good design given that you want to use it in different environments.

In case it wasn't clear, I was suggesting that you should change it.

DrClapa at 2007-7-11 23:35:58 > top of Java-index,Java Essentials,Java Programming...
# 4
how would you suggest I change it, as I'm not very experienced so, using different methods is abit daunting for me.
beanymanuka at 2007-7-11 23:35:58 > top of Java-index,Java Essentials,Java Programming...
# 5
As I said earlier:"Your SearchPrisoners method should have returned a List of Prisoners, or maybe an array of them."
DrClapa at 2007-7-11 23:35:58 > top of Java-index,Java Essentials,Java Programming...
# 6
so you want me to sort of filter out array so that only the result with the matching Prisoner id is left
beanymanuka at 2007-7-11 23:35:58 > top of Java-index,Java Essentials,Java Programming...