The Artima Developer Community
Sponsored Link

Java Answers Forum
applet with 2 panels with cardLayout, need to pass parameter from p1 to p2

2 replies on 1 page. Most recent reply: Oct 10, 2004 8:25 AM by Tienshan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Tienshan

Posts: 7
Nickname: tienshan
Registered: Oct, 2004

applet with 2 panels with cardLayout, need to pass parameter from p1 to p2 Posted: Oct 9, 2004 12:29 PM
Reply to this message Reply
Advertisement
Hi All!

<p>
I have an applet with two panels, A110 and A150, set up with card layout. A110 and A150 are instantiated (from separate classes) and added in that order in the applet. I need to pass some parameters from A110 to A150 and do certain things (like make a textField editable, etc.) with the received parameter.
<p>

For example, A110 has 2 buttons, btnUpdate and btnDisplay. When I call the applet A110Main, the first child (A110) is displayed. With a click on any button, I can switch to the second child(A150).

<p>
I want to make the textField of A150 editable when I click btnUpdate.
<p>

I don't know how to do it because panel A150 has already been created (just not shown )with initial paramers from the applet(A110Main). How do I gain access to the second child's components from the first child? How do I repaint the component, to make it editable? Do I have a design flaw from the very beginning? At the very least, I do need to have two panels and control one panel from the other.
<p>

Does anyone have some suggestion?
<p>

Thanks.
<p>

Here is my code

<pre>
import java.applet.Applet;
import java.awt.*;

public class A110Main extends Applet {

Container cont = null;
CardLayout clayout = null;
Panel cpanel = null;


A110 p110 = null;
A150 p150 = null;


String dispString = "I am non-editable";

public void init() {

clayout = new CardLayout();
cpanel = new Panel();
cpanel.setLayout(clayout);

p110 = new A110();
Panel jp1 = (Panel)p110.showPage( cpanel);


p150 = new A150(dispString);
Panel jp2 = (Panel)p150.showPage( cpanel );

cpanel.add("1",jp1);
cpanel.add("2",jp2);

add(cpanel);

this.setBackground(Color.lightGray);

}

}

import java.awt.*;
import java.awt.event.*;

public class A110 {

public A110 () {}

Button btnUpdate;
Button btnDisplay;
Panel cPanel;

public Component showPage(Panel _cPanel) {

this.cPanel = _cPanel;

Panel p110 = new Panel();
p110.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
p110.setBackground(Color.lightGray);


btnUpdate = new Button("Update");
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
p110.add(btnUpdate, c);

btnUpdate.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){

((CardLayout)cPanel.getLayout()).next(cPanel);

}
});

Button btnDisplay = new Button("Display Only");
c.gridx = 2;
c.gridy = 0;
p110.add(btnDisplay, c);


btnDisplay.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
((CardLayout)cPanel.getLayout()).next(cPanel);
}
});


return p110;

}


}

import java.awt.*;
import java.awt.event.*;

public class A150 {

String displayString = "";

public A150 (String _displayString) {
this.displayString = _displayString;

}

public Component showPage(Panel _cPanel) {

Panel p150 = new Panel();
p150.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
p150.setBackground(Color.lightGray);


final Panel cPanel = _cPanel;

Button btnBack = new Button("Back");
c.gridx = 0;
c.gridy = 0;
p150.add(btnBack, c);

btnBack.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
((CardLayout)cPanel.getLayout()).next(cPanel);
}
});

Label lblDummy = new Label();
c.gridx = 1;
c.gridy = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
p150.add(lblDummy, c);


TextField txtDisp = new TextField(40);
c.gridx = 0;
c.gridy = 2;
p150.add(txtDisp, c);

txtDisp.setText(displayString);
txtDisp.setEditable(false);


return p150;

}



}

</pre>


Tienshan

Posts: 7
Nickname: tienshan
Registered: Oct, 2004

Re: applet with 2 panels with cardLayout, need to pass parameter from p1 to p2 Posted: Oct 9, 2004 12:37 PM
Reply to this message Reply
I messed the formatting and I am sorry. It did look alright when I previewed it. Believe me! In fact, when I put [pre][(slash pre], the square brackets, it didn't look alright, everything was like flowlayout!! A firm believer in WYSWYG, I am embarassed!

Tienshan

Posts: 7
Nickname: tienshan
Registered: Oct, 2004

Re: applet with 2 panels with cardLayout, need to pass parameter from p1 to p2 Posted: Oct 10, 2004 8:25 AM
Reply to this message Reply

Hello All!

Two answers have been proposed by paulcw, 74philip and Michael_Dunn and both work fine.


First proposal:
(1)pass a reference to class A150 instance, so A110 instance can access fields in class A150.
A110 p110 = new A110(p150);

(2) inside my actioListner of btnAdd
a150.txtDisp.setEditable(true);

Second proposal:

(1) inside my actioListner of btnAdd
((A110Main)cPanel.getParent()).p150.txtDisp.setEditable(true);

Thank you paulcw, 74philip and Michael_Dunn and all for reading my post.

Best of Octobers,
tienshan.

Flat View: This topic has 2 replies on 1 page
Topic: Simple array equality question (hopefully!) Previous Topic   Next Topic Topic: date in xml, java & mysql(help me pelase)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use