Results 1 to 4 of 4
  1. #1
    tib0lt is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Thread running multiple flashing buttons.

    Hey all,
    first time post. I've been trying to self teach java for the last couple weeks now and alittle hung up on multithreading multiple buttons flashing.

    Here is my code so far.

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

    class WhiteLight extends JFrame {

    public static void main (String[] args) {
    //Frame set up//
    JFrame frame = new JFrame ();
    frame.setAlwaysOnTop(true);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE );
    frame.setUndecorated(true);

    WhiteLightPanel panel = new WhiteLightPanel();

    frame.getContentPane().add(new WhiteLightPanel());
    frame.pack();
    frame.setVisible(true);


    }
    }



    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.lang.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    class WhiteLightPanel extends JPanel {

    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;

    public void changeColor (int buttonColor)
    {

    switch (buttonColor)
    {
    case 1:button1.setBackground(Color.black);break;
    case 2:button1.setBackground(Color.red);break;
    case 3:button2.setBackground(Color.black);break;
    case 4:button2.setBackground(Color.red);break;
    case 5:button3.setBackground(Color.black);break;
    case 6:button3.setBackground(Color.green);break;
    case 7:button4.setBackground(Color.black);break;
    case 8:button4.setBackground(Color.green);break;
    default:button1.setBackground(Color.black); break;
    }
    }



    public WhiteLightPanel () {

    //Panel set up//
    setPreferredSize(new Dimension(170,265));
    setBackground(Color.black);
    //Buttons set up//
    button1 = new JButton();
    button1.setPreferredSize(new Dimension(80,60));
    button1.setBackground(Color.red);
    button1.addActionListener(new ButtonListener(2));
    add(button1);
    button2 = new JButton();
    button2.setPreferredSize(new Dimension(80,60));
    button2.setBackground(Color.red);
    add(button2);
    button3 = new JButton();
    button3.setPreferredSize(new Dimension(80,60));
    button3.setBackground(Color.green);
    add(button3);
    button4 = new JButton();
    button4.setPreferredSize(new Dimension(80,60));
    button4.setBackground(Color.green);
    add(button4);
    button5 = new JButton();
    button5.setPreferredSize(new Dimension(80,60));
    button5.setBackground(Color.orange);
    add(button5);
    button6 = new JButton();
    button6.setPreferredSize(new Dimension(80,60));
    button6.setBackground(Color.orange);
    add(button6);
    button7 = new JButton();
    button7.setPreferredSize(new Dimension(80,60));
    button7.setBackground(Color.white);
    add(button7);
    button8 = new JButton();
    button8.setPreferredSize(new Dimension(80,60));
    button8.setBackground(Color.white);
    add(button8);
    }



    public class ButtonListener extends Thread implements ActionListener, Runnable {
    Thread runner;

    int Button=0;
    int i =1;
    int j =1;
    boolean onOff = true;

    public ButtonListener(int numb) {
    Button = numb;
    }

    public void actionPerformed (ActionEvent event) {
    System.out.println("Here");
    if (i%2==1) {
    turnOn();j = 1;System.out.println("onOff: "+onOff);start();}
    else { changeColor(Button);requestStop();System.out.print ln("J: "+j+"I: "+i+"onOff:"+onOff);}
    i++;
    }






    public void run() {
    onOff=true;
    while (onOff!=false) {
    if (j%2==1) {
    changeColor(Button);}
    else {changeColor(Button-1);}
    j++;
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    e.printStackTrace(); }
    }
    }
    public void requestStop() {
    onOff = false;
    }
    public void turnOn() {
    onOff = true;
    }




    }
    }




    I'm sure its sloppy at best. However, I am able to generate a window with multiple colored buttons and make one of them flash at the click of a button and then turn off with the second click.
    The issue I'm having is getting the button to start flashing once again at the third click. Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Thread running multiple flashing buttons.

    Please edit your post and wrap the code in code tags. See: BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: Thread running multiple flashing buttons.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    s4developer is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Re: Thread running multiple flashing buttons.

    In java you cannot call the run method directly...users have no influence on when a thread can start however
    try calling the run method after instan... the thread: then check to see if alive, then signal an interrupt cmd:


    Thread t = new Thread(r);
    t.start(); // overwrite implementing run();

    System.out.println("Thread is alive:" + t.isAlive() );
    t.interrupt();


    ALTERNATIVELY,

    //ThreadPool used to execute Callables
    ExecutorService es = Executors.newCachedThreadPool();
    Last edited by s4developer; 09-26-2012 at 08:07 PM.

Similar Threads

  1. Best Way to Run a method on multiple buttons
    By Dcalladi in forum New To Java
    Replies: 1
    Last Post: 11-07-2011, 04:45 AM
  2. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 04:48 PM
  3. while running multiple thread repaint isnt being called....
    By raptor in forum Threads and Synchronization
    Replies: 4
    Last Post: 08-26-2010, 12:47 AM
  4. multiple submit buttons on webpage
    By samanthamaryhorgan in forum Advanced Java
    Replies: 1
    Last Post: 03-04-2010, 03:51 AM
  5. One controller with multiple buttons and colors
    By Cutter in forum New To Java
    Replies: 5
    Last Post: 01-24-2010, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •