Solved

get a string from a TextField

Posted on 1998-11-18
7
198 Views
Last Modified: 2010-03-30
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
0
Comment
Question by:s_lavie
  • 4
  • 2
7 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1227710
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?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227711
why don't you just POST here the code ?
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1227712
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


0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
ID: 1227713
I suppose that want to get the text of "textField". The easiest way is to make it public

----
public class B extends extends com.sun.java.swing.JFrame {
         static final int WIDTH=350;
         static final int HEIGHT=180;
public  JTextField textField;
         public B(String myStr) {
           super(myStr);
.
----

and from class A you can access it

B b =  new B("TRY TEXT");
..
// show b
// wait for user to chose
..
       /* here I need the string from class B - what should I      do???? */
CString st
if (b.textField == NULL)
  st = "";
else
  st = b.textField.getText();

----

Note: It seems that class B should be Dialog - not a Frame, so that when you show the 'b' object you will continue to work in object 'a' only after the user has closed the dialog
the better approach is to access not the textField object but just the siteName string
 ( siteName = textField.getText())
for this you have only to make this String public, so that it is visible from outside ...
(Note: if both classes are from the same package, object of type A will see all fields from object of type B that don't have modifiers (like public, private, protected) - this is called 'friendly behaviour')

if you want a simple source example I can post it here
 
  hope this helps
  heyhey
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1227714
Hi heyhey,

Yes I would like you to post a simple source example
   
Thanks s_lavie  
 
 
 
   
 

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227715
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();
}
}
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227716
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();}});        
}
}


0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 29
more than one jdk and one jre 1 50
going to wrong jsp page 2 33
java mysql insert application 14 25
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question