0

Hi!..I am Amrinder and i am new to java language as i am a student of Engg.

i have to make a program using Swings or Frame in which Loop would be handled by Two different Buttons named as Start & Stop.....but how can i make a loop in ActionEvent (actionPerformed) of a button which will start by click on a Button and break by clicking on the another Button...Please help me to do this

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Amrinder07
0

Not sure what you want to do in the loop, but maybe the Start button should start a new thread, and the Stop button can stop it? See this link.

0

@kramerd
actually i am studying the library Jpcap
and usin this i want to display the packets comming to my computer through internet and this is an infinite process

and i have to make an application GUI which contains 2 Buttons
1. Start ---> start capturing packets
2. Stop ---> Stop Capuring process

0

thanks kramerd...i did it
check it....to run it install Jpcap and Winpcap libraries

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import jpcap.*;

public class Assignment1 extends Frame implements WindowListener,ActionListener,ItemListener
{
      static NetworkInterface devices[] = JpcapCaptor.getDeviceList();
      Panel pn;
      static Button bt1,bt2;
      static Choice cb;
      static TextArea ta;
      static int index;

      Assignment1()
      {
          bt1=new Button("Start");
          bt2=new Button("Stop");
          cb=new Choice();
          ta=new TextArea(10, 85);

          pn=new Panel();

          pn.add(cb);
          pn.add(bt1);
          pn.add(bt2);
          pn.add(ta);


          add(pn);
          addWindowListener(this);
          bt1.addActionListener(this);
          bt2.addActionListener(this);
          cb.addItemListener(this);

       for(int i=0;i<devices.length;i++)
       {
           cb.addItem(i+". "+devices[i].description);
       }

         pack();
         bt1.setEnabled(false);
         bt2.setEnabled(false);
    }
      public static void main(String []args)
      {
           Assignment1 ass=new Assignment1();
           ass.setVisible(true);

      }


    public void windowOpened(WindowEvent e)
    {    }

    public void windowClosing(WindowEvent e)
    {
        this.dispose();
    }

    public void windowClosed(WindowEvent e)
    {    }

    public void windowIconified(WindowEvent e)
    {    }

    public void windowDeiconified(WindowEvent e)
    {    }

    public void windowActivated(WindowEvent e)
    {    }

    public void windowDeactivated(WindowEvent e)
    {
    }

    public void actionPerformed(ActionEvent e)
    {
        CaptAnal rn=new CaptAnal();
        Thread th=new Thread(rn);
       if(e.getSource()==bt1)
         {
           CaptAnal.flag=true;
            th.start();
            bt1.setEnabled(false);
            bt2.setEnabled(true);

         }

        else if(e.getSource()==bt2)
         {
            CaptAnal.flag=false;
            bt2.setEnabled(false);
            bt1.setEnabled(true);

         }

    }


    public void itemStateChanged(ItemEvent e)
    {
        index=cb.getSelectedIndex();
        bt1.setEnabled(true);
        bt2.setEnabled(false);
    }
}

class CaptAnal implements Runnable
{
   static Boolean flag;
    public void run()
    {
       int count=0;
        JpcapCaptor captor = null;
        try {
              captor = JpcapCaptor.openDevice(Assignment1.devices[Assignment1.index], 65535, false, 20);
            } catch (IOException ex)
            {
                System.out.println("Captor Error");
            }

        while(flag!=false)
            {
            count=count+1;
            Assignment1.ta.append(count+"-->  "+captor.getPacket()+"\n");
            }


    }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.