Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get Some TEXT (Simple)

Posted on 2004-10-16
6
Medium Priority
?
255 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

Industry Leaders: 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!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

604 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