0

I have created this Progress Code and now I want it to close automatically after going to 100% and also open a new Frame with Text in the frame. So how do I do this?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
class Progress extends JFrame implements ActionListener {

    JProgressBar pb;
    JButton b1;
    Progress() {
        super("Progress");
        setLayout(null);
        b1 = new JButton("Start");
        b1.setBackground(Color.LIGHT_GRAY);             
        pb = new JProgressBar(1,100);
        pb.setValue(0);
        pb.setStringPainted(true);
        pb.setForeground(Color.green);   
        pb.setBackground(Color.pink); 
        b1.setBounds(20, 20, 80, 25);
        pb.setBounds(110, 20, 200, 25);
        pb.setVisible(false);
        add(b1);
        add(pb);             
        b1.addActionListener(this);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        int i=0;
        if(e.getSource()==b1) {
            pb.setVisible(true);
            try {
                while(i<=100) {
                    Thread.sleep(50);
                    pb.paintImmediately(0, 0, 200, 25);
                    pb.setValue(i);
                    i++;
                }
            } catch(Exception e1) {
                System.out.print("Caughted exception is ="+e1);
            }
        }
    }

    public static void main(String arg[]) {
        logindemo m=new logindemo();
        m.setSize(400,250);
        m.setVisible(true);
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - m.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - m.getHeight()) / 2);
        m.setLocation(x, y); 
    }
}

This is the code for my Progress bar.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
JohnTJC
  • 9
  • 2
  • 3
    what's the relation between your code and your question title? – Karthik Jul 11 '15 at 07:18
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jul 11 '15 at 08:56
  • 1
    `setLayout(null);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Jul 11 '15 at 08:57
  • 1
    Don't edit posts by using the 'back button'! That will wipe out changes made by other people. Instead use the `edit` link below the tags. – Andrew Thompson Jul 11 '15 at 10:16

1 Answers1

1

So a thing before the answer.

ProgressBar

In your code while progress is increasing rest of the application is frozen. If you want to avoid it, it's good to have progress bar code in another Thread. But remember to use SwingWorker instead of classic Threads.

Also read the thing what Andrew Thompson mentioned in his comments.

To "close" or rather hide main frame u can call one of these methods

setVisible(false);

or

dispose();

To open new Window/Frame or whatever, you have to put it after the while loop. You can check it with code below, just add this method to your class and call it after the loop.

    private void dialogMessage() {
    Object[] options = {"OK"};
    int result = JOptionPane.showOptionDialog(this,
            "Done!", "",
            JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[0]);
    if (result == JOptionPane.OK_OPTION) {
        System.exit(0);
    }
}

To read more about JOptionPane check out this link How to Make Dialogs

Zano
  • 99
  • 1
  • 9