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);
}
}

