Add to Favorites    Make Home Page 2465 Online  
 Language Categories  
 Our Services  

Home » Java Home » Java Swing Home » JProgressbar -- Sum of 'n' Numbers

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title JProgressbar -- Sum of 'n' Numbers
Author Arun Kumar Jain
Author Email akj_is [at] yahoo.com
Description A demonstration of JProgress bar which
computes and shows the progress of
Sum of 'n' numbers.

Category Java » Java Swing
Hits 392292
Code Select and Copy the Code
/* * <Applet code=TJProgressBar width=400 height=100> * </Applet> */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class TJProgressBar extends JApplet { Container container = null; JButton startButton, stopButton; JTextField inputTextField, outputTextField; JProgressBar pBar = null; Timer timer = null; static int sum = 0; static int counter = 0; public void init() { // 1. Get the handle on the content pane and // assign the grid layout. container = this.getContentPane(); container.setLayout(new GridLayout(3,1)); // 2. Add a horizontal box to the container. Box hbox1 = Box.createHorizontalBox(); container.add(hbox1); // 3. Add labels and input and output text fields // to the horizontal box. hbox1.add(Box.createHorizontalGlue()); JLabel label1 = new JLabel("Sum of first ", JLabel.LEFT); label1.setFont(new Font("Dialog", Font.PLAIN, 15)); hbox1.add(label1); inputTextField = new JTextField("100", 4); hbox1.add(inputTextField); JLabel label2 = new JLabel(" numbers is ", JLabel.LEFT); label2.setFont(new Font("Dialog", Font.PLAIN, 15)); hbox1.add(label2); outputTextField = new JTextField(10); hbox1.add(outputTextField); hbox1.add(Box.createHorizontalGlue()); // 4. Add another horizontal box to the container. Box hbox2 = Box.createHorizontalBox(); container.add(hbox2); // 5. Add Start and Stop buttons to the container. startButton = new JButton("Start"); startButton.addActionListener(new ButtonListener()); hbox2.add(Box.createHorizontalGlue()); hbox2.add(startButton); hbox2.add(Box.createHorizontalGlue()); stopButton = new JButton("Stop"); stopButton.addActionListener(new ButtonListener()); hbox2.add(Box.createHorizontalGlue()); hbox2.add(stopButton); hbox2.add(Box.createHorizontalGlue()); // 6. Create and add a progress bar to the remaining // display area. pBar = new JProgressBar(); pBar.setStringPainted(true); Border border = BorderFactory.createLineBorder(Color.red, 2); pBar.setBorder(border); pBar.setBackground(Color.white); pBar.setForeground(Color.blue); pBar.setMinimum(0); pBar.setMaximum(Integer.parseInt(inputTextField.getText())); container.add(pBar); // 7. Create a timer object. timer = new Timer(0, new TimerListener()); } // 8. Timer listener that computes the sum of natural numbers, // indicates the computation progress, and displays the result. class TimerListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (Integer.parseInt(inputTextField.getText())> 0){ counter++; sum = sum+counter; pBar.setValue(counter); outputTextField.setText(Integer.toString(sum)); } else { outputTextField.setText("0"); } if (counter >= Integer.parseInt(inputTextField.getText())) timer.stop(); } } // 9. Button listener that actually starts or stops the // process. class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); if (button.getText() == "Start") { outputTextField.setText(""); if (inputTextField.getText() != " ") { pBar.setMaximum(Integer.parseInt( inputTextField.getText())); sum = 0; counter = 0; timer.start(); } } else if (button.getText() == "Stop") { timer.stop(); outputTextField.setText(""); sum = 0; counter = 0; pBar.setValue(0); } } } }

Related Source Codes

Script Name Author
Sending mail Using JavaMail to Yahoo and Gmail accounts sai prasad
Simple Program in Java to Implement Multithreading Satish.K
Simple Calculator in Java Using Remote Method Invocation Satish.K
Guest Book Application Using Servlets Satish.K
String Manipulation Using Stringification Satish.K
String Manipulation Using Stringification Satish.K
Moving Ball Application Using Java Beans Satish.K
Rapid Roll game subrahmanyeswararao
student mgm arpan
Sourav Datta
Download Manager Sagar
Address Book in Java Rahul Chouhan
address book using java database connectivity(jdbc-msaccess) shekhar bansal
sun Steganography B.Rajavel
Connecting Java with MS-Access - Inserting data in Aseem

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1108


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review