Results 1 to 15 of 15
  1. #1
    ramesh.8189 is offline Member
    Join Date
    Feb 2009
    Location
    India
    Posts
    23
    Rep Power
    0

    Default Help regarding Frames

    Hey guys,
    This s my problem.

    Lets assume we've two frames. First one has three text fields and a button. second frame has three labels which initially has no text. now when the program is run only the first frame should be displayed and after entering values in that text field and clicking the button,the second frame should get displayed with the text in labels set to the text in text fields of first frame. Hope u can understand.

    now my attempts were,

    Implemented action listener in second frame class and passed that class as argument for Button's addActionListener. then I tried to write action performed in Second frame prog, but i got stuck how to get values from that text field.

    Can somebody help????

    Alternatively I tried using all the text fields and that button in a seperate file as panel added that panel to this frame, but now too i dono how to get the values from other class file and pass it to other file.

    Help pls..

  2. #2
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    let me try to understand what you are doing here...

    you have one frame with 3 text boxes and a button.

    you write int he 3 boxes and you press the button and it writes what was in the boxes on 3 labels. is that correct?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    better to show a small compilable program that demonstrates what you're trying to do. To keep it simple perhaps only use two windows.

    Are you using Swing or AWT? It sounds like AWT given your use of the terms Frame and Text field. If so, I suggest that you move up to the more powerful and robust Swing.

  4. #4
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    fubarable... you are making me really wana start with this swing thing. you make it sound delicious.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Yeah, as you can tell, I'm a die-hard Swing aficionado.

    to ramesh, again if swing, you should create classes that produce JPanels rather than JFrames or dialogs. I'd create a class that creates the second JPanel and give it 3 public setter methods to allow me to set the text in the three JTextFields. Then the first "frame" can create the JPanel, set its textfields via the setter methods, place it in a JFrame (or JDialog), call setVisible(false) on itself, and setVisible(true) on the new JFrame/JDialog, and you're done.

  6. #6
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    it sounds like hes learning alot of the same stuff as me.

    if i was you.. (with out using swing) return what was typed in the boxes. put them in a string. and print the string in the labels.

    how to code it.. i couldnt tell you exactly but im sure theres a java tutorial on it somewhere.

  7. #7
    ramesh.8189 is offline Member
    Join Date
    Feb 2009
    Location
    India
    Posts
    23
    Rep Power
    0

    Default

    thanq for replies, yeah i wish to develop in swing too. Mr.Fudarable can u explain me doing with setter and getter methods pls

  8. #8
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    better to show a small compilable program that demonstrates what you're trying to do. To keep it simple perhaps only use two windows.
    it would help.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You use setters and getters in Swing the same as you would for any other Java program. For e.g.,

    for a simple program, SimpleProgram.java
    Java Code:
    public class SimpleProgram
    {
      private int foo;
      
      public void setFoo(int f)
      {
        foo = f;
      }
      
      public int getFoo()
      {
        return foo;
      }
    }
    and same for a Swing program, SwingProgram.java:
    Java Code:
    import javax.swing.JTextField;
    
    public class SwingProgram
    {
      private JTextField foo = new JTextField(12);
      
      public void setFooText(String text)
      {
        foo.setText(text);
      }
      
      public String getFooText()
      {
        return foo.getText();
      }
    }

  10. #10
    ramesh.8189 is offline Member
    Join Date
    Feb 2009
    Location
    India
    Posts
    23
    Rep Power
    0

    Default

    Ok Fudarable, but how can I get that text from some other class

    Consider the snippet below:

    // This comes in second class
    public void actionPerformed(ActionEvent ae)
    {
    //in this we can get the button object by get Source() method. //But how can we take that and get the text in text fields in //that panel
    }

  11. #11
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    the best way would be to .gettext w/e what was in the boxes and = them to a string and just call the strings in the next frame

  12. #12
    ramesh.8189 is offline Member
    Join Date
    Feb 2009
    Location
    India
    Posts
    23
    Rep Power
    0

    Default

    Here s my first frame

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class FirstFrame extends JFrame
    {
    JTextField t1,t2,t3;
    JButton b;
    public FirstFrame()
    {
    setLayout(new FlowLayout());
    t1=new JTextField(10);
    t2=new JTextField(10);
    t3=new JTextField(10);
    b=new JButton("OK");
    add(t1);
    add(t2);
    add(t3);
    add(b);
    b.addActionListener(new SecondFrame());
    }
    public static void main(String a[])
    {
    FirstFrame ff=new FirstFrame();
    ff.setSize(400,400);
    ff.setVisible(true);
    }
    }

  13. #13
    ramesh.8189 is offline Member
    Join Date
    Feb 2009
    Location
    India
    Posts
    23
    Rep Power
    0

    Default

    my second frame

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class SecondFrame extends JFrame

    implements ActionListener
    {
    JLabel l1,l2,l3;
    public SecondFrame()
    {
    l1=new JLabel("");
    l2=new JLabel("");
    l3=new JLabel("");
    }
    public void actionPerformed(ActionEvent ae)
    {
    this.setVisible(true);
    //Im stuck here
    }
    }

  14. #14
    ramesh.8189 is offline Member
    Join Date
    Feb 2009
    Location
    India
    Posts
    23
    Rep Power
    0

    Default

    guys Im expecting replies..

    I ve made some modifications..

    Changed the first frame as panel

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class FirstPanel extends JPanel implements ActionListener
    {
    JTextField t1,t2,t3;
    JButton b;
    String s1,s2,s3;
    public FirstPanel()
    {
    setLayout(new FlowLayout());
    t1=new JTextField(10);
    t2=new JTextField(10);
    t3=new JTextField(10);
    b=new JButton("OK");
    add(t1);
    add(t2);
    add(t3);
    add(b);
    b.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae)
    {
    s1=t1.getText();
    s2=t2.getText();
    s3=t3.getText();
    }
    /*public static void main(String a[])
    {
    FirstFrame ff=new FirstFrame();
    ff.setSize(400,400);
    ff.setVisible(true);
    }*/
    }


    Then changed second class as

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class SecondFrame extends JFrame //implements ActionListener
    {
    JLabel l1,l2,l3;
    FirstPanel fp;
    public SecondFrame()
    {
    setLayout(new GridLayout(4,1));
    fp=new FirstPanel();
    l1=new JLabel(fp.s1);
    l2=new JLabel(fp.s2);
    l3=new JLabel(fp.s3);
    add(fp);
    add(l1);
    add(l2);
    add(l3);
    }
    /*public void actionPerformed(ActionEvent ae)
    {
    this.setVisible(true);
    //Im stuck here
    }*/
    public static void main(String a[])
    {
    SecondFrame sf=new SecondFrame();
    sf.setVisible(true);
    sf.setSize(400,400);
    }
    }
    Im not developer.. Only Java develops me..

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ramesh.8189 View Post
    guys Im expecting replies..
    This can be considered as quite a rude thing to say. Why so impatient, not to mention that all of us are volunteers??

    I'm hoping some of this is due to a language problem, but I'm going to suggest (as has been suggested before) that you go through the basic Sun Java tutorials and the Swing tutorials. There you will learn the correct way to do Swing coding. It will take some time and effort, but there are no short cuts here. If you are trying to develop, then you now are a developer and will have to pay your dues.

    And one more thing: I strongly suggest you not use your second class as the actionlistener for the first. Doing this will only force you to make further bad design choices. Create a separate actionlistener class either an anonymous inner class or a private inner class. In this listener create an instance of the second class, and with this call the setters etc...

    You've got some work cut out for you, and hopefully you'll learn some patience too. You'll need it.
    Last edited by Fubarable; 02-15-2009 at 09:20 AM.

Similar Threads

  1. navigation b/w frames
    By imrankhan in forum AWT / Swing
    Replies: 3
    Last Post: 03-19-2012, 01:25 PM
  2. Grid layout frames GUI
    By fritz1474 in forum AWT / Swing
    Replies: 1
    Last Post: 10-15-2008, 03:04 AM
  3. Help needed in Frames text alignment
    By ravjot28 in forum Java Applets
    Replies: 2
    Last Post: 06-27-2008, 05:42 PM
  4. Editing video frames in JMF
    By russ2620 in forum Networking
    Replies: 0
    Last Post: 05-31-2008, 12:26 AM
  5. will java webBrowser work with frames
    By elipford in forum New To Java
    Replies: 2
    Last Post: 06-04-2007, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •