• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

GUI converter practice ( without an action button )  RSS feed

 
Greenhorn
Posts: 7
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!  Im new to JAVA and learning a great deal ( its getting tougher now ). In the 1980s, I used to write small programs in BASIC, took a break and had a career totally unrelated. Now I want to get back in. I am self-teaching myself JAVA and have done a few 'helloworld' examples and compiled a few projects, so this is my next step/challenge for me to understand how things work. My friends recommended JAVA, and I have started using Eclipse with the WindowBuilder plug-in.
With my new challenge, I have searched google and around for examples to follow, but have not yet found any.

Here is what I have so far, im stuck at getting the action going.

What I want to do is convert Pounds to Kilograms without needing an action button. As you type your number, it automatically fills in the converted number. With that in mind, is it possible to have it go both ways? ( whether you input in pounds or kilograms - the opposite field fills in. )

Thanks in advance!!

 
Ranch Hand
Posts: 75
3
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, sure such easy converters are possible without the need for an extra button, but it's not so easy as just add some listeners. Rather you have to sub-class the input fields to change thier internal behaviour when an input is made.
Beware to not to write some endless-loop code but look what lines are executed when specific methods called from outside.
 
Master Rancher
Posts: 3270
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add some print statements to the key listeners to show what has been read.

At some point the code needs to get all the digits that the user has entered in the text field, converted them to the other units and put that results in the other textfield.
 
Rancher
Posts: 2934
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have started using Eclipse with the WindowBuilder plug-in.



Don't use an IDE to generate the code. You are spending time learning the IDE and the code will not be maintainable if you ever need to move to another IDE.

Learn to create your GUI windows manually and use the IDE to help with compiling debugging etc.

Another problem with the generated code is it is using null layout. Don't use a null layout. Swing was designed to be used with layout managers. Learn to how use layout managers when you manually create the code.

Here is what I have so far, im stuck at getting the action going.



So this means the conversion will only be done when the button is clicked, so you need to add a "Convert" button to the frame.

If you use a button then you can only do the conversion one way. So you need to:

1. get the text from the first text field and convert is to a number
2. convert the number to the other value
3. convert the number to a String and set the text on the other text field.

So the point of this exercise is to learn how to write a simple Action. There is no need for the KeyListeners.


As you type your number, it automatically fills in the converted number.



Now you create a new program because the approach will be different.

You don't use an Action.

Don't use a KeyListener. That is an old approach for AWT. Swing has newer and better API's to use. In this case you should use a DocumentListener. Read the section from the Swing tutorial on Listeneing for Changes on a Document for more information and examples to get your started.

With that in mind, is it possible to have it go both ways? ( whether you input in pounds or kilograms - the opposite field fills in. )



Of course, but you have to worry about creating an infinite loop.

1. First of all you would need to add a DocumentListener to each text field.

2. Now when you change one the other will change which will cause the another event to be generated and the first updated etc.

So, in the DocumentListener you would need to:

1. remove the DocumentListener from the other text field
2. do the appropriate conversion calculation
3. add the DocumentListener back to the other text field.
 
Steve Guy
Greenhorn
Posts: 7
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The drag and drop form builder was uber cool for me. I was thinking.... Surely it couldn't be this easy!  What can I say.... bells and whistles get me every time.

Im going to click some of the links you sent and see what I can learn.

Thanks!
 
Sheriff
Posts: 23644
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Steve!

There's another use for the DocumentListener which Rob described there: you have to take care of the situation where people type in non-numeric characters in your text fields. You would have bumped into this problem before long, but it doesn't hurt to anticipate that kind of problem and deal with it up front, rather than potentially having to rewrite a lot of code. Or alternatively you could use a JFormattedTextField to handle that problem -- Rob would have a better opinion about that than I would.
 
Paul Clapham
Sheriff
Posts: 23644
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Don't use an IDE to generate the code. You are spending time learning the IDE and the code will not be maintainable if you ever need to move to another IDE.



And if you're new to Java, it's like getting into the driver's seat of a car for the first time and heading out onto the autobahn, where there's no speed limit. It doesn't always end well!
 
Steve Guy
Greenhorn
Posts: 7
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did encounter that, and found some instruction on doing an override keyevent ( which I have on line 57 and 70 ).  So far that has worked. 
 
Paul Clapham
Sheriff
Posts: 23644
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Guy wrote:So far that has worked. 



That means your tests so far have only tested things that you expect to happen. For example your code doesn't handle the paste key -- why shouldn't the user copy a number from somewhere else and paste it into your text field?
 
Steve Guy
Greenhorn
Posts: 7
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know....  I have only scratched the surface. 
 
Norm Radder
Master Rancher
Posts: 3270
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what the best way to learn is.
Some of the helpers here rush OPs to get the final best solution, rather than let them work through the process of trial and error.
 
Paul Clapham
Sheriff
Posts: 23644
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the feeling you'd like to have decimal numbers, too, so that for example you could convert 1 pound to 0.454 kilograms.
 
Steve Guy
Greenhorn
Posts: 7
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ill probably keep it whole numbers, I have yet to look into the options for formatting and digit length.  The hundredth place would be max for me.  Im excited to try... at the same time, a bit frustrating because its a new language and the formatting has not yet 'hit me upside the head' with a bottle of 'NowIGetIt!!'

..... and this is why I like the WindowBuilder right now, its my crutch  :-/  :-/
 
Steve Guy
Greenhorn
Posts: 7
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is tough. ehh ehh....  So I have been reading the oracle learning guide trying to get my 'building blocks' going, and it is not yet clicking in my head.  So to break up the reading, I have been searching for example codes of what I want to do.  What I am finding is that their seems to be so many ways to do one thing, and I want the easiest.

I was quite excited when I found the answers how to code the 'close' and 'clear' buttons. Looking up in tutorials, there were complex lines to accomplish this - then I learned how to double click the button, and add the code ( textField.setText("")) to one, and (frame.dispose();) to the other - and it worked!  Simple and easy.  That is working for me.

So my next step is getting to get the calculator to work by following examples, this is the closest that I have found giving me that result - its just giving me an error on the new Float(value); "The constructor Float(String) is deprecated".   Looking this up, I get the impression that it is no longer used, but I could not find a substitute.

Any insight?

 
Steve Guy
Greenhorn
Posts: 7
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.... btw - this has the calcs for temperature, Im going to worry about the calc part once I get something working
 
Norm Radder
Master Rancher
Posts: 3270
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

its just giving me an error on the new Float(value);


I assume that you want to convert a String value to a floating point numeric value.
Look at the parse... methods in the Float or Double classes
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!