This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing noob looking to create a simple calculator interface  RSS feed

 
Kyle Jones
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

This is a simple guitar fret calculator I want to create. I have the program logic working fine and am now trying to make it into a swing interface.

All i need for now is for it to have 2 user input boxes (scale length & number of frets). And then to print the result into another box (bigger box though has to fit about 25 lines)

I am a complete swing noob and have got to the stage where I have 3 boxes but how do go about using the input from the first 2 boxes to print to the 3rd box?

Any help would be great
 
Swastik Dey
Rancher
Posts: 1792
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you done so far with the UI design?
 
Kyle Jones
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far its just 3 boxes, first 2 accept data but are doing nothing with it

I need to be able to input data (an int) into the first 2 boxes and somehow submit that data so that it can be used in my program logic (or formula) ?? .. and then print the result in the 3rd box..

is this possible ?
 
Swastik Dey
Rancher
Posts: 1792
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not. Easiest approach might be to use a button. On button click even you can do the calculation. Another approach would be to use key listeners on input fields, for. e.g when the enter key is pressed do the calculation.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree with you.

I think the first thing to do is get the calculator working without an interface. Run it from the command line. Then consider a GUI later.
 
Kyle Jones
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hoping someone can point me in the right direction with this..

Main problem I am getting:

That only the last line of the loop is displayed in the fretScale TextArea. Also i have commented out line 68 just to get the program running but this line is giving a compiler error. Is there an easier was to store the input values to use in this way?



Any questions please ask

 
Swastik Dey
Rancher
Posts: 1792
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what I noticed

I have the program logic working fine and am now trying to make it into a swing interface.


So I got I feeling that it must be already running through command line interface.
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably. I missed that bit, sorry.
 
Swastik Dey
Rancher
Posts: 1792
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that to you need multiple rows to display the output. So JTable might be an option, or may be JTextArea with line break. However JTable would give it a better look.
 
Kyle Jones
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Disagree with you.

I think the first thing to do is get the calculator working without an interface. Run it from the command line. Then consider a GUI later.


Oh ya I had it working fine .. using Scanner to accept input
 
Kyle Jones
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:It seems that to you need multiple rows to display the output. So JTable might be an option, or may be JTextArea with line break. However JTable would give it a better look.


Will try JTable

Thanks

edit; i did try JTextArea and then with a line break in the for loop, but this did not help, still only the last iteration (interation 22) was printed. It must be something to do with the JTextArea as when i run a System.out.println in the for loop it prints 22 iterations in the console
 
Swastik Dey
Rancher
Posts: 1792
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might have used jtextArea.setText, right? So in this case it will always overwrite the previous value. You have to first retrieve the previous value and then call setText again.

Some what like

 
Kyle Jones
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No i didnt use jTextArea.setText at all .. should I have?
 
Swastik Dey
Rancher
Posts: 1792
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then how are you displaying text in JTextArea?
 
Jim Ronholm
Greenhorn
Posts: 22
3
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 66
fretScale.setText("Fret " + i + ": Distance from nut(mm): " + (650 - z) + "mm");


so - yes you are using setText()

but
You have to first retrieve the previous value and then call setText again.
isn't necessary either. JTextArea has method. Use that on Line 66. You can (likely will want) new lines appended each time it loops.


http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextArea.html
 
Danny Wreczycki
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look this project. Its not finished calc I made while learning basics of java and swing. Its Eclipse project so just import into your ide and feel free to tweaking. calcsimple
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!