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 / September 2007

Tip: Looking for answers? Try searching our database.

Ping Java Peeps

Thread view: 
PerfectReign - 11 Sep 2007 23:17 GMT
Anybody want to help out with this one?

I have two JFrames. I want to update a control on one with the information
from another. The first jframe launches the second. the second has a text
box. I then want the results of the text box applied to a JLabel on the
first.

Ideas?

Here's the code:

http://www.donutmonster.com/viewtopic.php?p=43#43

Signature

www.perfectreign.com

Sabine Dinis Blochberger - 12 Sep 2007 09:29 GMT
> Anybody want to help out with this one?
>
[quoted text clipped - 8 lines]
>
> http://www.donutmonster.com/viewtopic.php?p=43#43

Without looking at the code, you can override the constructor of frame2
to include a JFrame as a parameter.

Signature

Sabine Dinis Blochberger

Op3racional
www.op3racional.eu

RedGrittyBrick - 12 Sep 2007 11:17 GMT
> Anybody want to help out with this one?

Why set followups to alt.2600?
Why set followups to alt.2600 only?

Seen & replied to in comp.lang.java.programmer

> I have two JFrames. I want to update a control on one with the information
> from another. The first jframe launches the second. the second has a text
[quoted text clipped - 6 lines]
>
> http://www.donutmonster.com/viewtopic.php?p=43#43

In frame1.java change
    frame2 frametwo = new frame2();
to
    frame2 frametwo = new frame2(this);

in the ActionListener add
    if (e.getActionCommand().equals("Close")) {
       if (e.getSource() instanceof JButton) {
          txtOne.setText(((JButton)e.getSource()).getText());
       }
    }

In frame2.java change
    public frame2(){
to
    public frame2(ActionListener listener){

change
    button2.addActionListener(this);
to
    button2.addActionListener(listener);

Above coding style based on that used in OP's source  :-(
UNTESTED - CAVEAT EMPTOR

There's probably better ways to do it and there's lots of other
improvements I think you could and should make to your code. For
example, you could pass an Action instead of an ActionListener, this
would make the code a bit cleaner. I hope the above helps you get started.
PerfectReign - 12 Sep 2007 15:28 GMT
on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick
took a rock and etched into the cave:

>> Anybody want to help out with this one?
>
> Why set followups to alt.2600?
> Why set followups to alt.2600 only?

My newsreader (Knode) did that. Also, if I don't check c.l.j.p in a few
months, I'll see the reply in a.2600.

> Seen & replied to in comp.lang.java.programmer
>
[quoted text clipped - 38 lines]
> example, you could pass an Action instead of an ActionListener, this
> would make the code a bit cleaner. I hope the above helps you get started.

Thanks! Will get started.

Signature

www.perfectreign.com

PerfectReign - 13 Sep 2007 22:13 GMT
on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick
took a rock and etched into the cave:

> In frame2.java change
>      public frame2(){
[quoted text clipped - 13 lines]
> example, you could pass an Action instead of an ActionListener, this
> would make the code a bit cleaner. I hope the above helps you get started.

Thank you - I did this and ended up with something bizarre.

http://donutmonster.com/stuff/2007/20070913_java_frames.jpg

The first frame is loaded twice.

Of course, then the button on the second frame doesn't seem to work.

Ideas??

I'll post the code inline, since the classes are not that long.

frame1.java

import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class frame1 extends JFrame implements ActionListener{
       
       frame2 frametwo = new frame2(this);

       private static frame1 mainForm = new frame1();

       public static void main(String args[]){
               new frame1();
               
       }

       public frame1(){
               
               frametwo.setVisible(false);
               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               Container content = getContentPane();
               content.setBackground(Color.white);
               content.setLayout(new FlowLayout());
               JButton button1 = new JButton("Click Me");
               button1.addActionListener(this);

               JLabel lblOne = new JLabel("                  ");

               lblOne.setBorder(new LineBorder(Color.green, 3));

               content.add(lblOne);

               content.add(button1);

               JButton button2 = new JButton("Exit");
               button2.addActionListener( this );
               content.add(button2);
               this.setSize(300, 300);

               setVisible(true);
               
       }

       public void actionPerformed(ActionEvent e) {
               if (e.getActionCommand().equals("Exit")) {

       
                       System.exit(0);
               }
       
               if (e.getActionCommand().equals("Click Me")) {
       
                       frametwo.setVisible(true);
               }
       }

       public frame1 getMainForm() {
               return mainForm;
       }
}

------------------------------------------------------------

frame2.java

/*
       Java test for multi-class update.
       
       This is the second frame called from the first.
*/

import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class frame2 extends JFrame implements ActionListener{

       JTextField txtOne = new JTextField(20);
       static ActionListener listener; // = new ActionListener();
       

       public static void main(String args[]){
               new frame2( listener );

               
       }

       public frame2( ActionListener listener ){
               
               setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
               Container content = getContentPane();
               content.setLayout(new FlowLayout());
               

               content.add(txtOne);

               JButton button2 = new JButton("Close");
               //button2.addActionListener(new ButtonListener());
               //button2.addActionListener(this);
               button2.addActionListener(listener);
               
               content.add(button2);

               this.setSize(300, 300);
               setVisible(true);
               
       }
       
       
       public void actionPerformed(ActionEvent e) {
       //if (e.getActionCommand().equals("Close")) {
               
               String blah = this.txtOne.getText();

               System.out.println(blah);
//              frame1.lblOne.setText(blah);
               setVisible(false);
       }

/*      public Form1 getFrame1(){
               return this.frame1;
       }
       public void setFrame1(Form1 frame1){
               this.frame1 = frame1;
       }
*/

}
Signature

www.perfectreign.com

Andrew Thompson - 14 Sep 2007 07:37 GMT
>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

RedGrittyBrick - 14 Sep 2007 10:47 GMT
> 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



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.