jbutton does not work

Hi I have the following code where I have a start and stop button . when start is pressed canvas starts painting but I cannot press stop button until the start operation is done . I need to stop and resume the paint on the button press . Once the start button is pressed the other buttons cannot be pressed till the canvas is painted.


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.Timer;


public abstract class JUIApp extends JPanel implements ActionListener {
    public static CACanvas cacanvas= null;
    public ArrayList<int[]> genL;
    public JFrame frame = null;
    protected JPanel mainPanel = null;
    private JButton btn0 = null;
    private JButton btn1 = null;
    private JButton btn2 = null;
    @SuppressWarnings("rawtypes")
    DefaultComboBoxModel rules = null;
    @SuppressWarnings("rawtypes")
    JComboBox rulesCombo =null;
    JScrollPane ruleListScrollPane=null;
    JLabel lable=null;
    JTextField generation=null;
    JLabel gLable=null;
    public static String Rule;
    public  static String Generations;
    public boolean isChanged =false;
    public int gridCellSize=4;
    private static final long serialVersionUID = 1L;
    public int genCurrent=0;
    public int posCurrent=0;
    public int i;
    public Color cellColor= null;
    public Timer waitTimer;
    public static boolean waitFlag;
    public static boolean alert1Flag=false;
    public boolean stopFlag=false;
    public JLabel Alert1=new JLabel();
    public int genCheck=0;
    //private List<Point> fillCells;
    public JUIApp() {
        initGUI();

    }
    public void initGUI() {
        //fillCells = new ArrayList<>(25);
        frame = new JFrame();
        frame.setTitle("Cellular Automata Demo");
        frame.setSize(1050, 610);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout(new BorderLayout());
        frame.add(getMainPanel(),BorderLayout.NORTH);
        frame.setVisible(true);
        Toolkit.getDefaultToolkit().setDynamicLayout(false); 
         cacanvas=new CACanvas();
            frame.add(cacanvas);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public JPanel getMainPanel() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new FlowLayout());
        //cacanvas=new CACanvas();

        btn0 = new JButton("Start");
        btn0.addActionListener(this);
        //waitTimer = new Timer(1000, this);
        mainPanel.add(btn0);

        JButton btn2 = new JButton("Stop");
        btn2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent s)
            {
                stopFlag=true;
                //cacanvas.repaint();
            }
        });
        mainPanel.add(btn2);
        btn1 = new JButton("Clear");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                CACanvas.clearFlag=true;
                generation.setText("");
                alert1Flag=true;
                rulesCombo.setSelectedIndex(0);
                  Alert1.setText("Please enter the number of generations");
                  Alert1.setBounds(30, 20, 5, 5);
                  Alert1.setVisible(alert1Flag);
                  mainPanel.add(Alert1);
                   cacanvas.repaint();
                   frame.setSize(1060, 610);

            }
    });
        mainPanel.add(btn1);
        lable=new JLabel();
        lable.setText("Select Rule :");
        mainPanel.add(lable);
        rules=new DefaultComboBoxModel();
        for (int i=0;i<=100;i++)
        {
            String p=String.valueOf(i);
        rules.addElement(p);
        }
        rules.addElement("250");
        rules.addElement("254");
        rulesCombo = new JComboBox(rules);    
        rulesCombo.setSelectedIndex(0);

         ruleListScrollPane = new JScrollPane(rulesCombo);
        mainPanel.add(ruleListScrollPane);
        //mainPanel.revalidate();
        gLable=new JLabel();
        gLable.setText("Enter the number of Generations (Max 64)");
        generation=new JTextField(2);

        mainPanel.add(gLable);
        mainPanel.add(generation);
       // mainPanel.add(cacanvas);
        return mainPanel;
    }


    public abstract void run();

    @Override
    public void actionPerformed(ActionEvent arg0) {
        Alert1.setVisible(false);
        waitFlag=false;
        System.out.println("We received an ActionEvent " + arg0);
         Generations=generation.getText();
        System.out.println(Generations);

         Rule = "";
        if (rulesCombo.getSelectedIndex() != -1) {                     
           Rule = 
                   (String) rulesCombo.getItemAt
                (rulesCombo.getSelectedIndex());             
        } 
        System.out.println(Rule);
        int rule=Integer.parseInt(Rule);
     Hashtable<String,Integer> rules= new Hashtable<String,Integer>();
        CARule ruleClass=new CARule();
        rules=ruleClass.setRule(rule);
        CAGenetationSet sa =new CAGenetationSet(100, false,rules);
         genL=new ArrayList<int[]>();
         genL=sa.runSteps(Integer.parseInt(Generations));
         System.out.println("calling pattern set");
        for(int i=0;i<=genL.size()-1;i++)
        {
            System.out.println("Painting generation :"+i);

            if(stopFlag==false)
            {

               cacanvas.repaint();
            }

            //genPaint();
            //sleep();
            int[] newCell=genL.get(i);
            for(int r=0;r<newCell.length;r++)
            {

                if(newCell[r]==1)
                {


                    System.out.println("Generaton is"+i+"CellLife is"+r);
                    cacanvas.fillCell(i,r);


        }
            }

        }




                    /*cacanvas.patternSet(genL);
                    waitFlag=true;


                  System.out.println("run completed");
                 // cacanvas.clearFlag=true;
*/  
    }   

    public void genPaint()
    {
        cacanvas.repaint();
    }

    public void sleep()
    {
        try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        JUIApp app = new JUIApp() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                System.out.println("Run method");

            }



    };
}
}

Answers


Seems like you are using the current Thread on that task, you should create another Thread and add one listeners code to the new Thread.


Need Your Help

segmentation fault when scaning file and counting values in c

c fopen scanf

I am scanning in a file and counting how many letters are uppercase, lowercase, numbers or other characters;