Solved

get a string from a TextField

Posted on 1998-11-18
7
194 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
Comment Utility
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_
Comment Utility
why don't you just POST here the code ?
0
 
LVL 2

Author Comment

by:s_lavie
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Accepted Solution

by:
heyhey_ earned 50 total points
Comment Utility
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
Comment Utility
Hi heyhey,

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

0
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
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_
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights  challenge 17 60
Fibonacci challenge 11 82
parentbit challenge 3 51
Error with Java/Cache JDBC Classpath 2 19
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now