1 Replies - 6821 Views - Last Post: 08 May 2009 - 05:34 PM Rate Topic: -----

#1 boshra   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

1. how to set an image for a jbutton 2.how to set a listener in prder

Posted 08 May 2009 - 01:09 PM

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.JTextComponent;

import pieces.Place;

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridLayout;;
public class GUI extends JFrame implements ActionListener, MouseListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton[] buttons=new JButton[64];
	private int timesClicked=0;
	Place p=new Place();
	// set up GUI
	public GUI()
	{
		super( "Chess" ); 
		Icon image=new ImageIcon("prison break.GIF");
		JPanel jp1=new JPanel(new GridLayout(8,8));
		JPanel jp2=new JPanel(new GridBagLayout());
		jp1.setLayout(new GridLayout(8,8));
		jp2.setLayout(new GridLayout(1,1));
		// create GUI components
		int k=0;
		char[] col=new char[2];
		for(int i=7;i>=0;i--){
			col[0]='A';
			col[1]=(char) ('0'+i+1);
			String sb=new String(col);
			for(int j=7;j>=0;j--){
				buttons[k].setRolloverIcon(image);
				buttons[k]=new JButton(sb,image);
				jp1.add(sb,buttons[k]);
				if(i%2==0){
					if(k%2==0){
						buttons[k].setBackground(new Color(255,255, 255));
						buttons[k].setForeground(new Color(255,255,255));
					}
					else if(k%2==1){
						buttons[k].setBackground(new Color(0,0,0));
						buttons[k].setForeground(new Color(0,0,0));
					}
				}
				else{
					if(k%2==1){
						buttons[k].setBackground(new Color(255,255, 255));
						buttons[k].setForeground(new Color(255,255,255));
					}
					else if(k%2==0){
						buttons[k].setBackground(new Color(0,0,0));
						buttons[k].setForeground(new Color(0,0,0));
					}
				}
				buttons[k].addActionListener(this);
				k++;
				col[0]++;
				sb=new String(col);
			}
		}
		JTextComponent jtc=new JTextArea(1,1);
		
		jp2.add(jtc);
		JPanel panel=new JPanel();
		panel.setLayout(new BoxLayout(panel,BoxLayout.X_AXIS));
		panel.add(jp2);
		panel.add(jp1);
		add(panel);
		
	} // end GridBagFrame constructor
	@Override
	public void actionPerformed(ActionEvent e) {
		this.timesClicked++;
		Integer k=new Integer(0); 
		k = p.get(e.getActionCommand());
		if(this.timesClicked%2==1){
			this.buttons[k].setBackground(new Color(128,128,128));
		}
		else{
			this.buttons[k].setBackground(buttons[k].getBackground());
		}
		repaint();
	}
	public static void main( String args[] )
	{
		GUI gridBagFrame = new GUI();
		gridBagFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		gridBagFrame.setSize(900,600 ); // set frame size
		gridBagFrame.setVisible( true ); // display frame
	} // end main

} // end class GUI
import java.util.HashMap;
import java.util.Map;

public class Place extends HashMap<String, Integer> implements Map<String, Integer>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Map<String,Integer> p=new HashMap< String,Integer>(64);
	public Place() {
		Integer k=1;
		for(int i=0;i<8;i++){
			char[] col=new char[2];
			col[0]='A';
			col[1]=(char) ('0'+i+1);
			String sb=new String(col);
			for(int j=0;j<8;j++){
				p.put(sb,k);
				k++;
				col[0]++;
				sb=new String(col);
			}
		}
	}
	protected Integer get(String key) {
		return super.get(key);
	}
}
 

first of all I want to set an image for each button, but I can't do it and I don't know what's the problem with the code. I get a "NullPointerException" when running the code with "buttons[k].setRolloverIcon(image);"!!!!(the image file is in the same folder as the .java and .class file)
then, I want each button's color to be changed whenever clicked, but when I run the code, I get a "NullPointerException"!!! ->Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ui.GUI.actionPerformed(GUI.java:...)->"this.buttons[k].setBackground(new Color(128,128,128));"
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
!!!!!!
I'd be appreciate if anyone can help as soon as possible.

Attached image(s)

  • Attached Image

This post has been edited by boshra: 08 May 2009 - 01:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: 1. how to set an image for a jbutton 2.how to set a listener in prder

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: 1. how to set an image for a jbutton 2.how to set a listener in prder

Posted 08 May 2009 - 05:34 PM

Please do not post your question in topic title.. it gots truncated

Do

	   Icon image=new ImageIcon("prison break.GIF");
	   System.out.println("Image read: " + image);



if the image was not read it shouldn't print:

Image read: null

This

  buttons[k].setRolloverIcon(image);
  buttons[k]=new JButton(sb,image);



You set button[k] to image
and then you erase all you have in it by having button[k] to point to a new JButton

by the way when was button[k] created ? This is your previous null pointer exception :)

public void actionPerformed(ActionEvent e) {
		this.timesClicked++;
		Integer k=new Integer(0); 
		k = p.get(e.getActionCommand());



To find out which button was pressed:

public void actionPerformed(ActionEvent e) {

	  Object o = e.getSource();
	  int k = 0;
	  for(; k < buttons.length; k++) {
		 if(o == buttons[k]) 
			  break;
	  }
	  buttons[k] is the one that got pressed


This post has been edited by pbl: 08 May 2009 - 05:27 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1