Results 1 to 5 of 5

Thread: How to assign and retrieve values from JTextField in java swing

  1. #1
    Join Date
    Dec 2011
    Posts
    40

    How to assign and retrieve values from JTextField in java swing

    Hi friends recently I was creating one desktop based application using Swing Technology. So the condition is that whenever my Frame opens the Textfield must have some of the predefine values and also the user can enter the required values into that fields and when click on the submit button, the programs retrieve the values from TextFields and display that in message box. So if you guys have any of the information or any of the suggestion or code for this type of Program then please does the reply.

  2. #2
    Join Date
    Jun 2011
    Posts
    45

    re: How to assign and retrieve values from JTextField in java swing

    According to my information this is easy to assign as well as to retrieve the values from JTextfield in Swing based java programming. So for that you can make use of the predefine method of JTextfield Class.
    The two useful method getText(); and setText(); can be used for getting and setting the values from the Textfield

    So with the help of this two inbuilt method this is possible to assign as well as retrieve the data to/from JTextfield

  3. #3
    Join Date
    Mar 2010
    Posts
    154

    re: How to assign and retrieve values from JTextField in java swing

    According to my information you just try this code may this will help you out.
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    class Abc extends JFrame implements ActionListener
    {
    JButton btn1;
    JTextField txt1,txt2;
    Abc()
    {
    setSize(500,500);
    setDefaultCloseOperation(3);
    setVisible(true);
    setTitle(“Text Field Demo”);
    btn1= new JButton(“Submit”);
    txt1=new JTextField(10);
    txt2= new JTextField(10);
    //Assigning values to both the TextField
    txt1.setText(“Enter the Name Here”);
    txt2.setText(“Enter your Age here”);
    setLayout(new FlowLayout());
    //Adding component to the main Frame
    add(txt1);
    add(txt2);
    btn1.addActionListener(this);
    add(btn1);
    
    }
    public void actionPerformed(ActionEvent ae)
    {
    String s1, s2;
    //Retrieving textField Values
    s1= txt1.getText();
    s2=txt2.getText();
    //Displaying values in Message box
    JOptionPane.showMessageDialog(null,”Your name is ”+ s1+ “/n and your Age is”+s2);
    }
    public static void main(String str[])
    {
    Abc a= new Abc();
    }
    }

  4. #4
    Join Date
    Dec 2011
    Posts
    40

    re: How to assign and retrieve values from JTextField in java swing

    That's great. It works Thanks Menominee

  5. #5
    Join Date
    Jun 2011
    Posts
    266

    Re: How to assign and retrieve values from JTextField in java swing

    In order to retrive values from JTextField use getText() method; and in order to assign value to JTextField make use of setText() method.

Similar Threads

  1. Retrieve multiple values from java class
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 12:05 AM
  2. drawPolygon in java Swing?
    By JagdishP in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 11:33 PM
  3. Java swing help with repaint()
    By grudge in forum Software Development
    Replies: 4
    Last Post: 23-07-2009, 03:41 PM
  4. Use DDE to retrieve values
    By machok in forum Software Development
    Replies: 3
    Last Post: 06-04-2009, 03:28 PM
  5. [JAVA] query results in a combobox and jtextfield
    By Aamin in forum Software Development
    Replies: 5
    Last Post: 31-03-2009, 02:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,057,810.04416 seconds with 17 queries