|
-
February 23rd, 2009, 06:28 PM
#1
non-static variable this cannot be referenced from a static context??
The code below is returning the following error :
"non-static variable this cannot be referenced from a static context" on line 25 (i have added notation to the code below to show where the error is...
all help will be greatly appreciated, i am new to GUI development, and this has me stumped.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class BinarySearchTreeApp
{
public static void main (String[]args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();}});}
private static void createAndShowGUI()
{
JFrame Frame1 = new JFrame("My Application");
Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame1.setSize(500,500);
Frame1.setVisible(true);
Frame1.add(new MyPanel()); <----ERROR IS ON THIS LINE!
}
class MyPanel extends JPanel
{
public MyPanel()
{
setBorder(BorderFactory.createLineBorder(Color.black));
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
}
}
-
February 23rd, 2009, 07:23 PM
#2
Re: non-static variable this cannot be referenced from a static context??
The problem is that the class MyPanel is an inner class of class BinarySearchTreeApp, and it's not declared static, but you're trying to create an instance of it from a static method.
Inner classes can be a little tricky at times, but in this case it's fairly simple - your MyPanel inner class behaves like a member variable of BinarySearchTreeApp in that it can be declared static or non-static, and if it's non-static it can't be accessed from a static method. As it is, you'd need to create it from an instance (non-static) method.
To prevent the error, either declare the MyPanel class static, or create your instance of it from a non-static method.
Better still, avoid using an inner class until you understand how to use them. They're not that difficult, but there are several variations of them and a fair amount to remember, so you're better off learning how to use them first. Until then either declare the MyPanel class in a separate file, or outside the scope of the BinarySearchTreeApp class (i.e. outside the curly braces).
If you don't understand the difference between static and non-static, read up about it asap - it is crucial.
The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time...
T. Cargill
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
February 23rd, 2009, 07:43 PM
#3
Re: non-static variable this cannot be referenced from a static context??
ok, so the best bet is to separate it into two separate files... this is going to sound really dumb... but how do i do that?
-
February 23rd, 2009, 07:48 PM
#4
Re: non-static variable this cannot be referenced from a static context??
oh wait, i just got it up and running there with two separate classes, thanks a lot!! you have saved me so much trouble! one last thing? how do i add other components onto MyPanel? can you help me with that?
-
February 24th, 2009, 07:38 AM
#5
Re: non-static variable this cannot be referenced from a static context??
If you want to add components to your panel (which is, after all, what it is for), you'll need to keep a reference to it instead of creating it as you add it to the frame, e.g.
Code:
JPanel panel = new MyPanel();
panel.add(component);
...
frame.add(panel);
You might want to keep the frame reference as a class field of BinarySearchTreeApp, so it's visible anywhere in the class, like this:
Code:
public class BinarySearchTreeApp {
static JFrame frame; // class field to hold the frame
...
private static void createAndShowGUI() {
...
frame = new JFrame(..);
...
}
}
It's also usual to add and lay out all the components in the panels and frames before calling setVisible(). After this point, the event thread becomes active, all the existing components display themselves and the GUI waits for user input. Adding components, panels, and frames subsequently should work, but you may see flickering as they get initialised and displayed.
I recommend reading the Java Swing Tutorial, which will guide you through creating a Swing GUI and how to use the components and lay them out in panels & frames, etc.
The sooner you start to code, the longer the program will take...
R. Carlson
Last edited by dlorde; February 24th, 2009 at 08:03 AM.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|