Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

instance of classes

Thread view: 
dendeezen - 13 Apr 2007 07:18 GMT
Hi,

Several days ago I asked the same question . Hereby now some code to
explain what I mean.

Working is:

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

public class testinstance extends JFrame{

    JLabel tekst ;
    basis base;
        public testinstance () {

            tekst = new JLabel();
            tekst.setHorizontalAlignment( JLabel.CENTER );
            tekst.setText("not pushed");

            base = new basis(this);;

            Container ContentPane = this.getContentPane();
            GridBagLayout gridbag = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            ContentPane.setLayout(gridbag);

            c.gridx = 0; c.gridy = 0;
            c.gridheight = 1; c.gridwidth = 5;
            c.weightx = 1; c.weighty = 1;
            c.fill = GridBagConstraints.BOTH;
            ContentPane.add(tekst,c);

            c.gridx = 0; c.gridy = 1;
            c.gridheight = 5; c.gridwidth = 5;
            c.weightx = 2; c.weighty = 2;
            c.fill = GridBagConstraints.BOTH;
            ContentPane.add(base,c);

                    GridLayout gr = new GridLayout(2,0);
                    ContentPane.setLayout(gr);
                    ContentPane.add(tekst);
                    ContentPane.add(base);
            }

    public static void main(String[] args) {
        testinstance f = new testinstance();
              //Display full window.
        Dimension screenSize =
          Toolkit.getDefaultToolkit().getScreenSize();
       f.setSize(screenSize);
       f.setVisible(true);
       f.setDefaultCloseOperation(EXIT_ON_CLOSE);
   }

   public void schrijftekst(String _text) {
     tekst.setText(_text);
   }
}
class basis extends JPanel{

    basis(testinstance ti) {
        GridLayout grid = new GridLayout(3,0);
        setLayout(grid);
        geg g = new geg(ti);
        beta b = new beta();
        this.add(g);
        this.add(b);
    }
}
class geg extends JPanel implements ActionListener {

    JButton knop;
    testinstance ti;

    public geg (testinstance ti) {

        setBackground(Color.green);
        knop = new JButton("Push");

        this.ti = ti;
        FlowLayout fl = new FlowLayout();
        setLayout(fl);
        this.add(knop);
        knop.addActionListener(this);
        }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == knop) {
            ti.schrijftekst("Button pushed!");
        }
    }
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
}

class beta extends JPanel {
    JButton kn;
    public beta() {
        setBackground(Color.blue);
        kn = new JButton("betabutton");

        FlowLayout flow = new FlowLayout();
        setLayout(flow);
        this.add(kn);
    }
}

When I try to instantiate the classes geg en beta (in 'real life'
public classes)in each other ( to exchange data) I got in trouble in
the main method from the class testinstance . The code:

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

public class testinstance extends JFrame{

    JLabel tekst ;
    private basis base;
        public testinstance (basis ba) {

            tekst = new JLabel();
            tekst.setHorizontalAlignment( JLabel.CENTER );
            tekst.setText("not pushed");

            this.base = ba;

            Container ContentPane = this.getContentPane();
            GridBagLayout gridbag = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            ContentPane.setLayout(gridbag);

            c.gridx = 0; c.gridy = 0;
            c.gridheight = 1; c.gridwidth = 5;
            c.weightx = 1; c.weighty = 1;
            c.fill = GridBagConstraints.BOTH;
            ContentPane.add(tekst,c);

            c.gridx = 0; c.gridy = 1;
            c.gridheight = 5; c.gridwidth = 5;
            c.weightx = 2; c.weighty = 2;
            c.fill = GridBagConstraints.BOTH;
            ContentPane.add(base,c);

                    GridLayout gr = new GridLayout(2,0);
                    ContentPane.setLayout(gr);
                    ContentPane.add(tekst);
                    ContentPane.add(base);
            }

