Link to home
Start Free TrialLog in
Avatar of s_lavie
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
Avatar of msmolyak
msmolyak

Could you clarify what the problem is.

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?
why don't you just POST here the code ?
Avatar of s_lavie

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

    JPanel textPanel = new JPanel();

    textPanel.setBorder (
      BorderFactory.createEtchedBorder());

    textPanel.setLayout(new BorderLayout(0,6));

    JLabel textTitle = new JLabel("Open Site ... ");
     
    Font fancyFont = new Font("Serif", Font.BOLD | Font.ITALIC, 12);
    textTitle.setFont(fancyFont);
    Icon textIcon = new ImageIcon("www.gif");
    textTitle.setIcon(textIcon);
    textTitle.setHorizontalAlignment(JLabel.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("OK");
            buttonOK.setBounds(20,120,15,15);
        buttonCancel = new com.sun.java.swing.JButton("",new ImageIcon ("no.gif"));
            buttonCancel.setToolTipText("Cancel");
            buttonCancel.setBounds(120,120,15,15);

    JPanel listPanel = new JPanel();

    listPanel.setLayout(new BorderLayout());
    listPanel.add(buttonOK,"West");
    listPanel.add(buttonCancel,"East");

    textField.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent 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


ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of s_lavie

ASKER

Hi heyhey,

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