Hi i have been trying to get a GUI in BlueJ working properly but i hav come across a prolem, i hav 2 buttons which i want to use but when i click them the actionListener wont run my methods.


In the action preformed bit i tried to make it so when you pressed the button it would make the NewPatient method work but it came up with the error '.class' was expected', also when i try to use it to call the method getPatient the same error occurs. if anyone could help me with this it would be helpful
Thanks
Chris

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * Service desk is a program which can hold multiple service 
 * requests which can be looked at by members of staff so
 * the staff can try and solve them
 * 
 * @author (Chris Holmes) 
 */
public class PatientInformationSystem
implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {    
        
        //System.out.println("item:"+event.getActionCommand());
    }
    private JFrame frame;

    // Storage for the arbitrary number of mail items 
    //to be stored on the help desk system
    private List<Patient> Patients;
   
    //create a new integer called nextuniquenumber
    private int nextuniquenumber;
   /**
    * construct a service desk
    */
    public PatientInformationSystem()
    {
        makeFrame();
        //create a new array list
        Patients=new ArrayList<Patient>();
        
        nextuniquenumber=1;
    }

    /**
     * This method adds a new request to the system
     */
    public void newPatient (int Uniquenumber, String PatientName, String PatientGender,String PatientAddress,String HealthProblem,String CurrentState,String DateAdmitted)
    {
        Patients.add(new Patient(nextuniquenumber, PatientName, PatientGender, PatientAddress, HealthProblem, CurrentState, DateAdmitted));
        //change the uniguenumber value up one
        nextuniquenumber++;
    }
    private void makeFrame()
    {
        frame= new JFrame("Frame");
        Container contentPane=frame.getContentPane();
        
        JLabel label=new JLabel("Patient Information System");
        contentPane.add(label);
        
        JButton button=new JButton("Button1");
        contentPane.add(button);
        
        JButton quitItem=new JButton("Button2");
        quitItem.addActionListener(this);
        contentPane.add(quitItem);
        
        frame.pack();
        frame.setVisible(true);
    }
   
    /**
     * This method shows all the Requests on the system
     */
    public void showPatients()
    {
        for(Patient patient : Patients) {
            System.out.println(patient);
        }
    }
    
    public Patient getPatient(int uniquenumber)
    {
        if ((uniquenumber >=1) && (uniquenumber < nextuniquenumber)){
            Patient selectedpatient=Patients.get(uniquenumber-1);
            return selectedpatient;
        }
        else
       {
           System.out.println("Patient number: " + uniquenumber +
                              " does not exist.");
           return null;
        }
    }   
    
}
/**
 * Each individual Request
 * 
 * Chris Holmes 

 */
public class Patient
{
    // Variables for each request
    public final int PatientID;
    public String PatientName;
    public String PatientGender;
    public String PatientAddress;
    public String HealthProblem;
    public String CurrentState;
    public String DateAdmitted;

    /**
     * 
     */
    public Patient(int PatientID, String PatientName, String PatientGender,String PatientAddress,String HealthProblem,String CurrentState,String DateAdmitted)
    {
       this.PatientID=PatientID;
       this.PatientName=PatientName;
       this.PatientGender=PatientGender;
       this.PatientAddress=PatientAddress;
       this.HealthProblem=HealthProblem;
       this.CurrentState=CurrentState;
       this.DateAdmitted=DateAdmitted;
    }

    /**
     *  
     */
    public String toSting()
    {
        String details = PatientID + ": " + PatientName + ":" + PatientGender + ":" + PatientAddress + ":" + HealthProblem + ":" + CurrentState +":" + DateAdmitted;
        return details;
    }       
    
    public int getPatientID()
    {
        return PatientID;
    }
    public String getPatientName()
    {
        return PatientName;
    }
    public String getPatientGender()
    {
        return PatientGender;
    }
    public String getPatientAddresse()
    {
        return PatientAddress;
    }
    public String getHealthProblem()
    {
        return HealthProblem;
    } 
    public String getCurrentState()
    {
        return CurrentState;
    }
    public String getDateAdmitted()
    {
        return DateAdmitted;
    }
    
}

Hi Chris =) I remember making GUIs with BlueJ =)

You've done most everything right, there are just a few problems =)

In the Patient Class, change toSting(), to toString() ;) just a typo, but it needs to be fixed.

In the PatientInformationSystem (That's a mouthful!) Class, you should change "nextuniquenumber" to a static int. This needs to be static because you should only ever be counting this in one place. Example. You have this program running in a hospital and it's networked together on all the computers. If you add a patient on computer 1, then the patient count on computer 1 goes up, but not on any of the others (Someone will call me out on this example, i know). This isn't networked, but it's the same thing. What the static does, is it ensures that there is only ever 1 instance of that variable - you don't want to double up on patient numbers, or skip over some.

You also don't need to pass an int to the addPatient method, it's never used, just use the static nextuniquenumber.

Also, you should set a default close operation for your frame, so it doesn't continue to eat up the computers memory.
Just add this line after the frame constructor: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE That makes it so that when you close the window, the program exits, rather than running in the background - which is what it does normally.

Okay. On to the actionListener part....
It all looks good, just a couple little things ;)

When you make a button that's going to have an ActionEvent attached to it, you might want to do something like this:

