Problem in actionListener


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problem in actionListener

  1. #1
    Join Date
    May 2005
    Posts
    1

    Problem in actionListener

    Dear friends,

    I'm new in JAVA programming.I tried 2 do a program called Repeat Grid Color. When I click the enter button, the grid will changing colors randomly.If I press the stop button then the changing color will stop. And If I click the start button again, it will cont changing. The problem is when I click the enter button, the program will stuck. But If i put the loopGrid() method in the constructor. It work perfectly. Please help if anyone know how to solve the problem. Thank you.


    regards
    plan_ever




    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;




    public class RepeatGridColor extends JFrame {

    public JPanel jpanel1,jpanel2,jpanel3;
    JButton start,stop;
    Container con = getContentPane();
    Color colour [] = {Color.BLUE,Color.GREEN,Color.RED,Color.YELLOW};
    static int counter = 0;


    public RepeatGridColor(){


    setTitle("Repeating Grid Color");
    setSize(500,500);
    setLocation(300,100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setup();
    setup2();
    loopGrid();

    setVisible(true);

    }

    public void setup(){

    jpanel1 = new JPanel();
    start = new JButton("START");

    start.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){

    System.out.println(1);
    loopGrid();


    }
    });


    stop = new JButton("STOP");

    stop.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){


    counter = 1;

    }


    });


    jpanel1.add(start);
    jpanel1.add(stop);
    con.add(jpanel1,BorderLayout.NORTH);
    }

    public void setup2(){

    jpanel2 = new JPanel();

    jpanel2.setBackground(Color.white);






    }


    public void grid(){

    int x = 5;
    int y = 5;

    jpanel2 = new JPanel();

    jpanel2.setLayout(new GridLayout(x,y));

    for(int i = 0;i<x;i++){
    for (int j = 0;j<y;j++){

    JPanel p = new JPanel();
    JButton b = new JButton("Enter");
    p.setBackground(colour[(int)(Math.random()*colour.length)]);
    jpanel2.add(p);

    }

    }


    con.add(jpanel2,BorderLayout.CENTER);

    }

    public void loopGrid(){

    for (int i = 0; i <100; i++){
    for (int j = 0; j<100;j++){

    con.remove(jpanel2);
    grid();



    }
    setVisible(true);
    }

    }



    public static void main(String args[]){

    new RepeatGridColor();


    }

    }

  2. #2
    Join Date
    May 2005
    Posts
    75
    Hi,
    I've been looking at the code and there's a couple things that I don't understand.

    When you hit the Stop button nothing happens except that counter is changed to 1. Counter doesn't seem to serve any purpose, which means that hitting the stop button doesn't stop the gridLoop().

    Also, I don't see where the enter button is added to the pane. I tried adding it myself where it's delcared and I got a squished button next to each color block.

    jpanel3 is declared but never instantiated.

    I'm going to keep looking at to see why it freezes when Start is pressed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center