Hi,
I am having a problem with this program. I don't know why the counter won't work correctly. After "doctor is out" is clicked the inCounter should be zero. So when the "doctor is in" is clicked "the doctor is in" should be displayed, but "the doctor IS IN, Already!" is displayed. Also, why does the frame have to be enlarged slightly in order for display to change?
Thank You in advance for any help.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;


public class Lucy extends JFrame implements ActionListener
{

	JPanel panel = new JPanel(new GridLayout(1,2));
	JPanel panel2 = new JPanel(new BorderLayout());
	JPanel panel3 = new JPanel(new BorderLayout());
	GridBagConstraints c = new GridBagConstraints();

	//Create two icons
	ImageIcon doctorIn = new ImageIcon("doctorIn.jpg");
	ImageIcon doctorOut = new ImageIcon ("doctorOut.jpg");

	//Create two labels for images
	JLabel jlblDoctorIn = new JLabel(doctorIn);
	JLabel jlblDoctorOut = new JLabel (doctorOut);

	//Create two labels for text
	JLabel jlblDoctorInText = new JLabel("The doctor is in.");
	JLabel jlblDoctorOutText = new JLabel("The doctor is out.");
	JLabel jlblDoctorINText = new JLabel ("The doctor IS IN, already!");

	int inCounter = 1;

  public Lucy()
  {

	// Create two buttons
	JButton jbtDoctorIn = new JButton("Click for In");
	JButton jbtDoctorOut = new JButton("Click for Out");

 	// Create a panel to hold buttons
    panel.add(jbtDoctorIn,c);
    panel.add(jbtDoctorOut,c);

	//Create a panel to hold the label
	panel2.add(jlblDoctorIn,BorderLayout.NORTH);

	//Create a panel to hold both panels
	panel3.add(panel, BorderLayout.SOUTH);
	panel3.add(panel2, BorderLayout.NORTH);

   	//add contents to the frame
   	add(panel3,BorderLayout.NORTH);
   	add(jlblDoctorInText, BorderLayout.SOUTH);

    // Register listeners
    jbtDoctorIn.addActionListener(this);
    jbtDoctorOut.addActionListener(this);
  }


  public void actionPerformed(ActionEvent e)
  {
	  String actionCommand = e.getActionCommand();
	  if(actionCommand.equals("Click for In"))
		{
			doctorIsIn(inCounter);

		}
	  else if(actionCommand.equals("Click for Out"))
		{
			doctorIsOut(inCounter);

		}
	else System.out.println("Error in button interface.");
  }

  public int doctorIsOut(int inCounter)
  {
	panel2.remove(jlblDoctorIn);
	remove(jlblDoctorInText);
	remove(jlblDoctorINText);
	panel2.add(jlblDoctorOut,BorderLayout.NORTH);
	add(jlblDoctorOutText,BorderLayout.SOUTH);
	inCounter = 0;
	repaint();
	return(inCounter);

  }

  public int doctorIsIn(int inCounter)
  {
	if(inCounter < 1)
	{
		panel2.remove(jlblDoctorOut);
    	remove(jlblDoctorOutText);
    	panel2.add(jlblDoctorIn,BorderLayout.NORTH);
    	add(jlblDoctorInText,BorderLayout.SOUTH);
		inCounter = inCounter + 1;
    	repaint();
    	return (inCounter);
	}
	else
	{
		panel2.remove(jlblDoctorOut);
		remove(jlblDoctorOutText);
		remove(jlblDoctorInText);
		panel2.add(jlblDoctorIn,BorderLayout.NORTH);
		add(jlblDoctorINText, BorderLayout.SOUTH);
		inCounter = inCounter + 1;
		repaint();
		return (inCounter);
	}
  }

  public static void main(String[] args)
  {
    JFrame frame = new Lucy();
    frame.setTitle("Lucy");
    frame.setSize(300, 320);
    frame.setLocation(200, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

well ... what you are describing is not the output of running the code you've posted.
in the code you've posted, when you start your application, you'll see the text "the doctor is in"

when you press any button, you clear that text without setting a new value visible.

How does your code change the value of the counter?
Add some printlns to show the value of the counter every time it is changed and to show it when it is being tested.

Yes, when the program starts "the doctor is in" is displayed. When "doctorOut" is clicked, "the doctor out is displayed", but then when "doctorIn" is clicked, "the doctor IS IN Already is displayed, instead of "the doctor is in". "the doctor IS IN Already should only display if the doctorIn button is clicked twice in a row.

I have put println statements in to display the counter as the program runs. The counter stays 1 no matter how many times "doctorIn" is clicked. I don't understand why because I increment it.

Thank You both for your help

Follow the logic of the program and print the counter's value until you see where the problmem is.
print its value out where you change its value and then print it out when you return from the method where you change its value.

Remember that java passes variables by value. How do the values set in in the method get outside of the method?

inCounter somehow gets set to zero before it gets sent to the methods, even though it is incremented and sent back as 1
I had it initialized at 1 at one point, but then it was always 1.
I can't initialize at zero because since the program starts out with "doctor is in" the counter should be at one. Right?

They get outside of the method by being returned from the method.

They get outside of the method by being returned from the method.

Where does the value returned by the method go? Is it used for anything or is it ignored?

Thank You, it works!!
Can you help me with why I have to slightly enlarge the frame in order to get the new display to appear? I have tried making the frame larger to start with, but it doesn't help.

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.