This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in GUI

 
Arwa Saad
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arwa Saad
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're welcome
 
Arwa Saad
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn I'm really sorry
that was a bad behaivor from me !
I should have thanked you before
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic