s_lavie
asked on
get a string from a TextField
The problem is this:
We have class A and class B - both extends JFrame class.
class A instantiates class B which set a textField.
Now we want class A to get that string (from textField),
and disposing class B.
At the moment we get an empty string.
How can we do it?
Please add an example.
Thanks
We have class A and class B - both extends JFrame class.
class A instantiates class B which set a textField.
Now we want class A to get that string (from textField),
and disposing class B.
At the moment we get an empty string.
How can we do it?
Please add an example.
Thanks
why don't you just POST here the code ?
ASKER
Here is the relevant code:
public class A extends com.sun.java.swing.JFrame {
.
.
.
new B("TRY TEXT");
/* here I need the string from class B - what should I do???? */
.
.
.
}
public class B extends extends com.sun.java.swing.JFrame {
static final int WIDTH=350;
static final int HEIGHT=180;
JTextField textField;
public B(String myStr) {
super(myStr);
getContentPane().setLayout (new BorderLayout(0,30));
setBackground(Color.lightG ray);
JPanel textPanel = new JPanel();
textPanel.setBorder (
BorderFactory.createEtched Border());
textPanel.setLayout(new BorderLayout(0,6));
JLabel textTitle = new JLabel("Open Site ... ");
Font fancyFont = new Font("Serif", Font.BOLD | Font.ITALIC, 12);
textTitle.setFont(fancyFon t);
Icon textIcon = new ImageIcon("www.gif");
textTitle.setIcon(textIcon );
textTitle.setHorizontalAli gnment(JLa bel.LEFT);
textPanel.add(textTitle, BorderLayout.NORTH);
textField = new JTextField();
textPanel.add(textField, BorderLayout.SOUTH);
buttonOK = new com.sun.java.swing.JButton ("",new ImageIcon ("yes.gif"));
buttonOK.setToolTipText("O K");
buttonOK.setBounds(20,120, 15,15);
buttonCancel = new com.sun.java.swing.JButton ("",new ImageIcon ("no.gif"));
buttonCancel.setToolTipTex t("Cancel" );
buttonCancel.setBounds(120 ,120,15,15 );
JPanel listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
listPanel.add(buttonOK,"We st");
listPanel.add(buttonCancel ,"East");
textField.addActionListene r(new ActionListener() {
public void actionPerformed(ActionEven t e) {
siteName = textField.getText();
}
});
Container c = getContentPane();
c.add(textPanel);
c.add(listPanel, BorderLayout.SOUTH);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
this.setSize(this.WIDTH, this.HEIGHT);
this.setVisible(true);
}
}
Thanks
public class A extends com.sun.java.swing.JFrame {
.
.
.
new B("TRY TEXT");
/* here I need the string from class B - what should I do???? */
.
.
.
}
public class B extends extends com.sun.java.swing.JFrame {
static final int WIDTH=350;
static final int HEIGHT=180;
JTextField textField;
public B(String myStr) {
super(myStr);
getContentPane().setLayout
setBackground(Color.lightG
JPanel textPanel = new JPanel();
textPanel.setBorder (
BorderFactory.createEtched
textPanel.setLayout(new BorderLayout(0,6));
JLabel textTitle = new JLabel("Open Site ... ");
Font fancyFont = new Font("Serif", Font.BOLD | Font.ITALIC, 12);
textTitle.setFont(fancyFon
Icon textIcon = new ImageIcon("www.gif");
textTitle.setIcon(textIcon
textTitle.setHorizontalAli
textPanel.add(textTitle, BorderLayout.NORTH);
textField = new JTextField();
textPanel.add(textField, BorderLayout.SOUTH);
buttonOK = new com.sun.java.swing.JButton
buttonOK.setToolTipText("O
buttonOK.setBounds(20,120,
buttonCancel = new com.sun.java.swing.JButton
buttonCancel.setToolTipTex
buttonCancel.setBounds(120
JPanel listPanel = new JPanel();
listPanel.setLayout(new BorderLayout());
listPanel.add(buttonOK,"We
listPanel.add(buttonCancel
textField.addActionListene
public void actionPerformed(ActionEven
siteName = textField.getText();
}
});
Container c = getContentPane();
c.add(textPanel);
c.add(listPanel, BorderLayout.SOUTH);
this.addWindowListener(new
public void windowClosing(WindowEvent e) {
dispose();
}
});
this.setSize(this.WIDTH, this.HEIGHT);
this.setVisible(true);
}
}
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi heyhey,
Yes I would like you to post a simple source example
Thanks s_lavie
Yes I would like you to post a simple source example
Thanks s_lavie
hope this is what you need
heyhey
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class A extends Frame
{
Button but;
public A()
{
super("a");
but = new Button("press for Dialog");
but.addActionListener(new ActionListener()
{public void actionPerformed (ActionEvent e) {
B b = new B(A.this);
b.setModal(true);
b.pack();
b.show();
String valA;
if (b.val == NULL)
valA = "";
else
valA = b.val;
System.out.println(valA);
}});
add(but);
}
public static void main (String args[])
{
A a = new A();
a.setSize(100,100);
a.addWindowListener(new WindowAdapter() {public void windowClosing (WindowEvent e) { System.exit(0);}});
a.show();
}
}
heyhey
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class A extends Frame
{
Button but;
public A()
{
super("a");
but = new Button("press for Dialog");
but.addActionListener(new ActionListener()
{public void actionPerformed (ActionEvent e) {
B b = new B(A.this);
b.setModal(true);
b.pack();
b.show();
String valA;
if (b.val == NULL)
valA = "";
else
valA = b.val;
System.out.println(valA);
}});
add(but);
}
public static void main (String args[])
{
A a = new A();
a.setSize(100,100);
a.addWindowListener(new WindowAdapter() {public void windowClosing (WindowEvent e) { System.exit(0);}});
a.show();
}
}
i have missed to post some code
here is the whole example
(do you need more help ?)
---- start of code ----
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class A extends Frame
{
Button but;
public A()
{
super("a");
but = new Button("press for Dialog");
but.addActionListener(new ActionListener()
{public void actionPerformed (ActionEvent e) {
B b = new B(A.this);
b.setModal(true);
b.pack();
b.show();
String valA;
if (b.val == NULL)
valA = "";
else
valA = b.val;
System.out.println(valA);
}});
add(but);
}
public static void main (String args[])
{
A a = new A();
a.setSize(100,100);
a.addWindowListener(new WindowAdapter() {public void windowClosing (WindowEvent e) { System.exit(0);}});
a.show();
}
}
class B extends Dialog {
public String val;
TextField id;
Label enter;
Button b;
public B (Frame f)
{
super (f);
setLayout(new GridLayout(3,1));
enter = new Label("enter");
b = new Button ("press");
id = new TextField();
add(enter);
add(id);
add(b);
b.addActionListener(new ActionListener() {public void actionPerformed (ActionEvent e) { val = id.getText(); hide();}});
addWindowListener(new WindowAdapter() {public void windowClosing (WindowEvent e) { hide();}});
}
}
here is the whole example
(do you need more help ?)
---- start of code ----
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class A extends Frame
{
Button but;
public A()
{
super("a");
but = new Button("press for Dialog");
but.addActionListener(new ActionListener()
{public void actionPerformed (ActionEvent e) {
B b = new B(A.this);
b.setModal(true);
b.pack();
b.show();
String valA;
if (b.val == NULL)
valA = "";
else
valA = b.val;
System.out.println(valA);
}});
add(but);
}
public static void main (String args[])
{
A a = new A();
a.setSize(100,100);
a.addWindowListener(new WindowAdapter() {public void windowClosing (WindowEvent e) { System.exit(0);}});
a.show();
}
}
class B extends Dialog {
public String val;
TextField id;
Label enter;
Button b;
public B (Frame f)
{
super (f);
setLayout(new GridLayout(3,1));
enter = new Label("enter");
b = new Button ("press");
id = new TextField();
add(enter);
add(id);
add(b);
b.addActionListener(new ActionListener() {public void actionPerformed (ActionEvent e) { val = id.getText(); hide();}});
addWindowListener(new WindowAdapter() {public void windowClosing (WindowEvent e) { hide();}});
}
}
How does B define that text field (public or not) and how A is supposed to get hold of it (using an instance variable or using a method)? Why can't you get the text field content? What do you expect there other than an empty string (that is who populated the field)? What does disposing of B have to do with the text field value?
How can you do what?
May be a snippet of your code?