- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add multiple number input fields with JOptionPane and display the sum in Console with Java
At first, set multiple number input fields −
JTextField text1 = new JTextField(10); JTextField text2 = new JTextField(10); JTextField text3 = new JTextField(10); JTextField text4 = new JTextField(10); JTextField text5 = new JTextField(10); JTextField text6 = new JTextField(10); JTextField text7 = new JTextField(10); JTextField text8 = new JTextField(10); panel.add(text1); panel.add(text2); panel.add(text3); panel.add(text4); panel.add(text5); panel.add(text6); panel.add(text7); panel.add(text8);
Now, let us add the values of the multiple number input fields created above −
System.out.println(Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText()) + Integer.parseInt(text3.getText())+ Integer.parseInt(text4.getText())+ Integer.parseInt(text5.getText())+ Integer.parseInt(text6.getText())+ Integer.parseInt(text7.getText())+ Integer.parseInt(text8.getText()));
Above, we have displayed the sum in the Console.
The following is an example to sum multiple number input fields with JOptionPane −
Example
package my; import java.awt.GridLayout; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class SwingDemo { public static void main(String[] args) { JPanel panel = new JPanel(new GridLayout(5, 3)); JTextField text1 = new JTextField(10); JTextField text2 = new JTextField(10); JTextField text3 = new JTextField(10); JTextField text4 = new JTextField(10); JTextField text5 = new JTextField(10); JTextField text6 = new JTextField(10); JTextField text7 = new JTextField(10); JTextField text8 = new JTextField(10); panel.add(text1); panel.add(text2); panel.add(text3); panel.add(text4); panel.add(text5); panel.add(text6); panel.add(text7); panel.add(text8); JOptionPane.showMessageDialog(null, panel); System.out.println(Integer.parseInt(text1.getText()) + Integer.parseInt(text2.getText()) + Integer.parseInt(text3.getText())+ Integer.parseInt(text4.getText())+ Integer.parseInt(text5.getText())+ Integer.parseInt(text6.getText())+ Integer.parseInt(text7.getText())+ Integer.parseInt(text8.getText())); } }
Output
Now, enter the numbers in the TextField and click OK −
On pressing OK above, the result would be visible in the Console that is sum of all the numbers added above −
- Related Articles
- Get number from user input and display in console with JavaScript
- How to add and remove input fields dynamically using jQuery with Bootstrap?
- How to add form validation for empty input fields with JavaScript?
- Customize the JOptionPane layout with updated color and image in Java
- Select all elements with “data-” attribute with jQuery and display on Console?
- How to use JOptionPane with Array Elements in Java?
- Can we set JOptionPane with predefined selection in Java?
- Display the month number with SimpleDateFormat(“M”) in Java
- Count by multiple fields with MongoDB aggregation
- Display multiple lines of text in a component’s tooltip with Java
- Style Input Fields of a form with CSS
- Prevent duplicates of multiple fields with index in MongoDB
- Ways to read input from console in Java
- Can we read from JOptionPane by requesting input from user in Java?
- Display a message on console while focusing on input type in JavaScript?

Advertisements