Results 1 to 2 of 2

Thread: getActionCommand[Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    103

    getActionCommand[Resolved]

    okay, buttons A, B, C, D, and E.

    how do i make it so that, if button A, B, or C is pressed, do method1, else do method2? i'm using getActionCommand, not getSource.

    i tried something along the lines of:

    String in = e.getActionCommand();

    if in.indexOf("ABC") > -1 //found
    method1();
    else method2(); //not found
    Last edited by Dilenger4; Jun 27th, 2003 at 09:43 PM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;
    
      public class Test3{
         public static void main(String[] args){
    
         JFrame jf = new JFrame(); 
         Container c = jf.getContentPane(); 
         JButton[] jb = new JButton[5];
     
         for(int i = 0; i < jb.length; ++i){
          jb[i] = new JButton(String.valueOf(i));
          jb[i].addActionListener(new ButtonListener()); 
         }
    
         c.add(jb[0],BorderLayout.NORTH); 
         c.add(jb[1],BorderLayout.EAST); 
         c.add(jb[2],BorderLayout.SOUTH); 
         c.add(jb[3],BorderLayout.WEST); 
         c.add(jb[4],BorderLayout.CENTER); 
    
         jf.addWindowListener(new WindowAdapter(){
          public void windowClosing(WindowEvent evt){
           System.exit(0);
         }
        });
      
         jf.setSize(400,300);
         jf.setVisible(true);   
     }
    }
    
     class ButtonListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
       String s = new String(e.getActionCommand()); 
       if(s.equals("0") || s.equals("1") || s.equals("2")){
        method1(); 
       }else{
          method2();
      }
     }
     private void method1(){System.out.println("Method 1");}
     private void method2(){System.out.println("Method 2");}
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width