Results 1 to 7 of 7
  1. #1
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default JFrame Text field - Reset if value doesnt equal int?

    Am programming my GUI and need to reset the text field if the user enters a string.

    Part of my current code:

    Java Code:
    private void diameterTextActionPerformed(java.awt.event.ActionEvent evt) {                                             
            // TODO add your handling code here:
        }
    Java Code:
    private void DiamFocus(java.awt.event.FocusEvent evt) {
            int d = Integer.parseInt(diameterText.getText());
            if (d >= 1 && d <= 20) {
            } else if (d >= 21 || d <= 0) {
                diameterText.setText("0");
            }
        }
    How can I setText back to 0 if user enters anything apart from numbers 1-20.

    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: JFrame Text field - Reset if value doesnt equal int?

    What happens when you execute your code and try it with different values?

  3. #3
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default Re: JFrame Text field - Reset if value doesnt equal int?

    If i enter a string, i get:

    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "sdfsdf"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at pipesrus.GUI.LengthFocus(GUI.java:300)
    at pipesrus.GUI.access$400(GUI.java:17)
    at pipesrus.GUI$5.focusLost(GUI.java:144)
    at java.awt.AWTEventMulticaster.focusLost(AWTEventMul ticaster.java:213)
    at java.awt.Component.processFocusEvent(Component.jav a:6181)
    at java.awt.Component.processEvent(Component.java:604 5)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4652)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.KeyboardFocusManager.redispatchEvent(Keyb oardFocusManager.java:1850)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(DefaultKeyboardFocusManager.java:910)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent (DefaultKeyboardFocusManager.java:568)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4524)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:644)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:603)
    at java.awt.EventQueue$1.run(EventQueue.java:601)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:617)
    at java.awt.EventQueue$2.run(EventQueue.java:615)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    I get the code if i enter most symbols however if i enter ; or > for example, the symbol remains. I only want to allow for ints.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: JFrame Text field - Reset if value doesnt equal int?

    xception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "sdfsdf"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at pipesrus.GUI.LengthFocus(GUI.java:300)
    At line 300 in GUI.java you call the parseInt() method with the String: "sdfsdf"
    The parseInt method does not think that String is a valid number and throws the NumberFormatException

    If you want to handle the case where a user enters bad data, you need to put the parseInt() method inside of a try{} catch block that catches the exception, tells the user about the problem and continues waiting for the user to enter good data.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: JFrame Text field - Reset if value doesnt equal int?

    First off, for integer input from 1 to 20 I would use a JSpinner. If you absolutely have to accept input in a text component, you could use a JFormattedTextField or a DocumentFilter on a JTextField's Document.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    FadedAura is offline Member
    Join Date
    Nov 2011
    Posts
    26
    Rep Power
    0

    Default Re: JFrame Text field - Reset if value doesnt equal int?

    Quote Originally Posted by Norm View Post
    At line 300 in GUI.java you call the parseInt() method with the String: "sdfsdf"
    The parseInt method does not think that String is a valid number and throws the NumberFormatException

    If you want to handle the case where a user enters bad data, you need to put the parseInt() method inside of a try{} catch block that catches the exception, tells the user about the problem and continues waiting for the user to enter good data.
    Thanks. Just 1 more issue. How can i limit the user to 1 decimal place?

    Current code:

    Java Code:
    private void lengthFocus(java.awt.event.FocusEvent evt) {
            try {
                l = Double.parseDouble(lengthText.getText());
                if (l >= 1 && l <= 20) {
                } else if (l >= 21 || l <= 0) {
                    lengthText.setText("0");
                }
            } catch (NumberFormatException ex) {
                lengthText.setText("0");
            }
        }
    Quote Originally Posted by DarrylBurke View Post
    First off, for integer input from 1 to 20 I would use a JSpinner. If you absolutely have to accept input in a text component, you could use a JFormattedTextField or a DocumentFilter on a JTextField's Document.

    db
    I would use the JSpinner however i need to allow for users to enter decimals. However i could implement for users Quantity input however is there a way to limit the Spinners values? I only want 1-99 to be selectable.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: JFrame Text field - Reset if value doesnt equal int?

    Quote Originally Posted by FadedAura View Post
    I would use the JSpinner however i need to allow for users to enter decimals. However i could implement for users Quantity input however is there a way to limit the Spinners values? I only want 1-99 to be selectable.
    Use a SpinnerNumberModel set with the appropriate parameters:

    Java Code:
    import javax.swing.*;
    
    public class TestSpinner {
       private static void createAndShowGui() {
          SpinnerNumberModel spinModel = new SpinnerNumberModel(0.0, 0.0, 20.0, 0.1);
          JSpinner spinner = new JSpinner(spinModel);
          
          JPanel mainPanel = new JPanel();
          mainPanel.add(spinner);
    
          JFrame frame = new JFrame("TestSpinner");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }

Similar Threads

  1. The field "name" doesnt apparently exsist :/
    By Addez in forum New To Java
    Replies: 6
    Last Post: 10-22-2010, 11:15 PM
  2. Replies: 1
    Last Post: 04-26-2010, 01:02 PM
  3. JFrame window doesnt change background color
    By Addez in forum New To Java
    Replies: 7
    Last Post: 11-07-2009, 09:38 PM
  4. how to point cursor on the 1st text field in a jframe
    By VineetKumar in forum AWT / Swing
    Replies: 4
    Last Post: 04-17-2009, 02:58 PM
  5. launching Jframe from text field
    By crunchymonkey in forum AWT / Swing
    Replies: 4
    Last Post: 10-15-2008, 02:50 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •