Ping Java Peeps

Discussion in 'Java' started by PerfectReign, Sep 11, 2007.

  1. PerfectReign

    PerfectReign Guest

    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


    --
    www.perfectreign.com
    PerfectReign, Sep 11, 2007
    #1
    1. Advertising

  2. PerfectReign wrote:

    > 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
    >

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


    --
    Sabine Dinis Blochberger

    Op3racional
    www.op3racional.eu
    Sabine Dinis Blochberger, Sep 12, 2007
    #2
    1. Advertising

  3. PerfectReign wrote:
    > 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
    > 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



    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.
    RedGrittyBrick, Sep 12, 2007
    #3
  4. PerfectReign

    PerfectReign Guest

    on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick
    took a rock and etched into the cave:

    > PerfectReign wrote:
    >> 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
    >
    >>
    >> 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

    >
    >
    > 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.


    Thanks! Will get started.



    --
    www.perfectreign.com
    PerfectReign, Sep 12, 2007
    #4
  5. PerfectReign

    PerfectReign Guest

    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(){
    > 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.


    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;
    }
    */

    }
    --
    www.perfectreign.com
    PerfectReign, Sep 13, 2007
    #5
  6. PerfectReign wrote:
    >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

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
    Andrew Thompson, Sep 14, 2007
    #6
  7. PerfectReign wrote:
    >
    > 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();


    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.

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


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



    > }
    >
    > ------------------------------------------------------------
    >
    >
    > 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{


    public class frame2 extends JFrame {


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


    remove that line
    // 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);
    >
    > }
    >
    >


    Remove the following method ...
    /*

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


    */
    .... up to here


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



    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
    RedGrittyBrick, Sep 14, 2007
    #7
  8. PerfectReign

    PerfectReign Guest

    on Thursday 13 September 2007 11:37 pm, someone posing as Andrew Thompson
    took a rock and etched into the cave:

    > PerfectReign wrote:
    >>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.


    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
    >


    --
    www.perfectreign.com
    PerfectReign, Sep 14, 2007
    #8
  9. PerfectReign

    Lew Guest

    RedGrittyBrick wrote:
    > 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 ).

    --
    Lew
    Lew, Sep 14, 2007
    #9
  10. PerfectReign

    PerfectReign Guest

    on Friday 14 September 2007 02:47 am, someone posing as RedGrittyBrick took
    a rock and etched into the cave:

    > PerfectReign wrote:
    >>
    >> 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();

    >
    > In the above line you create your first instance of class frame1. You
    > 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")) {
    >>
    >> frametwo.setVisible(true);
    >> }

    >
    > Missing:
    > if (e.getActionCommand().equals("Close")) {
    > lblOne.setText(((JTextField)getSource).getText());
    > }
    >
    >> }


    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:
    > 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


    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.)


    --
    www.perfectreign.com
    PerfectReign, Sep 14, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bernd wegener

    Piping ping into perl-prog

    bernd wegener, Sep 15, 2004, in forum: Perl
    Replies:
    3
    Views:
    655
    Jim Gibson
    Sep 22, 2004
  2. Rahul
    Replies:
    1
    Views:
    2,524
    Thomas Weidenfeller
    Aug 4, 2005
  3. Marc van den Bogaard

    Ping Class java

    Marc van den Bogaard, Sep 30, 2005, in forum: Java
    Replies:
    27
    Views:
    99,901
    steve
    Oct 23, 2005
  4. Replies:
    1
    Views:
    2,007
    IchBin
    Dec 18, 2006
  5. sahm
    Replies:
    13
    Views:
    6,753
    glen herrmannsfeldt
    Dec 7, 2011
Loading...

Share This Page