Results 1 to 15 of 15
Thread: Help regarding Frames
- 02-15-2009, 06:18 AM #1
Member
- Join Date
- Feb 2009
- Location
- India
- Posts
- 23
- Rep Power
- 0
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..
- 02-15-2009, 06:25 AM #2
Member
- Join Date
- Feb 2009
- Posts
- 48
- Rep Power
- 0
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?
-
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.
- 02-15-2009, 06:36 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 48
- Rep Power
- 0
fubarable... you are making me really wana start with this swing thing. you make it sound delicious.
-
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.
- 02-15-2009, 06:55 AM #6
Member
- Join Date
- Feb 2009
- Posts
- 48
- Rep Power
- 0
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.
- 02-15-2009, 07:07 AM #7
Member
- Join Date
- Feb 2009
- Location
- India
- Posts
- 23
- Rep Power
- 0
thanq for replies, yeah i wish to develop in swing too. Mr.Fudarable can u explain me doing with setter and getter methods pls
- 02-15-2009, 07:09 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 48
- Rep Power
- 0
-
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
and same for a Swing program, SwingProgram.java:Java Code:public class SimpleProgram { private int foo; public void setFoo(int f) { foo = f; } public int getFoo() { return foo; } }
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(); } }
- 02-15-2009, 07:26 AM #10
Member
- Join Date
- Feb 2009
- Location
- India
- Posts
- 23
- Rep Power
- 0
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
}
- 02-15-2009, 07:28 AM #11
Member
- Join Date
- Feb 2009
- Posts
- 48
- Rep Power
- 0
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
- 02-15-2009, 07:37 AM #12
Member
- Join Date
- Feb 2009
- Location
- India
- Posts
- 23
- Rep Power
- 0
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);
}
}
- 02-15-2009, 07:41 AM #13
Member
- Join Date
- Feb 2009
- Location
- India
- Posts
- 23
- Rep Power
- 0
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
}
}
- 02-15-2009, 08:00 AM #14
Member
- Join Date
- Feb 2009
- Location
- India
- Posts
- 23
- Rep Power
- 0
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..
-
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 08:20 AM.
Similar Threads
-
navigation b/w frames
By imrankhan in forum AWT / SwingReplies: 3Last Post: 03-19-2012, 12:25 PM -
Grid layout frames GUI
By fritz1474 in forum AWT / SwingReplies: 1Last Post: 10-15-2008, 02:04 AM -
Help needed in Frames text alignment
By ravjot28 in forum Java AppletsReplies: 2Last Post: 06-27-2008, 04:42 PM -
Editing video frames in JMF
By russ2620 in forum NetworkingReplies: 0Last Post: 05-30-2008, 11:26 PM -
will java webBrowser work with frames
By elipford in forum New To JavaReplies: 2Last Post: 06-04-2007, 03:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks