7 Replies - 1105 Views - Last Post: 13 June 2012 - 12:57 PM Rate Topic: -----

#1 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Java JButton Array

Posted 12 June 2012 - 03:10 PM

I have 30 JButtons, i have put these in an array and then are changing Button icons in the Action listener class made for the timer
basicly every so often an icon on a button will change
although, im quite new to java and im getting problems with using the array in the other class

public class name
{
  JButton btn_M1, btn_M2, btn_M3, btn_M4, btn_M5;
  JButton[] array_Buttons={btn_M1, btn_M2, btn_M3, btn_M4, btn_M5};

  Icon icon_x = new ImageIcon(getClass().getResource("x.png"));
  Icon icon_y = new ImageIcon(getClass().getResource("y.png"));
}
public name
{
  //setting up all the buttons here
  btn_M1 = new JButton("",icon_x);
  btn_M1.addActionListener(this);
  btn_M1.setBounds(ButtonXCo, ButtonYCo+60, ButtonSize,ButtonSize);
  add(btn_M1);
}
public class TimerListener
{
  public void actionPerformed(ActionEvent Action) 
	{

	  array_Buttons[5].setIcon(icon_y);//errors here

        ]
}





i get errors when it hits the set Icon, the icon is fine etc
just where do i need to put the array of buttons or what do i need to set it as to be able to access it, as it dosnt seem to be.
its a run time error, null pointer exception?

sorry if thats not very clear, thanks for the advice

Is This A Good Question/Topic? 0
  • +

Replies To: Java JButton Array

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,438
  • Joined: 20-September 08

Re: Java JButton Array

Posted 12 June 2012 - 03:17 PM

Quote

array_Buttons[5]


Out of bounds. Arrays are zero indexed. Last button is 4
Was This Post Helpful? 1
  • +
  • -

#3 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Re: Java JButton Array

Posted 12 June 2012 - 03:21 PM

View Postg00se, on 12 June 2012 - 03:17 PM, said:

Quote

array_Buttons[5]


Out of bounds. Arrays are zero indexed. Last button is 4


sorry ive got 30 buttons, just didn't add them to the code there.
Thanks
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Java JButton Array

Posted 12 June 2012 - 08:39 PM

Your code shouldn't compile for more than one reason. Run your code through a compiler first-off.

If you have an array, you don't need the variables btn_M1, btn_M2, etc. They're all null, as you haven't initialized them. So array_Buttons holds null elements as well.
  JButton btn_M1, btn_M2, btn_M3, btn_M4, btn_M5;
  JButton[] array_Buttons={btn_M1, btn_M2, btn_M3, btn_M4, btn_M5};



Regardless if you have 5 or 30 elements, an array and a loop is much cleaner.
JButton[] buttons = new JButton[30];
for(int i = 0; i < buttons.length; i++){
    buttons[i] = new JButton();
    buttons[i].addActionListener(actionListenerVariable);
}



Here, this should be the constructor. It must be declared inside the name class, and public name should have parentheses after name since the constructor is a method: public void name().
public name
{
  //setting up all the buttons here
  btn_M1 = new JButton("",icon_x);
  btn_M1.addActionListener(this);
  btn_M1.setBounds(ButtonXCo, ButtonYCo+60, ButtonSize,ButtonSize);
  add(btn_M1);
}



And here: array_Buttons[5].setIcon(icon_y);, since icon_y is in another class and is non-static, you will have to access it from the name class. Unless you intended for the TimerListener class to be an inner class of name.
Was This Post Helpful? 1
  • +
  • -

#5 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Re: Java JButton Array

Posted 13 June 2012 - 05:35 AM

View Postmacosxnerd101, on 12 June 2012 - 08:39 PM, said:

Your code shouldn't compile for more than one reason. Run your code through a compiler first-off.

If you have an array, you don't need the variables btn_M1, btn_M2, etc. They're all null, as you haven't initialized them. So array_Buttons holds null elements as well.
  JButton btn_M1, btn_M2, btn_M3, btn_M4, btn_M5;
  JButton[] array_Buttons={btn_M1, btn_M2, btn_M3, btn_M4, btn_M5};



Regardless if you have 5 or 30 elements, an array and a loop is much cleaner.
JButton[] buttons = new JButton[30];
for(int i = 0; i < buttons.length; i++){
    buttons[i] = new JButton();
    buttons[i].addActionListener(actionListenerVariable);
}



Here, this should be the constructor. It must be declared inside the name class, and public name should have parentheses after name since the constructor is a method: public void name().
public name
{
  //setting up all the buttons here
  btn_M1 = new JButton("",icon_x);
  btn_M1.addActionListener(this);
  btn_M1.setBounds(ButtonXCo, ButtonYCo+60, ButtonSize,ButtonSize);
  add(btn_M1);
}



And here: array_Buttons[5].setIcon(icon_y);, since icon_y is in another class and is non-static, you will have to access it from the name class. Unless you intended for the TimerListener class to be an inner class of name.


Thats not my actual code, mine does compile
but i do know whats wrong now i made the array of buttons null...
ive moved the array creation down to the bottom of the constructer
now how do i use this array in the class for the TimerListener? thanks
Was This Post Helpful? 0
  • +
  • -

