Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-18-2007, 11:39 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Need help with JButton event
I'm trying to code an JButton and cannot get the code to compile correctly. When I add the action listener to the button I get an error that non-static variable this cannot be referenced from a static context. I'm also having problems with the method that is setup to handle the event. Here is my code:

Code:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; import java.awt.*; public class GuessNumber implements ActionListener{ public static void main(String[] args) { JButton okButton; JLabel msgLabel; final JTextField answerTextField; JPanel panel; String askForNum = "Please enter what you think the random number is below."; // generates randomNumber between 1 and 100 int randomNumber; int max = 100; Random random = new Random(); randomNumber = random.nextInt(max +1); // output of random number for testing purposes remove prior to submission System.out.println(randomNumber); // creates JLabel that asks user to guess what the random number is and enter it below msgLabel = new JLabel(askForNum, JLabel.CENTER); msgLabel.setSize(400, 150); msgLabel.setFont(new Font("Times New Roman", Font.BOLD, 14)); answerTextField = new JTextField(); answerTextField.setFont(new Font("Times New Roman", Font.PLAIN, 14)); answerTextField.setSize(75, 30); answerTextField.setVisible(true); okButton = new JButton("Ok"); okButton.setSize(30, 30); okButton.setFont(new Font("Times New Roman", Font.PLAIN, 14)); okButton.addActionListener(this); panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(msgLabel); panel.add(answerTextField); panel.add(okButton); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(panel); f.setSize(400, 200); f.setVisible(true); } public void actionPerformed(ActionEvent e){ if(e.getSource() == okButton){ } } }
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-19-2007, 12:29 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
Code:
C:\jexp>javac gn.java gn.java:40: non-static variable this cannot be referenced from a static context okButton.addActionListener(this); ^ gn.java:58: cannot find symbol symbol : variable okButton location: class GN if(e.getSource() == okButton){ ^ 2 errors
1 — You cannot use the "this" keyword in static context, ie, inside a method whose signature includes the static modifier or with a field/variable that is declared static. You can use a reference variable:
Code:
public class GN implements ActionListener { static JButton okButton; // member variable has class scope public static void main(String[] args) { GN gn = new GN(); // reference to the enclosing class // JButton okButton; // local variable ... okButton.addActionListener(gn);
2 — the JButton "okButton" is declared inside a method (as a local variable) and thus cannot be seen inside the actionPerformed method. To be seen inside actionPerformed "okButton" must be in class scope, ie, declared as a member variable.

When making guis it is more elegant to put them together outside the main method.
Code:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.*; import java.awt.*; public class GN implements ActionListener{ JButton okButton; private JPanel getContent() { String askForNum = "Please enter what you think the random number is below."; // generates randomNumber between 1 and 100 int randomNumber; int max = 100; Random random = new Random(); randomNumber = random.nextInt(max +1); // output of random number for testing purposes remove prior to submission System.out.println(randomNumber); // creates JLabel that asks user to guess what the random // number is and enter it below JLabel msgLabel = new JLabel(askForNum, JLabel.CENTER); msgLabel.setSize(400, 150); msgLabel.setFont(new Font("Times New Roman", Font.BOLD, 14)); JTextField answerTextField = new JTextField(); // The designers recommend that we specify a columns argument // to help establish the size of a JTextField, eg, new JTextField(16) // The setSize method is of little value until after realization. // Swing components are made to work well with preferredSize. // So another option is: Dimension d = answerTextField.getPreferredSize(); d.width = 75; answerTextField.setPreferredSize(d); // Or answerTextField.setPreferredSize(new Dimension(75, 30)); // Choice of parent layout manager has a lot to do with how // this will be shown/handled. answerTextField.setFont(new Font("Times New Roman", Font.PLAIN, 14)); // answerTextField.setSize(75, 30); // answerTextField.setVisible(true); okButton = new JButton("Ok"); okButton.setSize(30, 30); okButton.setFont(new Font("Times New Roman", Font.PLAIN, 14)); okButton.addActionListener(this); // "this" is okay now JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(msgLabel); panel.add(answerTextField); panel.add(okButton); return panel; } public void actionPerformed(ActionEvent e){ if(e.getSource() == okButton){ } } public static void main(String[] args) { GN gn = new GN(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(gn.getContent()); f.setSize(400, 200); f.setVisible(true); } }

Last edited by hardwired : 11-19-2007 at 12:30 AM. Reason: fix typo
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-19-2007, 02:15 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Thanks Hardwired appreciate the help
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JButton onClick? Joe2003 AWT / Swing 2 01-06-2008 04:04 PM
Help with JButton and layout adlb1300 AWT / Swing 1 12-25-2007 09:33 AM
mouse over on JButton gradon Java Applets 1 08-04-2007 06:50 AM
Few action in one Jbutton kubiasty New To Java 0 07-25-2007 11:19 AM
Problem with JButton Marcus AWT / Swing 1 07-05-2007 06:56 AM


All times are GMT +3. The time now is 10:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org