Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Newbie Question...GUI Text input Areas

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Newbie Question...GUI Text input Areas

    I am still VERY new to java and programming in general and spend most of my time comparing my code with the example code my teacher provides me with.

        jtfMessage.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            jlblMessage.setText(jtfMessage.getText());
            jtfMessage.requestFocusInWindow();
          }

    Here is a line of code from one my EXAMPLE scripts I am trying to reproduce. BUT in my assignment I want my text box to be receiving a INT not a String. So instead of "jtf.Message.getText();" I would like to imagine I could just type in getINT or something simple but that is obviously not the case. How can I get a int?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Newbie Question...GUI Text input Areas

    want my text box to be receiving a INT not a String.
    Read the API doc for the class to see what data types the methods use.
    http://docs.oracle.com/javase/7/docs/api/
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    steme (April 2nd, 2013)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Newbie Question...GUI Text input Areas

    JTextField - which I'm guessing these jtfMessage things are - are designed to allow the user to enter and edit text. If you intend to use them for int values then the (conceptually) simplest thing to do is to use getText() and then parse the string that it returns using Integer.parseInt() or the Integer.toString(i) method as mentioned in Oracle's Tutorial.

    Of course you may decide you want to do something is the text in the field does not make sense as an integer. This involves the extra complexity of using try/catch.

    Java does provide the JFormattedTextField subclass of JTextField for dealing with the display and editing of objects other than strings. But, again, this involves you in more complexity.

    ---

    If what you want to do is move from dealing with examples involving text fields holding strings to those associated with int values, I suggest you investigate Integer.parseInt() and Integer.toString(i) and modify a simple example you already understand. Post if you get stuck.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    steme (April 2nd, 2013)

  6. #4
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Newbie Question...GUI Text input Areas

    Quote Originally Posted by steme View Post
    Here is a line of code from one my EXAMPLE scripts I am trying to reproduce. BUT in my assignment I want my text box to be receiving a INT not a String. So instead of "jtf.Message.getText();" I would like to imagine I could just type in getINT or something simple but that is obviously not the case. How can I get a int?
    Easiest way that you can do in 1 chained line or a few lines is to first keep jtf.Message.getText() as that retrieves and represents the value as a String object. To get that into a primitive int, you should first use the int's wrapper class (Integer), which "converts" it from a String object to an Integer object. Next, the .parseInt() method basically parses the Integer object as a primitive int. The one problem with this is this conversion is vulnerable to the user entering inappropriate data, such as letters, symbols, nothing at all, decimals or really big integers. These can be addressed though by try-catch blocks or throwing exceptions.

  7. The Following User Says Thank You to SunshineInABag For This Useful Post:

    steme (April 2nd, 2013)

Similar Threads

  1. newbie Question.....
    By Haseo in forum Java Theory & Questions
    Replies: 1
    Last Post: August 31st, 2012, 07:52 AM
  2. newbie Question.....
    By Haseo in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 22nd, 2012, 08:10 AM
  3. Newbie Question
    By mmazur in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 15th, 2012, 10:15 PM
  4. Looping Question (newbie)
    By xdrechsler in forum Loops & Control Statements
    Replies: 13
    Last Post: July 19th, 2010, 09:12 PM
  5. newbie GUI/ event handling problem
    By zyspt in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 10th, 2010, 11:36 AM