#6 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Re: Java JButton Array

Posted 13 June 2012 - 11:35 AM

I need help accessing a JButton array, ive made it in the constructer after all the buttons
how do i access it in the class TimerListener, trying to use "array_Buttons[5].setIcon(icon_Mole);" and it errors
Its a simple game to click buttons and the icon of the button changes on the timer events
Im quite new to java, so any advice would be helpfull, thanks

public class WhackAMole extends JFrame implements ActionListener
{
	int GameScore = 0;
	int TimeLeft = 99;
	
	int NextMole = 0;
	int CurrentMole = 0;
	
	private boolean Palying = false;
	
	JLabel lbl_Title, lbl_Score, lbl_Time;
	JButton btn_Start;
	private Timer GameTimer;
	Random Random = new Random();
	
	//JButton[] array_Buttons= new JButton[31];
	JButton btn_M1, btn_M2, btn_M3, btn_M4, btn_M5;// up to 30
	
        //icons
	Icon icon_Mole = new ImageIcon(getClass().getResource("Mole.png"));
	Icon icon_NoMole = new ImageIcon(getClass().getResource("NoMole.png"));
		
	
	public WhackAMole()
	{
		super("Whack a diglett");
		setLayout(null);

		//Timer
		GameTimer = new Timer(1000, new TimerListener());
		
		//Labels
		lbl_Title = new JLabel("Whack them!");
		lbl_Title.setFont(new Font("serif",Font.PLAIN, 30));
		lbl_Title.setBounds(110,10, 170, 30);
		add(lbl_Title);
		
		lbl_Score = new JLabel("Score: "+GameScore);
		lbl_Score.setFont(new Font("serif",Font.PLAIN, 20));
		lbl_Score.setBounds(20,40, 170, 30);
		add(lbl_Score);
		
		lbl_Time = new JLabel("Time: "+TimeLeft);
		lbl_Time.setFont(new Font("serif",Font.PLAIN, 20));
		lbl_Time.setBounds(280,40, 170, 30);
		add(lbl_Time);
		
		//Buttons
		int ButtonXCo = 20;
		int ButtonYCo = 20;
		int ButtonSize = 50;
		
		btn_Start = new JButton("Start");
		btn_Start.setFont(new Font("serif",Font.PLAIN, 15));
		btn_Start.setBounds(150,50, 70, 20);
		btn_Start.addActionListener(this);
		add(btn_Start);
		
		//Top Row
		btn_M1 = new JButton("",icon_NoMole);
		btn_M1.addActionListener(this);
		btn_M1.setBounds(ButtonXCo, ButtonYCo+60, ButtonSize,ButtonSize);
		add(btn_M1);
		
		btn_M2 = new JButton("",icon_NoMole);
		btn_M2.addActionListener(this);
		btn_M2.setBounds(ButtonXCo+60, ButtonYCo+60, ButtonSize,ButtonSize);
		add(btn_M2);
		
		btn_M3 = new JButton("",icon_NoMole);
		btn_M3.addActionListener(this);
		btn_M3.setBounds(ButtonXCo+120, ButtonYCo+60, ButtonSize,ButtonSize);
		add(btn_M3);
		
	        //up to 30 buttons
				
		JButton[] array_Buttons={btn_M1, btn_M2, btn_M3, btn_M4, btn_M5;//30

		}

	public void actionPerformed(ActionEvent Action) 
	{
		if(Action.getSource()==btn_Start)
		{ 

			GameTimer.start();
		}
	}
	
	public static void main(String args[])
	{
		WhackAMole WhackAMole = new WhackAMole();
		WhackAMole.setVisible(true);
		WhackAMole.setSize(400,420);
		WhackAMole.setDefaultCloseOperation(EXIT_ON_CLOSE);
				
	}
	
	public class TimerListener implements ActionListener
	{
		
		public void actionPerformed(ActionEvent Action) 
		{
			array_Buttons[5].setIcon(icon_Mole);//Errors Here
		}
		
	}
}




Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10563
  • View blog
  • Posts: 39,087
  • Joined: 27-December 08

Re: Java JButton Array

Posted 13 June 2012 - 12:12 PM

Duplicate threads merged. Please avoid duplicate posting.

Because you declared the array in the constructor, it cannot be accessed outside of the constructor. It is called a local variable. You should declare the array as an instance variable and get rid of the button_M1, button_M2, etc., variables as I suggested in my last post.
Was This Post Helpful? 1
  • +
  • -

#8 podypodpod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 21-January 11

Re: Java JButton Array

Posted 13 June 2012 - 12:57 PM

View Postmacosxnerd101, on 13 June 2012 - 12:12 PM, said:

Duplicate threads merged. Please avoid duplicate posting.

Because you declared the array in the constructor, it cannot be accessed outside of the constructor. It is called a local variable. You should declare the array as an instance variable and get rid of the button_M1, button_M2, etc., variables as I suggested in my last post.


Sorry about the repost, thought posting my actual code would be better
Thanks a lot, ive actually got it working now :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1