Runtime problem with a simple program

Hey all , I am trying to get accustomed with swing , and windows , and JFrame … JPanel and all those things.I had a book from long ago which contains some nice code examples and explains them.But for the love of god I can not understand why this poarticular code does not do what thought it does.I am making a window with 3 buttons which when pressed change the panel’s color to yellow , blue and red respectively.There is also a hotkey for each button. Ctrl + Y for yellow , Ctr + B for blue and Ctrl+ R for red.So here is the code.Any help would be really appreciated :slight_smile:

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

public class ActionTest
{
	public static void main(String[] args)
	{
		ActionFrame frame = new ActionFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}

/**
A frame with a panel that demonstrates color change actions
*/

class ActionFrame extends JFrame
{
	public ActionFrame()
	{
		setTitle("ActionTest");
		setSize(DEFAULT_WIDTH , DEFAULT_HEIGHT);

		//add panel to frame

		ActionPanel panel = new ActionPanel();
		add(panel);
	}

	public static final int DEFAULT_WIDTH = 300;
	public static final int DEFAULT_HEIGHT = 200;
}

/**
A panel with buttons and keyboard shortcutsto change the background color
*/

class ActionPanel extends JPanel
{
	public ActionPanel()
	{
		//define actions

		Action yellowAction = new ColorAction("Yellow" , new ImageIcon("yellow-ball.gif") , Color.YELLOW);
		Action blueAction = new ColorAction("Blue" , new ImageIcon("blue-ball.gif") , Color.BLUE);
		Action redAction = new ColorAction("Red" , new ImageIcon("red-ball.gif") , Color.RED);

		//add buttons to these actions

		add(new JButton(yellowAction));
		add(new JButton(blueAction));
		add(new JButton(redAction));

		//associate the Y , B and R keys with names

		InputMap imap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

		imap.put(KeyStroke.getKeyStroke("ctrl Y") , "panel.yellow");
		imap.put(KeyStroke.getKeyStroke("ctrl B") , "panel.blue");
		imap.put(KeyStroke.getKeyStroke("ctrl R") , "panel.red");

		//associate the names with actions

		ActionMap amap = getActionMap();
		amap.put("panel.yellow" , yellowAction);
		amap.put("panel.blue" , blueAction);
		amap.put("panel.red" , redAction);
	}

	public class ColorAction extends AbstractAction
	{
		/**
		Constructs a color action
		@param name the name to show on the button
		@param icon the icon to display on the button
		@param c the background color
		*/
		public ColorAction(String name , Icon icon , Color c)
		{
			putValue(Action.NAME , name);
			putValue(Action.SMALL_ICON , icon );
			putValue(Action.SHORT_DESCRIPTION , "Set panel color to "+name.toLowerCase());
		}

		public void actionPerformed(ActionEvent event)
		{
			Color c = (Color) getValue("color");
			setBackground(c);
		}
	}
}

First of all, I think you need a repaint() at the end of your actionPerformed() method so that the display is re drawn when changed.

Calling repaint is not needed. setBackground already takes care of invalidating the component and scheduling a repaint. In general it’s rarely necessary to explicitly call repaint in swing.

The color you pass to the ColorAction constructor is not being stored anywhere. In actionPerformed you try to retrieve the color using

getValue("color")

but this is never set, so the action always sets the background color to null. Adding

putValue("color", c);

to the ColorAction constructor should do the trick.

Hey thanks ! It did work :slight_smile:

But … I should not have even posted here :stuck_out_tongue:

Why ? Because even thought I read the code in the book 3 times I omitted that putCode() line all 3 times and did not see it ::slight_smile:

Thanks again mate :slight_smile:

I am so excited about learning to actually do things with Java now , all these things with swing , learning UI components and swing … I feel all warm and fuzzy inside :slight_smile:

setBackground already takes care of invalidating the component and scheduling a repaint. 

Invalidating? Unlikely, I’d say. It surely cannot be necessary to re-layout sub-components just in order to change the background color. It does repaint, though.

[quote=“Ask_Hjorth_Larsen,post:5,topic:25773”]
Yeah well… that’s what I meant :wink: My bad…
Note to self: think first, type later