-
Save button not working
I meant to say that the back button was not working
Code:
import java.awt.*;
import java.awt.event.*;
import javax .swing.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.util.StringTokenizer;
public class FindPatient extends JFrame implements ActionListener
{
private JLabel Name,MRN,Address,Age,Admission,Wardname,Wardnumber,Blank1,Blank2,Blank3,Blank4,Blank5;// declares the labels,the blank labels are used for spaces in the layout
private JTextField name,mrn,address,age,admission,wardname,wardnumber;//declares the text fields
private JButton Search,Back;// declares the buttons
public static void main(String args[])//main method
{
FindPatient app = new FindPatient();
}
public FindPatient() //constructor
{
super("Patient Search");//title of Frame
Container c = getContentPane();
c.setLayout(new GridLayout(11,2,0,0));//set the layout manager to grid and set the number of rows and columns in the grid
Name = new JLabel("Patient Name");// set the text property of the label
c.add(Name);//add the label to the container
name = new JTextField(20);
c.add(name);//add the textfield to the container
Blank3 = new JLabel(" ");
c.add(Blank3);//add the label to the container
Search = new JButton("Search");
Search.addActionListener(this);
c.add(Search);
Blank4 = new JLabel(" ");
c.add(Blank4);//add the label to the container
Blank5 = new JLabel(" ");
c.add(Blank5);//add the label to the container
MRN = new JLabel("MRN");
c.add(MRN);//add the label to the container
mrn = new JTextField(20);
c.add(mrn);//add the textfield to the container
Address = new JLabel("Address ");
c.add(Address);//add the label to the container
address = new JTextField(20);
c.add(address);//add the textfield to the container
Age = new JLabel("Age ");
c.add(Age);//add the label to the container
age = new JTextField(20);
c.add(age);
Admission = new JLabel("Admission ");
c.add(Admission);//add the label to the container
admission = new JTextField(20);
c.add(admission);
Wardname = new JLabel("Ward Name ");
c.add(Wardname);//add the label to the container
wardname = new JTextField(20);
c.add(wardname);
Wardnumber = new JLabel("Ward Number ");
c.add(Wardnumber);//add the label to the container
wardnumber = new JTextField(20);
c.add(wardnumber);
Blank1 = new JLabel(" ");
c.add(Blank1);//add the label to the container
Blank2 = new JLabel(" ");
c.add(Blank2); //add the label to the container
Back = new JButton("Back");
Search.addActionListener(this);
c.add(Back);
setSize(400, 300);//set the size of the frame
setVisible(true);//set the frame to visible
}
public void Search_actionPerformed(ActionEvent e)
{
Find();// call the find method
}
public void Back_actionPerformed(ActionEvent e)
{
Menu Menu = new Menu();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Search)
{
Search_actionPerformed(ae);
}
else
{
Back_actionPerformed(ae);
}
}
public void Find()//find method
{
try{
BufferedReader d = new BufferedReader(new FileReader("patient.txt"));// open and read for the text file "patient.txt"
String str ="";
while((str = d.readLine())!=null)
{
StringTokenizer stz = new StringTokenizer(str,"/");
if(stz.nextToken().equals(name.getText()))
{
mrn.setText((String)stz.nextToken());
address.setText((String)stz.nextToken());
age.setText((String)stz.nextToken());
admission.setText((String)stz.nextToken());
wardname.setText((String)stz.nextToken());
wardnumber.setText((String)stz.nextToken());
}
}
}
catch(Exception u)
{
JOptionPane.showMessageDialog(this,"Can't Open File");
}
}
}
-
Please define what "not working" and "working" mean.
I see that pressing the back button seems to create a new "Menu" object. What is this Menu object, and how will creating a new one do anything for your GUI?
-
And your tittle misleading too. Anyway, specifically ask your question please rather posting a long code.
-
Code:
// Instantiate the Back button
Back = new JButton("Back");
// and add the ActionListener to the Search button.
// But wait, you already added the listener to the
// Search button above - just after you instantiated
// it. So this next line is not needed.
Search.addActionListener(this);
// Instead, this would be a good place to add the
// listener to the Back button:
// Back.addActionListener(this);
c.add(Back);
...
public void Back_actionPerformed(ActionEvent e)
{
Menu Menu = new Menu();
// This method is never called because the ActionListener
// which calls this method was not added to the Back button.
// If you add the listener and this method does get called
// it doesn't do anything anyway. You could put a println
// statement in here to see if the button-listener-method
// sequence fires, eg,
System.out.println("Back_actionPerformed");
}