Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Exception in GUI

 
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

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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
 
Sheriff
Posts: 22025
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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 ?
 
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 !




 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic