Solved

Get Some TEXT (Simple)

Posted on 2004-10-16
6
252 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 107
How  can  i  resolve  HTTP Status 404 -? 8 78
Java 8 to Java 6 8 43
dao vs facade design patterns 2 69
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

734 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