[HELP] I'm stuck I create a simple Adding program using JTextField

0 Nathan Moland · April 15, 2015

  1. import java.awt.FlowLayout;

  2. import java.awt.event.ActionListener;

  3. import java.awt.event.ActionEvent;

  4. import javax.swing.JFrame;

  5. import javax.swing.JOptionPane;

  6. import javax.swing.JLabel;

  7. import javax.swing.JTextField;

  8. import javax.swing.JButton;

  9. public class child extends JFrame {

  10. private JLabel lbl1;

  11. private JLabel lbl2;

  12. private JTextField num1;

  13. private JTextField num2;

  14. private JButton answer;

  15. public child(){

  16. super("Simple Adding Program");

  17. setLayout(new FlowLayout());

  18. lbl1 = new JLabel("Enter First Number");

  19. add(lbl1);

  20. num1 = new JTextField(10);

  21. add(num1);

  22. lbl2 = new JLabel("Enter Second Number");

  23. add(lbl2);

  24. num2 = new JTextField(10);

  25. add(num2);

  26. answer = new JButton ("Get Sum");

  27. add(answer);

  28. thehandler handler = new  thehandler();

  29. num1.addActionListener(handler);

  30. num2.addActionListener(handler);

  31. answer.addActionListener(handler);

  32. }

  33. private class thehandler implements ActionListener{


  35. public void actionPerformed(ActionEvent event){     

  36. JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));

  37. }

  38. }

  39. }

Post a Reply


Oldest  Newest  Rating
0 steven kitchener · April 16, 2015
All of the imports in your project are external libraries found inside Java (I think, I taught myself so I may have got that wrong).

However, all you really need to understand is that to use things like JFrames, JTextFields etc you need to import them before using them and those particular classes are found in the javax.swing package. Similarly to use colours and graphics you need the awt package.

The * means import everything from that package. So instead of writing-
     -import javax.swing.JFrame;
     -import javax.swing.JTextField; etc
you can just import the whole package and use anything found in that package. 

As for code that you are unfamiliar with, if you are using an IDE, like Eclipse, then you can hold CTRL and click on the method name. This will take you through to the class that uses it and there should be some documentation regarding how it works. If you dont have this option or the docs dont provide enough information then you should google the method name followed by "Java" and "docs" i.e. "getText() Java docs". This should provide you with the official Java documentation of the class that uses that method within the first 3 results. Plus you may even find a website that has some pretty decent examples/tutorials regarding that code. 

I will say that the documentation is a little daunting at first but once youve looked through it a couple of times and know what your looking for it becomes simpler to read and understand.

Good luck

0 Nathan Moland · April 16, 2015
Great thanks, i'm just having trouble in the codes that i'm not familiar with getText(), getSource() others getStateChange() stuff like that. What do you call those? id like to search and read them it would be a big help. 
+1 Rich Sharma · April 16, 2015
nathan, i had the same problem and i made the same solution. i am not sure why, but you cannot parse int jtextfields. i tried doing everything that saturday mourning, but i decided the best way was the use guis and that is what i did. that was a good decision, as i ended up creating a pretty cool calculator, with not just arithmetic, but other forms of math, and even a graphing calculator also using guis. so only thing i can tell you is to listen to steven, and that will be good.
0 Nathan Moland · April 15, 2015
Just one more question I saw this line of code in the other forums
what the codes do?

import java.awt.*; 

import javax.swing.*;
0 Nathan Moland · April 15, 2015
Thank you very much for this it helps me a lot it make things clear to me.
+1 steven kitchener · April 15, 2015
Hi Nathan,

It's been a while since ive used JFrames but ill give it a go. Im going to make an assumption that you are going to print out the total in that JOptionPane.

All of the code i am going to type is going to go into the private class inside the actionPerformed method.

First off we're going to create an int variable called "total" and change the JOptionPane so that it prints out that variable like this 
int total;

JOptionPane.showMessageDialog(null, String.format("%s", total);

next we need to equate the total to something. Im going to be showing you this step by step but you could easily condense it into one line if you understand it well enough. The code needed looks like 
int firstNum = Integer.parseInt(num1.getText());
int secondNum = Integer.parseInt(num2.getText());
total = firstNum + secondNum;

Understandably this looks quite complicated if its the first time seeing it but once i explain what is happening it should be fairly easy to follow. Obviously we need two integers to give us a total but what we have at the moment is two strings. This is because the JTextField (defined here as num1) contains Strings and as such will return a String when getText() is called upon it. So what we need to do is convert the String into an Integer. Integer.parseInt() is a built in Java method that takes a String as a parameter and returns an Integer. So using this method we can convert our Strings into the required int's that we need. Finally sum the two numbers and we have our total. The final code looks like this.

public void actionPerformed(ActionEvent event) {
int total;
int firstNum = Integer.parseInt(num1.getText());
int secondNum = Integer.parseInt(num2.getText());
total = firstNum + secondNum;

So long as there is a string in both of the JTextFields you shouldnt get any errors following this code.

For future reference to put code into blocks as i have done above there is a toolbar above the text area when creating a new post. Find the button called "Code", about 75% across the bar, and click that. That will provide you with a different coloured background so that text meant as code is easier to read. Also, while on the topic of future references, you should make sure that all of your classes begin with a capital letter so in the above example the classes child and theHandler should begin with capitals.

Hope all this helps you,


0 Nathan Moland · April 15, 2015
Sorry i'm new in posting codes in forums i don't know to import the code that makes more readable e.g in Java coding view
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator