Results 1 to 7 of 7
- 11-23-2011, 09:38 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
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: }How can I setText back to 0 if user enters anything apart from numbers 1-20.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"); } }
Thanks.
- 11-23-2011, 09:56 PM #2
Re: JFrame Text field - Reset if value doesnt equal int?
What happens when you execute your code and try it with different values?
- 11-23-2011, 10:00 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
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.
- 11-23-2011, 10:07 PM #4
Re: JFrame Text field - Reset if value doesnt equal int?
At line 300 in GUI.java you call the parseInt() method with the String: "sdfsdf"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)
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.
- 11-23-2011, 10:16 PM #5
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-24-2011, 03:04 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 26
- Rep Power
- 0
Re: JFrame Text field - Reset if value doesnt equal int?
Thanks. Just 1 more issue. How can i limit the user to 1 decimal place?
Current code:
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.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"); } }
-
Re: JFrame Text field - Reset if value doesnt equal int?
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
-
The field "name" doesnt apparently exsist :/
By Addez in forum New To JavaReplies: 6Last Post: 10-22-2010, 11:15 PM -
unable to clear text Field in Multi Screen JFrame GUI Application
By harshalforu in forum AWT / SwingReplies: 1Last Post: 04-26-2010, 01:02 PM -
JFrame window doesnt change background color
By Addez in forum New To JavaReplies: 7Last Post: 11-07-2009, 09:38 PM -
how to point cursor on the 1st text field in a jframe
By VineetKumar in forum AWT / SwingReplies: 4Last Post: 04-17-2009, 02:58 PM -
launching Jframe from text field
By crunchymonkey in forum AWT / SwingReplies: 4Last Post: 10-15-2008, 02:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks