Results 1 to 5 of 5
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default JTextField and Try-Catch Question

    I have a general question about using JTextFields and Try-Catches properly.. If I had, say 3 JTextFields that are only supposed to accept integers, how can I initiate a try-catch to check WHICH JTextField has incorrect information?

    I know how to use a normal try-catch, for example;

    Java Code:
    try
    {
    int price1 = Integer.parseInt(txtField1.getText());
    int price2 = Integer.parseInt(txtField2.getText());
    int price3 = Integer.parseInt(txtField3.getText());
    }
    catch (NumberFormatException e)
    {
    JOptionPane.showMessageDialog(null, "Please fill in the required fields", "Invalid data!", 
    								JOptionPane.ERROR_MESSAGE);
    
    //Now here's the problem, program needs to determine WHICH JTextFields
    //have crashed and do something with it afterwards.. like setting the background
    //of said textfield red, for example
    }
    Something along the lines of that. Would I have to do a try-catch inside a try-catch? If so, how could I format that properly?
    Last edited by CuppaCoffee; 02-19-2012 at 10:24 PM.

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

    Default Re: JTextField and Try-Catch Question

    Programming by Exception is generally regarded as bad form. You could use a DocumentFilter that permits only digits and does a bounds check to prevent a text that would represent a value greater than Integer.MAX_VALUE.

    Moving to AWT/Swing.

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

  3. #3
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: JTextField and Try-Catch Question

    I would do that, but this is an in-class assignment and we're specifically learning about try-catch statements, so I'll have to abide by the assignment rules.

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

    Default Re: JTextField and Try-Catch Question

    Use an array for the text fields and the index will tell you which one had the bad data
    or put each of the parseInt() calls in its own try/catch

  5. #5
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: JTextField and Try-Catch Question

    Yeah, I did what Norm suggested and gave each parseInt() it's own try/catch. It has a lot of confusing nonsense involved.. but my teacher said it's correct, in the terms for which he asked for, so there's no real need to post the code.

    Thanks for the suggestions, guys. Much appreciated!
    Last edited by CuppaCoffee; 02-20-2012 at 01:18 AM.

Similar Threads

  1. JTextfield question
    By xyknight in forum New To Java
    Replies: 3
    Last Post: 04-08-2011, 04:25 PM
  2. JTextField Question
    By Rocketz in forum New To Java
    Replies: 1
    Last Post: 03-09-2011, 12:38 AM
  3. JTextField Question
    By Kyle227 in forum New To Java
    Replies: 5
    Last Post: 05-09-2010, 07:44 AM
  4. JTextField question
    By Chasingxsuns in forum New To Java
    Replies: 5
    Last Post: 07-14-2009, 02:39 AM
  5. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:56 PM

Posting Permissions

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