Solved

Get Some TEXT (Simple)

Posted on 2004-10-16
6
248 Views
Last Modified: 2010-03-31
Hi experts, i am new to java and have looked at some of the tutorials at sun. However i don't seem to be able to get my head around the events in swing. I simply want to allow an input say in a TextField and save that to a variable. This i can do nomally in console with Scanner class. How does one go about it using swing. An example would be appreciated.

Thank you in anticipation

Craig.
0
Comment
Question by:Newt6398
  • 5
6 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12328773
TextField field=new TextField("HelloWorld");

String hello=field.getText();
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12328783

    JTextField textfield = new JTextField("Initial Text");
    int cols = 30;
    textfield = new JTextField("Initial Text", cols);

String text=textfield .getText();
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12328785
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 14

Expert Comment

by:sudhakar_koundinya
ID: 12328797
0
 
LVL 1

Author Comment

by:Newt6398
ID: 12328843
Here is my code. Thanks for the reply i will check the web sites out asap. Gather i don't need to be using Scanner here? Or am i wrong should it also be included??????? Thanx sudhakar 4 help.

public class HelloWorldSwing {

    private static void createAndShowGUI() {

      Scanner input = new Scanner(System.in);

        JFrame.setDefaultLookAndFeelDecorated(true);

        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JTextField textfield = new JTextField("Initial Text");
          int cols = 30;
          textfield = new JTextField("Initial Text", cols);

      String text=textfield .getText();
      
      JLabel Label1 = new JLabel(text);

        frame.getContentPane().add(textfield);

        frame.getContentPane().add(Label1);

        frame.pack();
        frame.setVisible(true);
    }
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 50 total points
ID: 12328873

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class TextFieldTest
    extends JFrame
    implements ActionListener {
  private TextField field = new TextField();
  private JTextField field1 = new JTextField();
  String strFromTextField = null;
  String strFromJTextField = null;

  public TextFieldTest() {

    JPanel panel1 = new JPanel();
    panel1.add(field);
    JPanel panel2 = new JPanel();
    panel1.add(field1);
    this.getContentPane().add(panel1);
    this.getContentPane().add(panel2);
    field.addActionListener(this);
    field1.addActionListener(this);
    setSize(400, 400);

  }

  public void actionPerformed(ActionEvent ae) {
    if (ae.getSource() == field) {
      strFromTextField = field.getText();
      JOptionPane.showMessageDialog(this, strFromTextField);
    }
    else if (ae.getSource() == field1) {
      strFromJTextField = field1.getText();
      JOptionPane.showMessageDialog(this, strFromJTextField);
    }
  }
  public static void main(String s[])
  {
    TextFieldTest test=new TextFieldTest();
    test.show();
   
  }
}
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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

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

19 Experts available now in Live!

Get 1:1 Help Now