• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaFX unit converter ignores first number in field unless I put in a full stop at the end.

 
Greenhorn
Posts: 11
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.

So I have been refreshing myself on Java. To do this, I have been doing a self-assigned Java project which is a GUI unit converter program using Netbeans, Java8, and JavaFX.  A had some tutorial help. Some road bumps, but I managed to solve them.

So far, I have aiming to convert to basic length units.(Meters and Feet) with two text fields that have combo boxes that allow to choose between the two units(in future, more)  with another combo box that will allows one to choose between types of units(length, weight, volume.) that will change that unit the below combo boxes

So it seems to me that I have got everything in order to test my first basic unit conversion.

In case in becomes relevant(I suspect it is part of problem) . How the unit converter works is by using a base unit(meter in the case of length) and convert all queries to that unit before converting from that unit to the desired unit. I.E To convert inches to feet, inches is first converted to meters, then then result in meters is converted to feet.

So it seems to me that I have got everything in order to test my first basic unit conversion.

I type in 1 and 1 appears in the left text field(meters to feet for now), but the right text field(feet) does not fill with a conversion result. I type in another 1 into the left text field, making the number appear to be 11. The right text field finally spits out an answer. But instead of being the correct result(11 meters = 36,089 feet) the result is (1 meter = 3,2808) which means that the logic code ignores the first number.

I figured out that the field only outputs the correct results when I type a .full stop. at the end of the number I.E to make sure that ‘114’ is not only accepted as ‘14’ you have type in  ‘114.’ I can also then remove the full stop and the right field will not change, but to have more number added to query, you have to add them then type in a full stop again.  I have been using BigDecimal, so that might be part of the problem

There is also the problems that if I type in the right  (there is supposed to be a two way conversion system) then it still ignores the first number, but when I also type more numbers in right text field, then only the right text field fills with numbers, like the result is being outputted to the same  field it being typed in.

Another problem is in the if-else statement where the code that wipes both sides when the text field is cleared[isEmpty] is triggered into action by just typing in the text field. Though it just triggers the systems printout part of the statement and not the wiping of the text fields.

Any help will be appreciated.

Please find the code attached to post below
 
Hugh Van Der Vaal
Greenhorn
Posts: 11
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
App package

Controller.fxml




Main.class



Controller.java



App.Converters Package
BaseUnitConverter.java




UnitTypeConverter.java


 
Hugh Van Der Vaal
Greenhorn
Posts: 11
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

FootConverter.java



LengthConverter.java



MeterConverter.Java



Thank you very much for any help provided.
 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ran your code and from a first glance, everything seems to work as expected. All I did was focus the left text field, and type '1'.

Can you quickly summarize what the exact problem is?
converter.png
[Thumbnail for converter.png]
 
Hugh Van Der Vaal
Greenhorn
Posts: 11
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stephan.

Sorry the lateness of this reply

The problem seems to be to my program(at least on my end) does not output in the way I want it to, either requiring extra counterintuitive steps, not working at all, or getting "confused"

Since it seems to work fine on your end.  I think the might be something wrong with how my Netbeans complies the program. My I post the log for when the program first complies? Is there online IDE that anyone can suggest that I can use to write and test the program just so test whether or not the program failing might be in part due to my Netbeans IDE?

In the meantime here are screenshots of some of the problems.
Screenshot-(1).png
 The converter ignoring the first number.
The converter ignoring the first number.
Screenshot-(2).png
The converter is only reading second number in the text field.
The converter is only reading second number in the text field.
Screenshot-(3).png
The converter reading the entire number once I put a full stop at the end.(Though I have since figured that any typed character will do).
The converter reading the entire number once I put a full stop at the end.(Though I have since figured that any typed character will do).
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks like something to do with the left text box, that it is not executing its action until you do something else, e.g. adding the full stop.
 
Hugh Van Der Vaal
Greenhorn
Posts: 11
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is aslo a problem with the right field where the field does react until put in second number, When does react, the first number "disappears" and I think it fills itself with a conversion result. No reaction from the left field

Example.
Screenshot-(4).png
First Number is 2. No action from either field.
First Number is 2. No action from either field.
Screenshot-(5).png
Second number is 3. The 2 dissapears and completely replaced with 3(This can go on with lesser, greater, and equal numbers). Random decimal number appears after three. Possibly the conversion result being added to the field.
Second number is 3. The 2 dissapears and completely replaced with 3(This can go on with lesser, greater, and equal numbers). Random decimal number appears after three. Possibly the conversion result being added to the field.
Screenshot-(6).png
I press backspace. The 3 dissapears and the leaving only the 0. decimal number, which has also lessened. I pressed backspace nine times, each time the decimal number lessed until the decimal point dissapears leaving only a zero
I press backspace. The 3 dissapears and the leaving only the 0. decimal number, which has also lessened. I pressed backspace nine times, each time the decimal number lessed until the decimal point dissapears leaving only a zero
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please tell us what version of JavaFX you're running.

