Solved

Get Some TEXT (Simple)

Posted on 2004-10-16
6
247 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

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
strCount chalenge 3 50
strDist challenge 35 84
firstChar challenge 13 84
Securing Jmx Console and web console 2 47
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

707 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

14 Experts available now in Live!

Get 1:1 Help Now