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

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.

@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

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");
            }


    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.