• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Event Handler Glitch (I think)

 
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I have a homework in which I am supposed to create an application that converts money from Qatari Riyals to one of 3 selections (USD, Euro or Pounds).

I wanted to be the cool boy and gave my app a background a title and an icon. But it wasn't enough I also decided to not have a submit button to start the calculation. I added to the input field a key listener so it would calculate on run time. As you will see in the code below I added a catch in case the input was something other than a number, however no matter what I input I get the error message and to mention the weird patches.

I would like to know how to get rid of those annoying colour patches and why is the error appearing regardless of what is in the input? Please keep in mind this is incomplete I know, I just wish to know how to fix my issues before i move on.

Screenshots
https://gyazo.com/f4fbde1274311498f7381a43192bc85d
https://gyazo.com/ba90c38dbc4d12f029a1bfe6cadf955a
https://gyazo.com/1fd451b4c79e76d569b1225dfcfbd39c

Code:


 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yosuf Ibrahim,

I would like to know how to get rid of those annoying colour patches



You might have to look at how you are using your panels and their setLayout.

Secondly, on  

why is the error appearing regardless of what is in the input


The keypressed method takes in an empty regardless of what you put in the way you are using it presently.
And such can't be converted to a double. So you will always get the NumberFormatException.

So, how do you go around this? Get the text in the textfield before conversion, test to check the length of the input among other things.
The try/catch will take care of the strings inputs.

 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,  managed to fix my issues and completed the app. My issue with the patches was due to swing inability to deal with custom opacity. So I had to paint teh background with the following code first.


As for the event handler i changed it from onKeyPressed to on key released. Here is my app after completion

 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done for fixing your problems.

Some comments on the code.
That's a lot of magic numbers, why not use layout managers and avoid the need to hard code loads of numbers?
You might want to consider formatting the answer so it only displays the appropriate number of decimal places for the currency you are converting to.
You might also want to limit the input to the appropriate number of decimal places for the input currency.

BTW shouldn't the converted value automatically change when you select a different currency radio button without having to then select the input field and press Enter again.
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This kind of line: is usually written like this:
 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Well done for fixing your problems.

Some comments on the code.
That's a lot of magic numbers, why not use layout managers and avoid the need to hard code loads of numbers?
You might want to consider formatting the answer so it only displays the appropriate number of decimal places for the currency you are converting to.
You might also want to limit the input to the appropriate number of decimal places for the input currency.

BTW shouldn't the converted value automatically change when you select a different currency radio button without having to then select the input field and press Enter again.



I found it easier to use the absolute layout to be honest however due to its ineffectiveness I have to set the application size and make it unresizable. I am open to new ways if they are better
 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:This kind of line: is usually written like this:



Thanks bro, and i just thought i learnt nothing new today lol

Cheers
 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Well done for fixing your problems.

Some comments on the code.
That's a lot of magic numbers, why not use layout managers and avoid the need to hard code loads of numbers?
You might want to consider formatting the answer so it only displays the appropriate number of decimal places for the currency you are converting to.
You might also want to limit the input to the appropriate number of decimal places for the input currency.

BTW shouldn't the converted value automatically change when you select a different currency radio button without having to then select the input field and press Enter again.




You are right, i will add event handlers to the buttons and will also fix the decimal places
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

managed to fix my issues and completed the app. My issue with the patches was due to swing inability to deal with custom opacity. So I had to paint teh background with the following code first.






No that is not what suggested in your other cross posted question: http://stackoverflow.com/questions/43812813/eventhandler-acting-weirdly-and-weird-color-patches

There is no need for "magic numbers" to be used.


 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I added event handlers to the buttons as suggested and formatted the output
I have until Saturday to hand this in so if you guys have even better suggestion throw them in, please.
I do have a question When I turn my application into a runnable Jar file the icon representing my app does not appear on the desktop icon. How do I replace the default java icon with mine?

Cheers

Application as JAR file:
https://drive.google.com/open?id=0B9v7ABFs2xINOTh2bHl5U1ZzU1E

Source Code
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When I turn my application into a runnable Jar file the icon representing my app does not appear on the desktop icon. How do I replace the default java icon with mine?  


The answer is a little different depending on how you created your runnable jar, but basically you want to put your icon into a resource folder in the jar.  In your code you should use .getClass().getResource(filename);

 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:

When I turn my application into a runnable Jar file the icon representing my app does not appear on the desktop icon. How do I replace the default java icon with mine?  


The answer is a little different depending on how you created your runnable jar, but basically you want to put your icon into a resource folder in the jar.  In your code you should use .getClass().getResource(filename);

I think Yosuf means the icon that is displayed on the desktop for his jar not the one that is displayed on his JFrame.
If so, this is an OS issue and not a Java one. The problem is discussed here https://coderanch.com/t/513237/java/change-icon-jar-file.
The problem is as a jar is not an executable file you can't set individual icons for individual jars. The easy approach is to create a batch file to run the jar (ie .bat or .cmd on Windows) and set the icon on that.
 
Yosuf Ibrahim
Ranch Hand
Posts: 271
6
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

Knute Snortum wrote:

When I turn my application into a runnable Jar file the icon representing my app does not appear on the desktop icon. How do I replace the default java icon with mine?  


The answer is a little different depending on how you created your runnable jar, but basically, you want to put your icon into a resource folder in the jar.  In your code you should use .getClass().getResource(filename);

I think Yosef means the icon that is displayed on the desktop for his jar, not the one that is displayed on his JFrame.
If so, this is an OS issue and not a Java one. The problem is discussed here https://coderanch.com/t/513237/java/change-icon-jar-file.
The problem is as a jar is not an executable file you can't set individual icons for individual jars. The easy approach is to create a batch file to run the jar (ie .bat or .cmd on Windows) and set the icon on that.



yep, that is my issue, it appears fine on the title bar and taskbar but not on the desktop. Cheers mate I will read through
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic