We help IT Professionals succeed at work.

Question about Action Events

Gregg
Gregg used Ask the Experts™
on
Below is a sample application that I am using to learn about events. It simply changes the background color of my JPanel. If I wanted to change the application title as well, how would i implement that?

Do i reference the JFrame in my ButtonList class and set the title in the makeButton() method? How might i go about doing that? Or what method would you recommend if i wanted to change background color and App Title when a button is clicked.

Thank you.

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

/**
 * Driver Class
 */
public class TestApplication
{
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
			{
				public void run() 
				{
					AppFrame frame = new AppFrame();
					frame.init();
				}
			}
		);
	}
}//end class

/**
 * This class represents the JFrame object
 */
class AppFrame extends JFrame
{

	//*********************
	// Attributes
	//*********************
	
	//*********************
	// Constructors
	//*********************
	public AppFrame()
	{
		super();
	}
	
	//*********************
	// Methods
	//*********************
	public void init() {
		//Set Title
		setTitle("Test 1");
		
		//Set Window State
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		
		//Build Buttons Menu
		ButtonList bl = new ButtonList();
		add(bl.getButtonList());
		
		//Set Visible
		setVisible(true);
		
		//Set Close
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
}//end appFrame class

/** 
 * This class represents the Button JPanel
 */
class ButtonList extends JPanel
{	
	//*********************
	// Attributes
	//*********************
	
	//*********************
	// Constructors
	//*********************
	public ButtonList() 
	{
		super();
		//Create Buttons
		makeButton("Blue", Color.BLUE);
		makeButton("Green", Color.GREEN);
		makeButton("Orange", Color.ORANGE);
		makeButton("Yellow", Color.YELLOW);
	}
	
	//*********************
	// Methods
	//*********************
	public JPanel getButtonList() {
		return this;
	}
	
	public void makeButton(String title, final Color color)
	{
		JButton b = new JButton(title);
		b.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent event) {
					setBackground(color);
				}
			}
		);
		add(b);
	}
}//end class

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:
I would pass instance of your JFrame to class ButtonLisrt in the constructor
and then use this instance call setTitle("name") in actionListener
Awarded 2011
Awarded 2011
Commented:

Something like that:


ButtonList bl = new ButtonList(this);

...


class ButtonList extends JPanel
{      
JFrame f;
      //*********************
      // Attributes
      //*********************
      
      //*********************
      // Constructors
      //*********************
      public ButtonList(JFrame f)
      {
            super();
                            this.f = f;
.....

b.addActionListener(new ActionListener()
                  {
                        public void actionPerformed(ActionEvent event) {
                              setBackground(color);
                                                                             f.setTitle("name");
                        }
                  }
            );




Author

Commented:
Answer was perfect! Thanks for your help for_yan.