// constants that are the button text AND the action event name -
// now it only needs to be changed in one place.
    private static final String CHAT = "Chat";
    private static final String HIDE = "Hide Window";
    private static final String EXIT = "Quit";
    
    private void makeFrame(){
        frame = new JFrame("My Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        Container pane = frame.getContentPane();
        // Define the way the components organize themselves.
        pane.setLayout(new FlowLayout());
        
        // Create the buttons.
        JButton chat = new JButton(CHAT);
        JButton hide = new JButton(HIDE);
        JButton exit = new JButton(EXIT);
        
        // Add the actionListener to the buttons.
        chat.addActionListener(this);
        hide.addActionListener(this);
        exit.addActionListener(this);
        
        // Add everything to the ContentPane.
        pane.add(chat);
        pane.add(hide);
        pane.add(exit);
        
        frame.pack();
        frame.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent event){
        if(event.getActionCommand().equals(CHAT)){
            System.out.println("You pressed the Chat button.");
        }
        if(event.getActionCommand().equals(HIDE)){
            System.out.println("You pressed the Hide button.");
        }
        if(event.getActionCommand().equals(EXIT)){
            System.out.println("You pressed the Exit button.");
            System.out.println("Good-bye.");
            System.exit(0); // Shut everything down.
        }
    }

The key is making sure you're checking for the correct event, and responding correctly. When I ran your code, it worked for me. try fixing the above things, and running it again.

hope this helped =)

-Neil

Hey Neil, thanks for your help, just a quick one, im still trying to make the buttons work so that the run the methods "public void newPatient" and "public Patient getPatient" i keep getting errors such as ")" expected, i have tried several ways to fix this but non work, any chance you know what im doing wrong? cheers Chris

You wouldn't happen to be doing this would you?

public void actionPerformed(ActionEvent e){
    if(e.getActionCommand().equals("Command")){
        public void someMethod();
    }
}

That says you're defining a method inside another method - and java doesn't like that. To call the other method, all you have to do is type "someMethod()" rather than "public void someMethod()".

No i tried it without the public void so it was like

public void actionPerformed(ActionEvent event)
    {
        if(event.getActionCommand().equals(CHAT))
        {
            newPatient (String PatientName, String PatientGender,String PatientAddress,String HealthProblem,String CurrentState,String DateAdmitted);
            //System.out.println("You pressed the Chat button.");
        }

or

public void actionPerformed(ActionEvent event)
    {
        if(event.getActionCommand().equals(CHAT))
        {
            getPatient(int uniquenumber);
            //System.out.println("You pressed the Chat button.");
        }

i think it may be because i am using the patient class in these methods but im not sure :confused:

Chris

Oh! okay, I was making sure you weren't doing it the way i described. When you pass a value, you don't need the type out front.
Like so:

if(event.getActionCommand().equals(CHAT))
        {
            newPatient (PatientName, PatientGender, PatientAddress, HealthProblem, CurrentState, DateAdmitted);
            //System.out.println("You pressed the Chat button.");
        }

Also, be sure to use variables that are easy to read/type, and make sense. And don't start variables with a capital letter, that's reserved for class names (Not required, but a good coding practice).

When i tried this it said "cannot find symbol- variable PatientName"

The same thing happened when i tried it with the getPatient method as well, any idea how to fix this? thanx chris

have you never used java before? You have to define those variables before you can use them.

private String patientName = ""; // Use a lowercase name for vars.
// other vars that are used through out the class

Here are a few tutorials on some basic java concepts:
Getting Started with Java
Java Language Basics
Essential Java Classes

And a swing tutorial for when you understand how java works better.
Java Swing

Sorry, that was a stupid question, but i hav got the button mostly working how i want it to, the problem is now when i press the button it adds a new patient but i can't choose what details the patient will have, how can i change it so that a dialogue box appears where i can enter each indavidual patients details?

if(event.getActionCommand().equals(CHAT))
        {       
                Patients.add(new Patient(nextuniquenumber, PatientName, PatientGender, PatientAddress, HealthProblem, CurrentState, DateAdmitted));
                     
                nextuniquenumber++;
                System.out.println("You Added A New Patient");
            
           
            //System.out.println("You pressed the Chat button.");
        }

This is what i have so far, thanks chris

you need to look at the JComboBox, JTextField, and JTextArea classes. Those are used for getting information from the user. you'll have to make a new window that shows itself when you press the button, and closes when you hit a button that you call "save", or "add" in the new window. Read that Swing Tutorial that I left in the other post.

Hi thanks that swing tutorial helped, just a quick question im trying to get my search button working atm it looks like this

public JTextField idNumber= new JTextField();

if(event.getActionCommand().equals(SEARCH1))
        {
            uniquenumber= idNumber.getText();
            if ((uniquenumber >=1) && (uniquenumber < nextuniquenumber))
            {
                Patient selectedpatient=Patients.get(uniquenumber-1);
                return selectedpatient;
            }
            else
           {
                System.out.println("Patient number: " + uniquenumber +
                              " does not exist.");
                              return null;
            }
    }

When i try to run it, it says incompatible types found..... found string expected int, is this because i am using the getText() or because it is searching in a textfield?, if soo what should i be using instead? thanks chris

Hi =) You're right, the problem with what you're doing is comparing a String type from the getText() method, to an int type. You have to use the Integer.parseInt() method and pass it the String that you get from getText(). Look at the Integer class to see how to use it if you don't already know =)

Happy coding!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.