>on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick
>frame1.java
...
>public class frame1 extends JFrame implements ActionListener{
>
> frame2 frametwo = new frame2(this);
>
> private static frame1 mainForm = new frame1();
..remove this reference to an instance of frame1, then..
...
> public frame1 getMainForm() {
..remove this line, and replace it with..
> return mainForm;
..the following.
// we just need to return the reference to 'this'
return this;
...
Note that using the usual nomenclature helps people to
understand the code, and thereby help you. Common
nomenclature would suggest the names of the classes
should be Frame1 and Frame2, though in any non-test
situation, I am hoping you might come up with better
names than '1' or '2'.
HTH

Signature
Andrew Thompson
http://www.athompson.info/andrew/
PerfectReign - 14 Sep 2007 12:56 GMT
on Thursday 13 September 2007 11:37 pm, someone posing as Andrew Thompson
took a rock and etched into the cave:
>>on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick
>
[quoted text clipped - 22 lines]
> Note that using the usual nomenclature helps people to
> understand the code, and thereby help you.
I hope I was using that.
> Common
> nomenclature would suggest the names of the classes
> should be Frame1 and Frame2, though in any non-test
> situation, I am hoping you might come up with better
> names than '1' or '2'.
Heh - the actual names of the classes I'm trying to figure out how to
integrate are standard names: frmMain, frmOptions, frmGroups, clsData,
clsNews...
...names I've been using since the early '90s when I started doing OO with
Dataflex then VB. :P
these two classes are *very* simplistic so I can ensure the issue is taken
care of in the real project. I don't want to confuse anyone with real
code.
Thanks! I'll try this out...
> HTH

Signature
www.perfectreign.com
> http://donutmonster.com/stuff/2007/20070913_java_frames.jpg
>
[quoted text clipped - 19 lines]
>
> private static frame1 mainForm = new frame1();
In the above line you create your first instance of class frame1. You
don't need mainForm.
Remove the above line
> public static void main(String args[]){
> new frame1();
In the above line you create your *second* instance of class frame1.
The fact you now have two instances of frame1 should be no surprise.
>
> }
[quoted text clipped - 37 lines]
> frametwo.setVisible(true);
> }
Missing:
if (e.getActionCommand().equals("Close")) {
lblOne.setText(((JTextField)getSource).getText());
}
> }
Your IDE should tell you the following method is never used. I;d remove
it, its just a pointless confusing distraction to clutter your example
with unused methods.
> public frame1 getMainForm() {
> return mainForm;
> }
> }
>
[quoted text clipped - 15 lines]
>
> public class frame2 extends JFrame implements ActionListener{
public class frame2 extends JFrame {
> JTextField txtOne = new JTextField(20);
> static ActionListener listener; // = new ActionListener();
remove that line
// static ActionListener listener; // = new ActionListener();
>
>
[quoted text clipped - 26 lines]
>
>
Remove the following method ...
/*
> public void actionPerformed(ActionEvent e) {
> //if (e.getActionCommand().equals("Close")) {
[quoted text clipped - 5 lines]
> setVisible(false);
> }
*/
... up to here
> /* public Form1 getFrame1(){
> return this.frame1;
[quoted text clipped - 5 lines]
>
> }
Other notes.
When posting code, just remove any commented out code. It doesn't help
us understand your current problem (I understand why you comment bits
out, I do it too, but I clean it out when posting).
Please follow usual Java conventions when writing code for posting. It
makes it easier for people like me to speed-read code if your classes
have names starting with a capital letter. "frame1" looks like an
instance name. I'd write "Frame1" instead.
Generally when comparing a String variable to a String constant it is
usually safer (from NPE) to write it the other way around:
if ("Close".equals(e.getActionCommand()))
It probably doesn't matter, but if I intend to use the results of
e.getActionCommand() more than once, I always do
String command = e.getActionCommand() and use the command variable.
It is useful to create contants to be used as ActionCommands. There is
less chance of having "Close" in one place and "close" in another.
1)
public static final String CLOSE = "Close";
...
new JButton(CLOSE);
...
if (CLOSE.equals(command)) { ...
2) Better
Use an enum
3) Best?
Use Actions
I know its a concocted example but I find variable names like lblone and
blah make comprehension more difficult. I'd invent something based on
some typical teaching example like recording names and addresses, or
students and courses, or shops and products, ...
Hope that helps
Lew - 14 Sep 2007 13:28 GMT
> Generally when comparing a String variable to a String constant it is
> usually safer (from NPE) to write it the other way around:
> if ("Close".equals(e.getActionCommand()))
The only problem with that approach is that it removes null as an out-of-band
value and makes it equivalent to, say, "". If the null-ness actually
mattered, you would check for the value being null first, then compare to the
test value.
Or more generally you'd write
String command = e.getActionCommand();
if ( command != null && command.equals( "Close" ))
This lets you treat null as in-band for now but easily change to out-of-band
later, and maintains the code's self-documentary quality in explicitly showing
that you didn't ignore null.
Remember, ! "".equals( null ).

Signature
Lew
PerfectReign - 14 Sep 2007 16:22 GMT
on Friday 14 September 2007 02:47 am, someone posing as RedGrittyBrick took
a rock and etched into the cave:
>> http://donutmonster.com/stuff/2007/20070913_java_frames.jpg
>>
[quoted text clipped - 23 lines]
> don't need mainForm.
> Remove the above line
Yep! I eventually figured that one out.
(Not bad for a manager, IMO!)
<sniP>
>> if (e.getActionCommand().equals("Click Me")) {
>>
[quoted text clipped - 7 lines]
>
>> }
Okay, will add and see how it goes.
> Your IDE should tell you the following method is never used
I'm not using an IDE. Just the text editor, Kate. It has java syntax
highlighting.
> Remove the following method ...
Okay, will do.
<snip>
> Other notes.
>
> When posting code, just remove any commented out code.
Okay, will do. My apologies.
> Please follow usual Java conventions when writing code for posting. It
> makes it easier for people like me to speed-read code if your classes
> have names starting with a capital letter. "frame1" looks like an
> instance name. I'd write "Frame1" instead.
Will do. I realize now my class names don't follow Java conventions.
> Generally when comparing a String variable to a String constant it is
> usually safer (from NPE) to write it the other way around:
[quoted text clipped - 25 lines]
>
> Hope that helps
Yes, it does. Thank you. (Actually lblOne is designed to show it is a
label. I always use the prefix in my variable in my real coding.)

Signature
www.perfectreign.com