• Post Reply Bookmark Topic Watch Topic
  • New Topic

TextField() Alignment  RSS feed

 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear sir i would like to know that TextField constructor throws a HeadlessException although there is no need to define catch block for that exception why

TextField txt=new TextField();//There is no need to try catch why?
on ther other hand like sleep() method of Thread class most tack try, catch block , why this ambiguilty.
And listen sir one thing more when we use like
txt.setText("1");
it will display text in left starting side of txt field and i want to show it in right side what i have to do.

with regard

Arun kumar maalik
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. These are two quite different questions, aren't they?

HeadlessException is an unchecked exception, like NullPointerException, ArrayIndexOutOfBoundsException, etc. You are not forced to catch exception types that extend RuntimeException or Error.

Regarding your other question: I'll move this topic to our Swing/AWT forum for that to be answered.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously the exceptions you get from something like Thread.sleep() [InterruptedException] are checked exceptions.
Look at their inheritance in the API. As Ernest has told you, anything which is a subclass of Error or RuntimeException is unchecked, and any other type of Exception is checked, ie you must provide exception handling or the compiler will tell you off.
The reasons you write "throws HeadlessException" after the constructor signature are
  • Because you have used an IDE which found the possibility of a HeadlessException
  • and so any users of your method are warned about the possibility so they can deal with it for themselves.

  • And as for JTextFields: go into the API, where all Java programmers should be every few minutes and look for methods like setHorizontalAlignment. Try them, then tell us whether they work. CR
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!