14 Replies - 1113 Views - Last Post: 15 November 2011 - 06:19 PM Rate Topic: -----

#1 flamewolf393   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-September 11

method not passing across actionlistener interface

Posted 15 November 2011 - 03:08 PM

I need to make a basic GUI with three buttons that changes the background to either red, blue, or green. I have the interface set up just fine, but I cannot get the actionlistener code to work. The error is specifically on the ColorField.setColor in the three listener classes: "ColorField cannot be resolved".

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


public class ColorSelectorTester {

	
	public static void main(String[] args) {
		JFrame frame = new ColorSelector();
	    frame.show();
	
	JButton Rbutton = new JButton("Red");
	JButton Gbutton = new JButton("Green");
	JButton Bbutton = new JButton("Blue");
	JLabel ColorField = new JLabel();
	ColorField.setOpaque(true);


	JPanel panel = new JPanel();
	panel.add(ColorField);
	panel.add(Rbutton);
	panel.add(Gbutton);
	panel.add(Bbutton);
	frame.add(panel);
	
	ActionListener Rlistener = new RButtonListener();
	Rbutton.addActionListene(ColorField);yt
	ActionListener Glistener = new GButtonListener();
	Rbutton.addActionListener(Glistener);
	ActionListener Blistener = new BButtonListener();
	Rbutton.addActionListener(Blistener);
	}

}


import javax.swing.*;
	
class ColorSelector extends JFrame
{
	public ColorSelector() 
	{	setTitle("My Empty Frame");
		setSize(300,200); // default size is 0,0
		setLocation(10,200); // default is 0,0 (top left corner)
	}

		  
}


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class RButtonListener implements ActionListener{

	
	public void actionPerformed(ActionEvent Rbutton) 
	{
		ColorField.setColor(Color.red);

	}
	
}



import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class GButtonListener implements ActionListener{

	
	public void actionPerformed(ActionEvent Gbutton) 
	{
		ColorField.setColor(Color.green);

	}
	
}



import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class BButtonListener implements ActionListener{

	
	public void actionPerformed(ActionEvent Bbutton) 
	{
		ColorField.setColor(Color.blue);

	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: method not passing across actionlistener interface

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 03:12 PM

Its because you cannot access the ColorField label in the separate class.

I would suggest either making anonymous action listeners, or nest the action listeners inside the ColorSelector class itself. That way the action listeners will have access to the label as you want.

Edit : Or you could just implement the ActionListener interface itself in the ColorSelector class and provide the actionPerformed event inside. Will accomplish the same thing.

This post has been edited by Ryano121: 15 November 2011 - 03:14 PM

Was This Post Helpful? 1
  • +
  • -

#3 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 03:18 PM

In addition, your trying to call setColor on a JLabel, when you should be calling JFrame.setBackground(Color).
Was This Post Helpful? 2
  • +
  • -

#4 flamewolf393   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-September 11

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 03:37 PM

@ryano, I tried putting it straight into the main class, and it still gave the exact same problem.

@Blackcompe: I tried that and now the new error is: "Cannot make a static reference to the non-static method setBackground(Color) from the type Component"
Was This Post Helpful? 0
  • +
  • -

#5 flamewolf393   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-September 11

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 04:06 PM

Is anyone reading this? I still need help solving it!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 04:32 PM

You have to invoke the method on the specific object, not the class.

Quote

Is anyone reading this? I still need help solving it!

Your last post was an hour ago. Please be patient. We are all volunteers here and get to things as we are able. :)
Was This Post Helpful? 0
  • +
  • -

#7 flamewolf393   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-September 11

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 04:38 PM

ColorField IS the specific object.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 04:39 PM

Quote

ColorField IS the specific object.

I realize this. You have to invoke the method on ColorField, NOT Component.

Quote

@Blackcompe: I tried that and now the new error is: "Cannot make a static reference to the non-static method setBackground(Color) from the type Component"

Was This Post Helpful? 0
  • +
  • -

#9 flamewolf393   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-September 11

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 04:44 PM

I'm confused. What do you mean component?

I am invoking the method on ColorField aren't I?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 04:45 PM

Obviously not, or you wouldn't be getting that error. Post your revised code and the exact error message from your compiler. I'll be happy to take a look.
Was This Post Helpful? 0
  • +
  • -

#11 flamewolf393   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-September 11

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 04:49 PM

My code is still the same as when originally posted. I tried both earlier suggestions, but when they did not help I reverted back so I would be at the original error again.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 06:09 PM

Both of their suggestions are valid. You should try implementing them before we can really help you more.
Was This Post Helpful? 0
  • +
  • -

#13 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 06:16 PM

Why don't you send the object you'd like to access as an argument to the listener.

public class RButtonListener implements ActionListener{

        private JFrame frame;

        RButtonListener(JFrame frame) {
            this.frame = frame;
	}
}


Was This Post Helpful? 0
  • +
  • -

#14 flamewolf393   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-September 11

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 06:17 PM

I DID try both methods, they did not help any at all! Sorry if I seem impatient, but my homework is due tonight and I cant move on until I solve this issue, and I'm still on the first of three problems!
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: method not passing across actionlistener interface

Posted 15 November 2011 - 06:19 PM

Then post what you tried and the errors for the revised code! Otherwise, we are the blind leading the blind!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1