Results 1 to 11 of 11
  1. #1
    ashin is offline Member
    Join Date
    Nov 2008
    Posts
    71
    Rep Power
    0

    Red face try catch in swing.

    anyone would like to help me how to declare an exception in swing?
    just give me an example code.. i am using netbeans... tnx
    :cool:

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    It's the same as in AWT or any other piece of code. What's the problem? What part of the tutorials didn't you understand?

  3. #3
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    try { //put ur code that is supposed to thrw exception }
    catch (Exception e) //u can specify name of exception in place of "Exception" if u know, <or u can see wen u run without try n catch>
    {[
    System.out.println("exception"+e);
    }

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by programmer_007 View Post
    System.out.println("exception"+e);
    }
    Never ever do that! Print the stack trace.

  5. #5
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    yeah man .. correct ..but for the beginers sake ... can start this way ..

    but its always good to go 4 STACK Trace..

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by programmer_007 View Post
    ...but for the beginers sake ... can start this way ..

    ...
    Bad advice.

  7. #7
    ashin is offline Member
    Join Date
    Nov 2008
    Posts
    71
    Rep Power
    0

    Default

    hi to everyone.. tnx... my problem is when i put a string in the jtextfield still my System.out.println("error") didnt show ....this is my code:

    public static void main(String args[]) {
    try{


    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new OddEven().setVisible(true);
    }
    });
    }
    catch(Exception e){
    System.out.println("error");


    }

    i am using the netbeans editor
    :cool:

  8. #8
    ashin is offline Member
    Join Date
    Nov 2008
    Posts
    71
    Rep Power
    0

    Default

    this is my error... i already code the try catch but still the same output... the error message didnt show when i put a string in the textfield
    Attached Thumbnails Attached Thumbnails try catch in swing.-error.jpg  
    :cool:

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    If you want there to be an error printed to the console when someone enters text into a textfield then your code is way off what you want to do. Where are you checking the text entered on the textfield?
    You would need to register an appropriate listener on the textfield and do the printing in the listener's handler method.

  10. #10
    ashin is offline Member
    Join Date
    Nov 2008
    Posts
    71
    Rep Power
    0

    Default

    you mean , i have to code for the handler listener for textfield??
    :cool:

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Depends on when you want to check the value in the textfield. I assumed it'll be the listener for the textfield because you said "when i put a string in the jtextfield still my System.out.println("error") didnt show".

Similar Threads

  1. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  2. try catch...
    By MarkWilson in forum New To Java
    Replies: 8
    Last Post: 06-27-2008, 05:39 PM
  3. Try Catch
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 12-03-2007, 04:10 PM
  4. when to use try...catch
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-18-2007, 08:52 PM
  5. Use try and catch
    By zoe in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:50 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
  •