Please tell us how you're running your application.

If you're using Maven, please show us your POM.

Show us your app.css file.
 
Hugh Van Der Vaal
Greenhorn
Posts: 11
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I run the application with the RunProject option after using the clean and build project option.

When I run it I do get these messages in amongst the log.

No base JDK. Package will use system JRE.
No base JDK. Package will use system JRE.


When I made the Project, I made it with the JavaFXApplication option of Java with Ant. When given the option(the only one) I used SDK 1.8.(I am on Java Version 19 though I do have other java versions available ) . So suppose that I am using the JavaFx version included with Java jdk1.8.0_221. Though I did try to make JavaFx 19 work with my IDE'S.

When I made the project this way. It did not generate with app.css file or an fxml file. I made the files through other means and added them to the project. I initial used Java Eclipse to try to make the project but was unable to get JavaFX tooling working with it.(It asked that I make Eclipse  make the Java 8 the version that Eclipse run on, which I despite my best efforts, I was unable to do). I did  make the app.css.file named in it, though it is only has a generated comment. And I had to make the blank fxml file myself to.

here is the contents of the app.css



 
Bartender
Posts: 244
12
IntelliJ IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I feel like NetBeans is behind the times with their architecture being so dependent on Ant stuff. They do have Gradle support but last I checked it was kind of unreliable. Never really tried their Maven integration. I switched to IntelliJ which is really a better IDE overall anyway. Using Java 8 certainly is the easy way to get a JFX project up and running but it won't always be there.

I see the same thing as Stephan, the left field responds immediately to a keypress. This is on Linux tested with Java 11 and 17. What OS are you on? I wonder if different OSes (or Java versions) perform differently with the onKeyTyped event.

See if it behaves the same when you use onKeyReleased or onKeyPressed. With onKeyPressed I got it to behave the same as what you see, but with the release event handler it works correctly. I don't use onKeyTyped very often because it has always seemed inconsistent to me depending on the component or something (I forget why entirely TBH)... and apparently it can behave differently on different environments !

But..... better yet, use the text property:



That lambda is a change listener. The long form (no lambda) looks like this.


I would prefer to use the change listener lambda rather than relying on keypresses. This should be far more reliable.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lou Hamers wrote:Yeah I feel like NetBeans is behind the times with their architecture being so dependent on Ant stuff.


Um, what?

NetBeans has had strong out-of-the-box support for Maven projects for ages now, and as of a few versions ago Maven is also considered the default project layout, with Ant being available but discouraged.

Hugh, I would strongly recommend using Maven for any Java project, but for JavaFX in particular. I'll see if I can reproduce your issue with JavaFX 8 later today. I tested it with OpenJFX 18 earlier.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hugh, please clone this repository and open the project in NetBeans. Run or debug the project in NetBeans and tell us what happens.

Note that I renamed your packages so that their names are more in line with conventional package naming schemes. Feel free to revert or change of course, but make sure you also edit module-info.java and pom.xml.
 
Hugh Van Der Vaal
Greenhorn
Posts: 11
MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone for your help and advice and modifications to the project.

Sorry for the lateness of the reply . I suddenly got swamped with work out of nowhere, and now it is the end of the day I am very tired, with more work tomorrow. So won't have much time test everything out, though I will let you know when I do and if any problems arise. Though I now hopeful that there will be no problems that I can't handle going forward thanks to your help.

 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please let me know when you've cloned the repository, so I can take it offline.
 
Lou Hamers
Bartender
Posts: 244
12
IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Lou Hamers wrote:Yeah I feel like NetBeans is behind the times with their architecture being so dependent on Ant stuff.


Um, what?

NetBeans has had strong out-of-the-box support for Maven projects for ages now, and as of a few versions ago Maven is also considered the default project layout, with Ant being available but discouraged.

Hugh, I would strongly recommend using Maven for any Java project, but for JavaFX in particular. I'll see if I can reproduce your issue with JavaFX 8 later today. I tested it with OpenJFX 18 earlier.



Their Maven support is probably fine, I don't use it myself, but the times I've had to load a Maven project in NB it went fine. I can't say the same for Gradle vs Ant projects though, I always seemed to have issues with their Gradle integration, but their Ant projects worked pretty well, at least with JavaFX stuff.

Lou Hamers wrote:Yeah I feel like NetBeans is behind the times with their architecture being so dependent on Ant stuff.



I meant the NetBeans IDE build architecture. It's been a while since I was involved with NB but I believe I developed a (soft) opinion that they might be having trouble keeping up with modern changes after messing with the NetBeans source/project itself and that build architecture. The NetBeans build uses Ant and I get the feeling (and I could be wrong) that they would struggle to change that if Ant were ever to fall completely out of use and support. It has been a while since I've seen anyone even mention Ant for a new project, it's always either Gradle or Maven. That combined with some of the under-the-hood issues they seem to be having with 1st class Gradle integration gave me a bad impression.
 
reply
    Bookmark Topic Watch Topic
  • New Topic