Total members 10259 | Gratitudes |It is currently Mon May 21, 2012 4:05 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Topic
 Topic subject: JProgressbar Sum of 'n' Numbers
PostPosted: Fri Oct 17, 2008 12:38 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2277
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

* Project Name:   JProgressbar Sum of 'n' Numbers
* Programmer:   Arun Kumar Jain
* Type:   Java Swing
* Technology:  Java
* IDE:   Any
* Description:   A demonstration of JProgress bar which
computes and shows the progress of
Sum of 'n' numbers.


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);
            }
        }
    }
}

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Remove ordered list numbers
 ordered list with roman numbers
 Help for jprogressbar of java
 Elements of the enumerated array are numbers
 Complex numbers calculator (C++)
 Complex Numbers

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Topic | Next Topic 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team