Hello,
I have two classes, Help and RecylingGui. I have a button on the RecyclingGui called Help, which when clicked should open the text area created in the Help class. I added the ActionListener for this but nothing is happening. Any help would be appreciated. Thanks.

The Code:
Recycling Class

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
 * Simple Graphical User Interface for the Recycling Machine.

 *
 */
public class RecyclingGUI extends JFrame implements ActionListener  {
	
	CustomerPanel myPanel = new CustomerPanel(new Display()); 
	public CustomerPanel getPanel() { return myPanel; } 
	
	
	public void actionPerformed(ActionEvent e) {
		//System.out.println("Received: e.getActionCommand()="+e.getActionCommand()+
		//					" e.getSource()="+e.getSource().toString() ); 
		if( e.getSource() == slot1 )
		{
			myPanel.itemReceived(1);
		} else if( e.getSource() == slot2 ) { 
			myPanel.itemReceived(2);
		} else if( e.getSource() == slot3 ) {
			myPanel.itemReceived(3);
		} else if(e.getSource() == slot4 ){
			myPanel.itemReceived(4);
		} else if( e.getSource() == receipt ) {
			myPanel.printReceipt();
		} else if(e.getSource() == check){
			int siize = Integer.parseInt(size.getText().toString());
			int weeight = Integer.parseInt(weight.getText().toString());
			myPanel.printitem(siize,weeight);
		}else if(e.getSource() == unknown){
			myPanel.itemReceived(5);
		}
		
	}
	
	static JButton slot1 = new JButton("Slot 1"); 
	static JButton slot2 = new JButton("Slot 2"); 
	static JButton slot3 = new JButton("Slot 3"); 
	static JButton slot4 = new JButton("Slot 4");
	static JButton unknown = new JButton("unknown");
	static JButton help = new JButton("help");
	static JButton receipt = new JButton("Receipt");  
	static JTextField weight = new JTextField(5);
	static JTextField size = new JTextField(5);
	static JButton check = new JButton("Check"); 
	static JLabel sizeLabel  = new JLabel("Size");
	static JLabel weightLabel = new JLabel("Weight");
	
	
	private static JPanel getFieldPanel() {
	      JPanel p = new JPanel(new GridLayout(4,2));
	      p.setBorder(BorderFactory.createTitledBorder("Recycle"));
	      p.add(new JLabel("Weight:",SwingConstants.RIGHT));
	      p.add(size);
	      p.add(new JLabel("Size:",SwingConstants.RIGHT));
	      p.add(weight);
	      p.add(check);
	      p.add(help);
	      return p;
	   }
	
	public static JPanel getButtonPanel() {
	      JPanel p = new JPanel(new GridLayout(5,1));
	      //p.setBorder(BorderFactory.createTitledBorder("Recycle"));
	      p.add(slot1);
	      p.add(slot2);
	      p.add(slot3);
	      p.add(slot4);
	      p.add(unknown);
	      p.add(receipt);
	      return p;
	   }
	
	public RecyclingGUI() {
		super();
		setSize(400, 100);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2,1));
		
		
		panel.add(getButtonPanel()); 
		panel.add(getFieldPanel());
		
		slot1.addActionListener(this); 
		slot2.addActionListener(this);
		slot3.addActionListener(this); 
		slot4.addActionListener(this);		 
		receipt.addActionListener(this); 
		check.addActionListener(this);
		unknown.addActionListener(this); 
		help.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				new Help();
			}
			
		});
		
		
		getContentPane().add(panel);
		panel.repaint();
	
	}
	
	public static void main(String [] args ) { 
		RecyclingGUI myGUI = new RecyclingGUI(); 
		myGUI.setVisible(true); 
		myGUI.pack();
	}
}

Help Class

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

public class Help extends JPanel {
	public Help(){
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(400,200));
		
		JTextArea textArea = new JTextArea(5,40);
		textArea.setText("Help - FAQ");
		textArea.setEditable(false);
		JScrollPane ScrollPane = new JScrollPane(textArea);
		this.add(ScrollPane, BorderLayout.CENTER);
	}		
	
	public static void main(String[] args) {
		//Help GU = new Help();
		JPanel panel = new Help();
		panel.setOpaque(true);
		JFrame frame = new JFrame("Help");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(panel);
		frame.pack();
		frame.setVisible(true);
	}	
}

Recommended Answers

All 9 Replies

Your Help class has a main method that won't get called because that isn't the main method of the application. When you call new Help() that calls the Help() constructor, so you need to put ALL the code for creating and displaying the help window in that method.
The only reason why someone would have a main method in a class like that would be as an aid to testing - eg if you had

public static void main(String[] args) {
  new Help();
}

The you could test the help system by running it as a stand-alone application.

Thanks for the reply. Dont know if you meant doing this:

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

public class Help extends JPanel {
	public Help(){
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(400,200));
		
		JTextArea textArea = new JTextArea(5,40);
		textArea.setText("Help - FAQ");
		textArea.setEditable(false);
		JScrollPane ScrollPane = new JScrollPane(textArea);
		this.add(ScrollPane, BorderLayout.CENTER);
		JPanel panel = new Help();
		panel.setOpaque(true);
		JFrame frame = new JFrame("Help");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(panel);
		frame.pack();
		frame.setVisible(true);
		
		
	}
		
	public static void main(String[] args) {	
	}
	

}

This dint work.

Almost. In combining both sets of code you have created confusion. In particular on line 14 in the Help constructor you create a new instance of Help, which will recurse until you get a stack overflow.Also line 17 you probably just want to close the Help window, not exit the whole app.
But basically you are on the right line here.

Thanks i have made the modification you have suggested but nothing is still happening. I am clicking the button but there is no response, not even errors.

1. Put a print statement in the constructor so you can see if it's being called.
2. Post the current version of the Help class code

this is a stab in the dark but:

import javax.swing.*;

import java.awt.*;

public class Help extends JPanel {
	public Help(){
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(400,200));
		
		JTextArea textArea = new JTextArea(5,40);
		textArea.setText("Help - FAQ");
		textArea.setEditable(false);
		JScrollPane ScrollPane = new JScrollPane(textArea);
		this.add(ScrollPane, BorderLayout.CENTER);
		JPanel panel = new JPanel();
		panel.setOpaque(true);
		JFrame frame = new JFrame("Help");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(panel);
		frame.pack();
		frame.setVisible(true);
		
		
	}
		
	public static void main(String[] args) {
		new Help();
		
		
	}
	

}

Down to line 14 you build a Help JPanel (this), then 15-19 you create a new JPanel and add that to the frame instead. You don't need that new JPanel, and you should add "this" to the frame. Just slow down and think through this code slowly - it's really not hard.
Please don't do "stab in the dark". Use print statements to debug, and read your code and think what it's doing. Java is nothing if not logical.

Thanks for your help

recycled GUI:
Your original version need only one change in line 98
Replace new Help(); with Help.main(null);
(Change name main to any other)

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.