0

I created 2 classes in 1 file. One refers to notepad and second refers to loading screen progressBar.

But when I tried to execute it the progress bar frame remains on the screen and only close when I hit the close button. I want to close progress bar automatically without hit that close button after its 100% not the other frame.

public class swingNotePad extends MouseAdapter implements ActionListener,MouseListener{
JFrame f;
JTextArea txt;
JMenuBar mb;
JMenu m1,m2,m3,m4,m5;
JMenuItem mi1,mi2,mi3,mi4,mi5,mi6,mi7,mi8;
JPopupMenu pm;
JSeparator js,js2;
public void MethodswingNotePad(){
     f=new JFrame("Developed by UC");
     txt=new JTextArea();
     txt.setBounds(0,0,600,570);
     js=new JSeparator();                                                              
     mb=new JMenuBar();
     m1=new JMenu("File");
     js2=new JSeparator(SwingConstants.VERTICAL);
     m2=new JMenu("Edit");
     m3=new JMenu("Format");
     m4=new JMenu("View");
     m5=new JMenu("Help");
     mi1=new JMenuItem("Cut");
     mi2=new JMenuItem("Copy");
     mi3=new JMenuItem("Paste");
     mi4=new JMenuItem("Select All");
     mi5=new JMenuItem("cut");
     mi6=new JMenuItem("copy");
     mi7=new JMenuItem("paste");
     mi8=new JMenuItem("select all");
     m2.add(mi1);
     m2.addSeparator();
     m2.add(mi2);
     m2.addSeparator();
     m2.add(mi3);
     m2.addSeparator();
     m2.add(mi4);
     mi1.addActionListener(this);
     mi2.addActionListener(this);
     mi3.addActionListener(this);
     mi4.addActionListener(this);

     pm=new JPopupMenu();
     pm.add(mi5);pm.add(mi6);pm.add(mi7);pm.add(mi8);

     mb.add(m1);

    mb.add(m2);
    mb.add(m3);
    mb.add(m4);
    mb.add(m5);
     f.add(txt);f.add(pm);   
     f.setJMenuBar(mb);
     f.setSize(600,600);
     f.setLayout(null);
     f.setVisible(true);
     f.setDefaultCloseOperation(EXIT_ON_CLOSE);
 }
public void actionPerformed(ActionEvent e){
    if(e.getSource()==mi1){
        txt.cut();
    }
    if(e.getSource()==mi2){
        txt.copy();
    }
    if(e.getSource()==mi3){
        txt.paste();
    }
    if(e.getSource()==mi4){
        txt.selectAll();
    }
}
public static void main(String...args){
swingNotePad a=new swingNotePad();     
swingProgressBar2 obj=new swingProgressBar2();
obj.iterate();
a.MethodswingNotePad();
 }

 }



class swingProgressBar2 extends JFrame{
JProgressBar pb;
JLabel l;
int i=0,n=0;
swingProgressBar2(){
    pb=new JProgressBar(0,2000);
    pb.setBounds(50,60,150,40);
    l=new JLabel("Loading...");
    l.setBounds(120,20,100,20);
    add(pb);add(l);
    pb.setStringPainted(true);
    pb.setValue(0);
    setSize(300,300);
    setLayout(null);
    setVisible(true);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    if(i==2000){
        System.exit(0) ;   }
}
public void iterate(){
    while(i<2001){
        try{
            i+=100;
            pb.setValue(i);
            Thread.sleep(75);

        }
        catch(Exception e){
            System.out.println(e);
        }
    }   
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
UC57
  • 359
  • 4
  • 16
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) 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). 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). .. – Andrew Thompson Jul 22 '18 at 03:42
  • .. 4) 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! 5) The code is updating the progress bar incorrectly. It instead should use a Swing `Timer` to update the progress bar (or using the current approach, put the calls to GUI components on the EDT). 6) See also [`ProgressMonitor`](https://docs.oracle.com/javase/9/docs/api/javax/swing/ProgressMonitor.html) which incorporates a progress bar and a dialog to display it in. – Andrew Thompson Jul 22 '18 at 03:44

1 Answers1

0

You can't System.exit(0) in the constructor. The iteration wont even be started when that code finishes. Try putting the code in your iterate method. And use dispose() instead cause System.exit(0) will exit the whole application.

e.g.

public void iterate(){
 while(i<2001){
  try{
   i+=100;
   pb.setValue(i);
   Thread.sleep(75);

  }catch(Exception e){
   System.out.println(e);
  }
 }   
 dispose();
}
Ogiez
  • 97
  • 6