class to class

Hi,

Hereby the SSCCE of a public class JFrame, composed of a JTextfield and and 3 JPanels (as class).

From the JPanels I can write to the JTextfield of the JFrame, but not to a JTextfield of a JPanel. What do I wrong?

Please look a the code or run it to see what I exactly mean.

Thanks,

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

publicclass testinstanceextends JFrame{

JLabel tekst ;

alfa alf;

beta bet;

gamma gam;

public testinstance (){

tekst =new JLabel();

tekst.setHorizontalAlignment( JLabel.CENTER );

tekst.setText("not pushed");

alfa alf =new alfa(this,bet);

beta bet =new beta(this,alf,gam);

gamma gam =new gamma(this,bet);

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 = 20;

c.fill = GridBagConstraints.BOTH;

ContentPane.add(tekst,c);

c.gridx = 0; c.gridy = 1;

c.gridheight = 20; c.gridwidth = 20;

c.fill = GridBagConstraints.BOTH;

ContentPane.add(alf,c);

c.gridx = 0; c.gridy = 21;

c.gridheight = 20; c.gridwidth = 20;

c.fill = GridBagConstraints.BOTH;

ContentPane.add(alf,c);

c.gridx = 0; c.gridy = 41;

c.gridheight = 20; c.gridwidth = 20;

c.fill = GridBagConstraints.BOTH;

ContentPane.add(alf,c);

GridLayout gr =new GridLayout(4,0);

ContentPane.setLayout(gr);

ContentPane.add(tekst);

ContentPane.add(alf);

ContentPane.add(bet);

ContentPane.add(gam);

}

publicvoid schrijftest(String _text){

tekst.setText(_text);

}

publicstaticvoid main(String[] args){

//Display full window.

testinstance f =new testinstance();

Dimension screenSize =

Toolkit.getDefaultToolkit().getScreenSize();

f.setSize(screenSize);

f.setVisible(true);

f.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

class alfaextends JPanelimplements ActionListener{

JButton knop;

testinstance ti;

beta bet;

public alfa (testinstance ti, beta bet){

setBackground(Color.green);

knop =new JButton("Alfabutton");

this.ti = ti;

this.bet = bet;

FlowLayout fl =new FlowLayout();

setLayout(fl);

this.add(knop);

knop.addActionListener(this);

}

publicvoid actionPerformed(ActionEvent e){

if(e.getSource() == knop){

ti.schrijftest("Button pushed by alfa!");

bet.schrijfbeta("alfa");

}

}

publicvoid keyTyped(KeyEvent e){}

publicvoid keyReleased(KeyEvent e){}

}

class betaextends JPanelimplements ActionListener{

JButton kn;

JTextField tf;

testinstance ti;

alfa alf;

gamma gam;

public beta(testinstance ti,alfa alf, gamma gam){

setBackground(Color.blue);

kn =new JButton("betabutton");

tf =new JTextField("Kiekeboe",15);

this.gam = gam;

this.ti = ti;

this.alf=alf;

FlowLayout flow =new FlowLayout();

setLayout(flow);

this.add(kn);

kn.addActionListener(this);

this.add(tf);

}

publicvoid actionPerformed(ActionEvent e){

if(e.getSource() == kn){

ti.schrijftest("Button pushed by beta!");

tf.setText(ti.tekst.getText());

//gam.schrijfgamma(tf.getText());

gam.schrijfgamma(ti.tekst.getText());

}

}

publicvoid keyTyped(KeyEvent e){}

publicvoid keyReleased(KeyEvent e){}

publicvoid schrijfbeta(String _tekst){

tf.setText(_tekst);

}

}

class gammaextends JPanel{

JTextField gtf;

testinstance ti;

beta bet;

public gamma(testinstance ti, beta bet){

this.ti = ti;

this.bet = bet;

setBackground(Color.yellow);

gtf =new JTextField("gamma",15);

this.add(gtf);

}

publicvoid schrijfgamma(String _tekst){

gtf.setText(_tekst);

}

}

[7435 byte] By [kiekeboea] at [2007-11-27 4:06:45]
# 1

In your TestInstance constructor, you write:

alfa alf = new alfa(this,bet);

beta bet = new beta(this,alf,gam);

gamma gam = new gamma(this,bet);

Realize that you might as well have written:

alfa alf = new alfa(this,null);

beta bet = new beta(this,alf,null);

gamma gam = new gamma(this,bet);

DrLaszloJamfa at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...
# 2
I am a novice...What is the right thing to do?Thanks,
kiekeboea at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...
# 3

I'm not fond of every class knowing every other class, but let's leave that for another day. You could rewrite you code so that this works:

Alfo alf = new Alfa(this);

Beta bet = new Beta(this);

Gamma gam = new Gamma(this);

alf.setBeta(bet);

bet.setAlpha(alf);

bet.setGamma(gam);

gam.setBeta(bet);

DrLaszloJamfa at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...
# 4

I have to write

alfa alf = new alfa(this,bet);

with

alfa alf = new alfa(this)

I get a compiler error.

Same for beta and gamma.

also

alfa.setbeta(bet);

bet.setalpha(alf);

bet.setgamma(gam);

gam.setbeta(bet);

gives error warnings.

There might be another solution?

thanks,

kiekeboea at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...
# 5
> There might be another solution?There's only one solution: fix your code. Clearly, the code I wrote requiresyou to change your constructors and add some new methods. If youare happy with your previous runtime errors, you don't have to change anything.
DrLaszloJamfa at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...
# 6
There's only one solution: fix your code.(...)Yep, wright.If I knew howto, I wasn't posting this question witch is:how do I fix the code?
kiekeboea at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...
# 7

Okay, you have:

alfa alf = new alfa(this,bet);

while I suggest you need to write:

alfa alf = new alfa(this);

...

alf.setBeta(bet);

When you write alfa alf = new alfa(this); you get a syntax error because

alfa's constructor takes two arguments, not one. Now, what do you think

you need to change to fix this? This is not a hard question, even for a beginner. I'm not going to write your code for you.

DrLaszloJamfa at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...
# 8

okay, I guess I can write:

alfa alf = new alfa(this,null);

beta bet = new beta(this,null,null);

gamma gam = new gamma(this,null);

I guess I also have to create 3 methods:

setalfa(stuff) { stuff}

setbeta(stuff) { stuff}

setgamma(stuff) { stuff}

Can you give me a hind how to create these methods ?

Thanks,

kiekeboea at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...
# 9
c'mon, surely you're able to give it a try first before asking again for help. Write an attempt at the method, if it doesn't work; look at the tutorials and revise; if it still doesn't work, post your code. Please don't expect all the work to be done for you.
petes1234a at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...
# 10
Okay, I give up.How should I write the method?I tried variations on:public void setbeta(Object _obj) { _obj = _obj(alf); }Please help me.Thanks,
kiekeboea at 2007-7-12 9:11:54 > top of Java-index,Java Essentials,New To Java...