Solved

Get Some TEXT (Simple)

Posted on 2004-10-16
6
251 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 32
HashTable highest marks enumeration alternative 9 44
login jsp example 24 69
printf performancy 11 73
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 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