0

I have a gui class in which there are four buttons when a button is clicked the program should run an animation of a sorting algorithm. The problem is when i click a button it pops up a Jframe but nothing shows up on the jframe it appears black. Most probably because of the delay that i am using in Bubble Sort animation class. The screen goes black of the Jframe and when animation gets completed it shows the final result in the end. So does any one knows how to fix the problem?

    //This is the main GUI class

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class GUIMain {

        Bubble gui=new Bubble();
        Selection gui2=new Selection();

       private JFrame mainFrame;
       private JLabel headerLabel;
       private JLabel statusLabel;
       private JPanel controlPanel;

       public GUIMain(){
          prepareGUI();
       }

       public static void main(String[] args){
          GUIMain swingControlDemo = new GUIMain();  
          swingControlDemo.showEventDemo();       
       }

       private void prepareGUI(){
          mainFrame = new JFrame("Sorting Animations");
          mainFrame.setSize(400,400);
          mainFrame.setLayout(new GridLayout(3, 1));
          mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          //labels are for texts
          headerLabel = new JLabel("",JLabel.CENTER );
          statusLabel = new JLabel("",JLabel.CENTER);        

          statusLabel.setSize(350,100);
          mainFrame.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent windowEvent){
                System.exit(0);
             }        
          });
          //panel contains both the buttons and the text
          controlPanel = new JPanel();
          controlPanel.setLayout(new FlowLayout());

          mainFrame.add(headerLabel);
          mainFrame.add(controlPanel);
          mainFrame.add(statusLabel);
          mainFrame.setVisible(true);  
       }

       private void showEventDemo(){
          headerLabel.setText("Select any of the sorting algorithm!"); 

          JButton Insertion = new JButton("Insertion");
          JButton Selection = new JButton("Selection");
          JButton Bubble = new JButton("Bubble");
          JButton Shell = new JButton("Shell");
         // JButton Quick= new JButton ("Quick");

          Insertion.setActionCommand("Insertion");
          Selection.setActionCommand("Selection");
          Bubble.setActionCommand("Bubble");
          Shell.setActionCommand("Shell");
         // Quick.setActionCommand("Quick");

          Insertion.addActionListener(new ButtonClickListener()); 
          Selection.addActionListener(new ButtonClickListener()); 
          Bubble.addActionListener(new ButtonClickListener());
          Shell.addActionListener(new ButtonClickListener());
         // Quick.addActionListener(new ButtonClickListener());

          controlPanel.add(Insertion);
          controlPanel.add(Selection);
          controlPanel.add(Bubble);  
          controlPanel.add(Shell);
         // controlPanel.add(Quick);

          mainFrame.setVisible(true);  
       }

       private class ButtonClickListener implements ActionListener{
          public void actionPerformed(ActionEvent e) {
             String command = e.getActionCommand();  
             if( command.equals( "Insertion" ))  {
                statusLabel.setText("Insertion Button clicked.");

             }
             else if( command.equals( "Selection" ) )  {
                 try {
                    gui2.sort();
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                statusLabel.setText("Selection Button clicked."); 
             }
             else if(command.equals("Bubble"))
             {
                statusLabel.setText("Bubble Button clicked.");
                try {
                    gui.sort();
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
             }      
             else if(command.equals("Shell")){
                 statusLabel.setText("Shell Button clicked");
             }
             else
             {
                 statusLabel.setText("Quick Button clicked");
             }
          }     
       }
    }`






//and this is bubble sort class







import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import java.awt.*;

public class Bubble{

public static void main(String[] args) throws InterruptedException{
        Bubble b = new Bubble();
        b.sort();
    }

//////////////////////////////////
////SORT STARTS HER
/////////////////////////////////




public void sort() throws InterruptedException {
        JFrame frame = new JFrame("Bubble Sort");   //Naming the javaframe
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        int k =0;                                       //k=0
        frame.setLayout(null);                          //making layout null
        //  Border thickBorder = new LineBorder(Color.BLACK, 12);
          Border thin = new LineBorder(Color.RED, 12);
        JButton[][] buttons= new JButton [6][6];        //defining array for buttons and labels


        for(int i = 0 ; i<6; i++){
            buttons[i][0] = new JButton("7");
            buttons[i][1] = new JButton("4");
            buttons[i][2] = new JButton("3");
            buttons[i][3] =new JButton ("2");
            buttons[i][4] =new JButton("0");
            buttons[i][5] =new JButton("1");

        }   //for ends  
        for(int i = 0 ; i<6; i++){
            buttons[i][0].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][1].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][2].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][3].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][4].setFont(new Font("Verdana",Font.BOLD,22));
            buttons[i][5].setFont(new Font("Verdana",Font.BOLD,22));
        }

        int c,d,e,f;
        frame.setVisible(true);


        frame.setVisible(true);
        String temp;

        for (int i=0; i<buttons.length-1; i++)


        {   



            //main For Loop starts
            k++;
            if(k>=6){
                break;
            }
            for (int j=0; j<buttons[k].length-i-1; j++)     //sorting the buttons buttons[k].length=6
            {

                if (Integer.parseInt(buttons[k][j].getText())>Integer.parseInt(buttons[k][j+1].getText()))
                {
                    temp = buttons[k][j].getText();
                    buttons[k][j].setText(buttons[k][j+1].getText());   
                    buttons[k][j+1].setText(temp);
                    buttons[k][j+1].setBorder(thin);
                }// main if ends
                c=d=6;
                e=45;
                f=57;
                for(int l = 0 ; l<6; l++){
                    for(int v = 0; v < buttons[k].length; v++){
                        System.out.print(buttons[l][v].getText() + " ");

                        buttons[l][v].setBounds(c, d, 50, 53);

                        c+=102;
                        e+=110;
                        frame.add(buttons[l][v]);

                        buttons[l][j].setBackground(Color.WHITE);
                        buttons[l][j+1].setBackground(Color.WHITE);
                    }
                    System.out.println();
                    c=6;
                    e=45;
                    d+=76;
                    f+=78;
                }
            }//main for ends here

        }

        frame.setSize(700, 700);
        frame.setVisible(true);
        for(int j=1;j<6;j++){
            for(int i=0;i<buttons[0].length;i++){
                buttons[j][i].setVisible(false);

            }
        }
        for(int j=1;j<buttons[0].length;j++){
            Thread.sleep(200);
            for(int i=0;i<buttons[0].length;i++){   
                buttons[j][i].setVisible(true);

            }
        }
    }
}
Wiz
  • 17
  • 6
  • I also don't know what related problem to search on Google – Wiz Dec 30 '13 at 00:23
  • Can you supply us with source code? – JavaCake Dec 30 '13 at 00:24
  • 2
    `actionPerformed` is run from the event dispatch thread, and it's not good for something on the event dispatch thread to do something that takes a long time, such as `Thread.sleep` or a long calculation, because this prevents other Swing actions from getting done. You should look over http://docs.oracle.com/javase/tutorial/uiswing/concurrency/. – ajb Dec 30 '13 at 01:17
  • 1
    Also, by convention, variable names start with lower-case letters. Declaring a variable such as `JButton Bubble`, especially with the same name as a class, can be confusing to a reader. (It makes `Bubble.setActionCommand("Bubble")` look like a call to a static method in the `Bubble` class.) – ajb Dec 30 '13 at 01:19
  • Use a `SwingWorker` to pace the animation, as shown [here](http://stackoverflow.com/a/11372932/230513). – trashgod Dec 30 '13 at 02:31

0 Answers0