    public static void main(String[] args) {

        // ' this ' is not allowed in a static !
        //this.base = base;
        //the constructor basis() is undefined
        basis ba = new basis();
        // but, what is the solution?
        testinstance f = new testinstance(ba);
              //Display full window.
        Dimension screenSize =
          Toolkit.getDefaultToolkit().getScreenSize();
       f.setSize(screenSize);
       f.setVisible(true);
       f.setDefaultCloseOperation(EXIT_ON_CLOSE);
   }

   public void schrijftekst(String _text) {
     tekst.setText(_text);
   }
}
class basis extends JPanel{

    basis(testinstance ti, beta be, geg ge) {
        GridLayout grid = new GridLayout(3,0);
        setLayout(grid);
        geg g = new geg(ti, be);
        beta b = new beta(ge);
        this.add(g);
        this.add(b);
    }
}
class geg extends JPanel implements ActionListener {

    JButton knop;
    testinstance ti;
    beta be;
    public geg (testinstance ti, beta be) {

        setBackground(Color.green);
        knop = new JButton("Push");

        this.ti = ti;
        this.be = be;
        FlowLayout fl = new FlowLayout();
        setLayout(fl);
        this.add(knop);
        knop.addActionListener(this);
        }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == knop) {
            ti.schrijftekst("Button pushed!");
        }
    }
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
}

class beta extends JPanel {
    JButton kn;
    geg ge;
    public beta(geg ge) {
        setBackground(Color.blue);
        kn = new JButton("betabutton");
        this.ge = ge;

        FlowLayout flow = new FlowLayout();
        setLayout(flow);
        this.add(kn);
    }
}

Thanks for helping a newbie,
Andrew Thompson - 13 Apr 2007 07:44 GMT
..
>Several days ago I asked the same question .

It might have made more sense to continue the
discussion on that thread, but since this post has
all relevant information (though the question was
cleverly hidden!) I will continue it here.

>..Hereby now some code ..

Self contained examples, no less!   :-)

>..to explain what I mean.

'Let the code do the talking'?  I am comfortable
with that.

>Thanks for helping a newbie,

Note that a good group for those starting Java
is comp.lang.java.help, this group is better suited
to folks that have more complex problems.

But to your code.

When compiling, I got the error 'cannot find symbol
symbol  : constructor basis()'  that confused me for
a few moments, as I expected it to get a 'default
constructor' but a bit of googling later suggested to me
that the default constructor is *only* created when
there are *no* other constructors defined - and the
basis class already had one that took three arguments.

The 'simplest solution' is to explicitly add a 'no args.'
constructor, but note that is *probably* not the entire
solution.  The compilable form of the basis class, is
as follows..

<snippet>
class basis extends JPanel{

   basis() {
   }

   basis(testinstance ti, beta be, geg ge) {
       GridLayout grid = new GridLayout(3,0);
       setLayout(grid);
       geg g = new geg(ti, be);
       beta b = new beta(ge);
       this.add(g);
       this.add(b);
   }
}
</snippet>

HTH

Signature

Andrew Thompson
http://www.athompson.info/andrew/

dendeezen - 13 Apr 2007 08:10 GMT
> .
>
[quoted text clipped - 58 lines]
>
> Message posted viahttp://www.javakb.com

Dear Andrew,

I tried some 'newbie' groups, but could not get a solution...

As you could see, I already wrote the compilable form of the basis
class.

regards,

Dendeezen
Andrew Thompson - 13 Apr 2007 08:34 GMT
..
>I tried some 'newbie' groups, but could not get a solution...

Please refrain from 'full-quoting'.

But two notes:
- I see I was wrong about posting to c.l.j.help,
you already are!
- My web interface to usenet showed this as
a new post, hence mysuggestion about putting
this on the old thread - but again, you *did* do that.
..my apologies for any confusion my comments
might have  caused.

Signature

Andrew Thompson
http://www.athompson.info/andrew/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.