aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Exception in GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Exception in GUI" Watch "Exception in GUI" New topic
Author

Exception in GUI

Arwa Saad
Greenhorn

Joined: May 05, 2012
Posts: 9
Hi

I have a question
The teacher asked us for a this code

Using Exception Handling, allow only numeric values to be entered for the dimensions of the shape. If the user enters a non-numeric value, a popup window (i.e., another frame) should appear with a suitable error message displayed in a large font (use JLabel for this message). Make the popup window block input in the original frame until it is closed.


The proplem is where to put the try/catch thing ?
if I put it in the main the compile says it's empty string

sp I moved it to method actionPerformed
it worked but the conainer for the new frame is empty ! although I put Jlabel in it
can you help me ?

this is my try

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Arwa Saad wrote:the conainer for the new frame is empty ! although I put Jlabel in it


Read your code carefully. To what container do you add the JLabel text? Does that have anything to do with the new JFrame?

Additionally, you need to learn to use meaningful variable names, and adopt a consistent code formatting style, with appropriate indents and matching outdents. here's a publication that sets out the standard coding conventions: http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Your new JFrame doesn't block input in the original frame. You need a modal JDialog for that, or easier use JOptionPane and its static methods.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Arwa Saad
Greenhorn

Joined: May 05, 2012
Posts: 9
Read your code carefully. To what container do you add the JLabel text? Does that have anything to do with the new JFrame?


conatiner cp is the new container
and in this step , I added it to the newest



but you right ! how can I tell the compiler that the new container is for the new JFrame ?
that's one of my problem + thanks for the link , I'll check it

Your new JFrame doesn't block input in the original frame. You need a modal JDialog for that, or easier use JOptionPane and its static methods.


If I can I would do JOptionPane but she only want new frame with JLabel
Arwa Saad
Greenhorn

Joined: May 05, 2012
Posts: 9
I fixed one my problem
so I needed to create an object from JPanel !

now I only need to know that if he entered non-numeric value a new frame will pop out
is there an ActionListener for this ?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you use a try/catch block

in the try{}, you parse the input to a numeric value,
if this parsing throws an Exception --> in the catch{}
you popup your 2nd frame (which would be a JDialog)
Arwa Saad
Greenhorn

Joined: May 05, 2012
Posts: 9

Now , I have another problem
whenever I press a key wither number or letter
it will throw exception !

here the code :


I only need it to throw exception if he entered a letter !
anything wrong with my code ?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you're welcome
Arwa Saad
Greenhorn

Joined: May 05, 2012
Posts: 9
Michael Dunn I'm really sorry
that was a bad behaivor from me !
I should have thanked you before
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you have 3 textFields, with keyListeners on each
before you type anything, their getText() would return an empty string ("")

you type a character into one of the textFields, and your code tries to parse
all 3 textFields at the same time, so when it tries to parse "", it will error

you could test for the source in your KeyListener code, but do you really
want the listener firing on each keyStroke?

A button ("Calculate") might be better suited for the user to press when all
textfields have input data.
Arwa Saad
Greenhorn

Joined: May 05, 2012
Posts: 9
Michael Dunn wrote:you have 3 textFields, with keyListeners on each
before you type anything, their getText() would return an empty string ("")

you type a character into one of the textFields, and your code tries to parse
all 3 textFields at the same time, so when it tries to parse "", it will error

you could test for the source in your KeyListener code, but do you really
want the listener firing on each keyStroke?

A button ("Calculate") might be better suited for the user to press when all
textfields have input data.


thanks you so much !
I understand the problem now and I fixed in my way
here the soultion if anyone want



all I did was that I added 0 to the string !
if the user entered number for example 5
it will be 05 and it's the same as 5 !
but if he entered a char g
it will be 0g --> throw exception !

I'm really thankful for all the great ppl here
thank you so much ! this problem have been bother me for almost a 3 weeks !




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in GUI