Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > newbie static question
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 30th August 15:58
biner.sebastien
External User
 
Posts: 1
Default newbie static question



Hello,

I have a question about the following code. I want to draw a simple
rectangle on a JPanel. When I try to compile the program I have the
following message :

*** start message ***
TestGr1.java:19: non-static variable this cannot be referenced from a
static context
PanelGraph graph = new PanelGraph() ;
^
1 error
*** end message ***

I tried to understand what it means, reading the meaning of static
in tutorial and "thinking in java" but I just don't get it. Is there
any java Guru out there that can explain to me in a comprehensible way
why I cant instantiate an object of the PanelGraph class? I get those
static message error a lot.

Just to get some pressure off, I got to say that I am fighting like
hell to stick to learning java in my freetime but I find it is the
most frustrating language I have ever programmed in. I do unix script,
fortran, python, IDL daily, I did Basic, Modula2, Pascal, ... and I
have never been frustrated as I get when I try to do some Java. And
the worst is that I feel dumb because I always read how great that
language is and how clear and easy it is to programm in. Is there a
Java brain part that I am missing or is it just a phase I have to go
through before I can see the light? Just writing about it I get mad
again so I'll stop

Thanks for any help.

*** start of code ****

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

public class TestGr1 {

public static void main(String[] args) {
//
JFrame frame = new JFrame("TestGr1") ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
// creation du panel de base
JPanel panelBase = new JPanel() ;
frame.setContentPane(panelBase) ;

PanelGraph graph = new PanelGraph() ;


panelBase.add(new JLabel("bonjour")) ;
panelBase.add(graph) ;

// on rend le frame visible

frame.pack() ;
frame.setVisible(true) ;

} // fin main

public class PanelGraph extends JPanel{

public void paintComponent (Graphics g){
super.paintComponent(g) ;
g.setColor(Color.orange);
g.fillRect(0,0,50,50) ;
System.out.println("passe dans paint") ;
} // fin paintComponent

} // fin panelGraph

} // fin TestGr1
*** end of code ***
  Reply With Quote


 


2 30th August 15:59
ian shef
External User
 
Posts: 1
Default newbie static question



<Code deleted>

Your code defines class PanelGraph nested within class TestGr1. This is an
"inner class". As such, an instance of PanelGraph can only exist within an
instance of TestGr1, and you don't have any (nowhere is there a "new
TestGr1").

One solution is to not nest PanelGraph within TestGr1. Move it to the end.
You will have to remove the "public" modifier as well:


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

public class TestGr1 {

public static void main(String[] args) {
//
JFrame frame = new JFrame("TestGr1") ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
// creation du panel de base
JPanel panelBase = new JPanel() ;
frame.setContentPane(panelBase) ;

PanelGraph graph = new PanelGraph() ;


panelBase.add(new JLabel("bonjour")) ;
panelBase.add(graph) ;

// on rend le frame visible

frame.pack() ;
frame.setVisible(true) ;

} // fin main

} // fin TestGr1

class PanelGraph extends JPanel{

public void paintComponent (Graphics g){
super.paintComponent(g) ;
g.setColor(Color.orange);
g.fillRect(0,0,50,50) ;
System.out.println("passe dans paint") ;
} // fin paintComponent

} // fin panelGraph

The above will compile without error and is the simplest solution that I
thought of, although not necessarily the best.

If your intent is for PanelGraph to be public, then move it into a separate
file called PanelGraph, add the "public" modifier, and compile it. (This
may involve a new problem having to do with packages, but I didn't test
it.)

There are other solutions. The appropriate solution depends upon your
intentions.

See also:
http://mindprod.com/jgloss/nestedclasses.html
http://mindprod.com/jgloss/static.html


--
Ian Shef
These are my personal opinions and not those of my employer.
  Reply With Quote


 


3 30th August 15:59
raymond decampo
External User
 
Posts: 1
Default newbie static question


One could also make the PanelGraph class static. E.g.:

public class TestGr1
{
// blah blah blah...

public static class PanelGraph
{
// yada yada yada...
}
}

Although if you are new to Java I would recommend starting out by
putting all the classes in their own files. As you gain experience,
start with the fancy inner, nested and anonymous classes.

HTH,
Ray

--
XML is the programmer's duct tape.
  Reply With Quote
4 28th September 11:48
biner.sebastien
External User
 
Posts: 1
Default newbie static question


I forgot to thank you guys.

It now works and I understand a little better.

Thanks.
  Reply With Quote
Reply


Thread Tools
Display Modes




666