ram
ram

Reputation: 41

performing ActionListener within the JPanel

Here, i wanted to perform an action when a button is pressed, it is quite easy but the main problem is that to be performed within an extended JPanel. There might be an easy solution like add action listener to that particular button and invoke actionperformed event but my case is not like that, i have 4 buttons(t1,t2,t3,t4) all these buttons are to be functioned within a single ActionPerfomed event ae (look the segment of code). And later you can see the code that calls another JButtons tr and rf calling the actionlistener and actionperformed events.

for clear understandability you can have a glance at the code...

 class Tracker extends JPanel
{

public static void main(String[] args) {
    new Tracker();    }

public Tracker()
{

JButton tr=new JButton("TRACKER APPLET");
tr.setBounds(720,170,100,20);

JButton rf=new JButton("REFRESH");
rf.setBounds(200,170,100,20);

boolean tc1=false,tc2=false,tc3=false,tc4=false;

JButton t1=new JButton(" ");
t1.addActionListener(this);
JButton t2=new JButton(" ");
t2.addActionListener(this);
JButton t3=new JButton(" ");
t3.addActionListener(this);
JButton t4=new JButton(" ");
t4.addActionListener(this);


public void actionPerformed(ActionEvent ae)
{
    if(ae.getSource()==t1)
    {
            tc1=true;
    }
    if(ae.getSource()==t2)
    {
            tc2=true;
    }
    if(ae.getSource()==t3)
    {
            tc3=true;
    }
    if(ae.getSource()==t4)
    {
            tc4=true;
    }

}


tr.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException ex){}
            catch(InstantiationException ex){}
            catch(IllegalAccessException ex){}
            catch(UnsupportedLookAndFeelException ex) {}

                //some food here....


                           }
                        });
            }
             });        

    rf.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent e){

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run()
        {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } 
            catch (ClassNotFoundException ex){}
            catch(InstantiationException ex){}
            catch(IllegalAccessException ex){}
            catch(UnsupportedLookAndFeelException ex) {}
                         //some extra work runs here...
        }
    });
 }
    });
add(tr);
add(rf);
add(t1);
add(t2);
add(t3);
add(t4);
}

My problem here is i couldn't implement ActionListener as the main class already extending JPanel. I'm just trying to get work with Jbuttons :: wanted to perfom a single action when the buttons(t1|t2|t3|t4) and the JButtons(tr|rf) are pressed in a chronological order..

thanks in advance..

Upvotes: 1

Views: 11948

Answers (1)

Aubin
Aubin

Reputation: 14853

class Tracker extends JPanel implements ActionListener

Your code contains some methods into methods...

This code works:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Tracker extends JPanel implements ActionListener {
   public Tracker() {
      tr.setBounds( 720, 170, 100, 20 );
      tr.addActionListener( this );

      rf.setBounds( 200, 170, 100, 20 );
      rf.addActionListener( this );

      t1.addActionListener( this );
      t2.addActionListener( this );
      t3.addActionListener( this );
      t4.addActionListener( this );
      add(tr);
      add(rf);
      add(t1);
      add(t2);
      add(t3);
      add(t4);
   }

   @Override
   public void actionPerformed( ActionEvent e ) {
      Object src = e.getSource();
      if( src == t1 ) {
         tc1 = true;
      }
      else if( src == t2 ) {
         tc2 = true;
      }
      else if( src == t3 ) {
         tc3 = true;
      }
      else if( src == t4 ) {
         tc4 = true;
      }
      else if( src == tr ) {

      }
      else if( src == rf ) {

      }
   }

   private final JButton tr = new JButton( "TRACKER APPLET" );
   private final JButton rf = new JButton( "REFRESH" );
   private final JButton t1 = new JButton( " " );
   private final JButton t2 = new JButton( " " );
   private final JButton t3 = new JButton( " " );
   private final JButton t4 = new JButton( " " );
   boolean tc1 = false, tc2 = false, tc3 = false, tc4 = false;

   public static void main( String[] args ) {
      JFrame frame = new JFrame( "" );
      frame.add( new Tracker());
      frame.pack();
      frame.setLocationRelativeTo( null );
      frame.setVisible( true );
   }
}

Upvotes: 1

Related Questions