Welcome to Dream.In.Code
Become a Java Expert!

Join 150,058 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,729 people online right now. Registration is fast and FREE... Join Now!




Getting values in JTextField

 
Reply to this topicStart new topic

Getting values in JTextField

skin_
8 Jun, 2008 - 03:08 AM
Post #1

New D.I.C Head
*

Joined: 5 Apr, 2008
Posts: 33

Hey guys,
I'm just going over some tutorials and I'm stuck on this one. You're meant to create a GUI that takes two integers, then tells the user if they are divisible. This is what I have so far:
CODE
import javax.swing.JFrame;

public class UGUIDriver
{
    public static void main(String args[])
    {        
        JFrame frame = new JFrame("Divisors..");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        DivisorsUser panel = new DivisorsUser();
        frame.getContentPane().add(panel);
        
        frame.pack();
        frame.setVisible(true);
    }
}

and
CODE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DivisorsUser extends JPanel {
    
    private JButton process;
    private JTextField number, divisor, result;
    private JLabel num, div;
    
    public DivisorsUser()
    {
        num = new JLabel("Enter a number: ");
        div = new JLabel("Number to test for divisor: ");
        number = new JTextField(10);
        divisor = new JTextField(10);
        result = new JTextField(30);
        process = new JButton("Process");
        process.addActionListener(new ButtonListener());
        
        add(num);
        add(number);
        add(div);
        add(divisor);
        add(result);
        
        setBackground(Color.white);
        setPreferredSize(new Dimension(300, 200));
    }
    
    private class ButtonListener implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            if(e.getSource() == process)
            {
                number.getText();
                divisor.getText();
                
                if (number%divisor == '0')
                {
                    result.setText("Number is divisable");
                }
                else if (number%divisor != '0')
                {
                    result.setText("Number is NOT divisable");
                }
            }
        }
    }
}

I probably haven't done it the easiest way, but I'm very new to GUIs.
The problem is that I'm not sure on how to get values that are in JTextFields.
Thanks for the help. ph34r.gif
User is offlineProfile CardPM
+Quote Post

mensahero
RE: Getting Values In JTextField
8 Jun, 2008 - 04:05 AM
Post #2

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
JTextField numBox = new JTextField();
float number;

number = Float.valueOf(numBox.getText());

thats should do it.. I think..
blink.gif

This post has been edited by mensahero: 8 Jun, 2008 - 04:07 AM
User is offlineProfile CardPM
+Quote Post

fsloke
RE: Getting Values In JTextField
8 Jun, 2008 - 08:34 AM
Post #3

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
In GUI form, you will get the text in String only

If you want to use integer,
you need convert it into integer before you do any calculation

number.getText() will return String

When you want to display back the integer after you do calculation

You simply add ""
Example
int sum=0;
number.setText(sum+"");
User is offlineProfile CardPM
+Quote Post

mensahero
RE: Getting Values In JTextField
8 Jun, 2008 - 08:58 AM
Post #4

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
QUOTE(fsloke @ 8 Jun, 2008 - 09:34 AM) *

In GUI form, you will get the text in String only

If you want to use integer,
you need convert it into integer before you do any calculation

number.getText() will return String

When you want to display back the integer after you do calculation

You simply add ""
Example
int sum=0;
number.setText(sum+"");


I not sure about that bro.. IMO that will indeed "set the text" to the JTextField.. but if he wants to retrieve the values.. it will still return a string.. its better if he cast the .getText into a float before he do some calculations..

and lastly.. I don't get it.. sorry I'm such a n00b.. how does this run..

CODE

number.setText(sum+"");


add the value of sum to a blank string >>""<< .... blink.gif

This post has been edited by mensahero: 8 Jun, 2008 - 08:59 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Getting Values In JTextField
8 Jun, 2008 - 05:42 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
number and divisor are TextFiled not numbers
as you want to check if they divie I assume you want the integer in them

CODE

private class ButtonListener implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
          if(e.getSource() == process)
          {
                 int num1 = 0, num2 = 0;
                 try {
                       num1 = Integer.parseInt(number.getText());
                       num2 = Integer.parseInt(divisor.getText());
                 }
                 catch (NumberFormatException ne) {
                        System.out.println("one of the tw is not an integer");
                        return;    // bye bye
                 }
                
                 if (num1%num2 == 0)
                       result.setText("Number is divisable");
                 else
                        result.setText("Number is NOT divisable");
                }
          }
}

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month