Solved

get a string from a TextField

Posted on 1998-11-18
7
195 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse java  build path 6 45
how do i compare an object based on two fields 6 49
jar file executable 12 44
javap not working 8 39
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 …
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 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

23 Experts available now in Live!

Get 1:1 Help Now