Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 2nd, 2005, 9:09 PM   #1
TeamElement1
Newbie
 
TeamElement1's Avatar
 
Join Date: Oct 2005
Posts: 8
Rep Power: 0 TeamElement1 is on a distinguished road
noob gui help..

I have a class that implements actionlistener, and I add an actionlistener to a button button.addActionListener(this); it says "cannot use this in a static context"

??
TeamElement1 is offline   Reply With Quote
Old Oct 2nd, 2005, 10:17 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
it means that you can only use this in an object of the class and not the class itself.
OpenLoop is offline   Reply With Quote
Old Oct 3rd, 2005, 2:26 AM   #3
susam_pal
Encoder
 
Join Date: Sep 2005
Location: India
Posts: 12
Rep Power: 0 susam_pal is on a distinguished road
Send a message via Yahoo to susam_pal
TeamElement1, can you please share the code?

I tried the following code:-

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

public class Test
{
	public static void main(String[] args)
	{
		MyFrame frame = new MyFrame();
		frame.show();

	}
}

class MyFrame extends JFrame implements ActionListener
{
	public MyFrame()
	{
		JButton button = new JButton("Ok");
		JPanel panel = new JPanel();
		panel.add(button);
		button.addActionListener(this);
		getContentPane().add(panel);
		pack();
	}

	public void actionPerformed(ActionEvent e)
	{
		System.out.println("button pressed!");
	}

}

its similar to what you say, this code has a button.addActionListener(this) statement. but it compiles and runs successfully!
susam_pal is offline   Reply With Quote
Old Oct 8th, 2005, 1:47 PM   #4
TeamElement1
Newbie
 
TeamElement1's Avatar
 
Join Date: Oct 2005
Posts: 8
Rep Power: 0 TeamElement1 is on a distinguished road
I fixed it, I guess static was where it shouldn't be. I was just being careless
TeamElement1 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC