?
Solved

get a string from a TextField

Posted on 1998-11-18
7
Medium Priority
?
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 16

Accepted Solution

by:
heyhey_ earned 150 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

800 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