Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3851
  • Last Modified:

Java Swing: getSource vs. getActionCommand

Dear experts:

Hi again!

I gave a couple of questions:

First, I have a simple code below to play a lottery game. In actionPerformed, I used getActionCommand. But someone commented that getSource may be better to use that getActionCommand.  Can you kindly modify the code to use getSource, instead of getActionCommand.

Second, the someone also said that, with Java 5, content panes are no longer needed, and I need to remove the creation of the content pane and all later reference to it.

Can you kindly modify my code so that content panes may be removed?

Thanks a lot!

duta

__________________________  Here is my complete code _______________________________


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

public class Lottery extends JApplet
{
  private JFrame     myWindow;
  private final int  [ ] theNumber= { 3, 7, 5, 8, 6, 2 };
  private JTextField tTop1;
  private JTextField tTop2;
  private JTextField tTop3;
  private JTextField tTop4;
  private JTextField tTop5;
  private JTextField tTop6;
 
  private JTextField tCenter1;
  private JTextField tCenter2;
  private JTextField tCenter3;
  private JTextField tCenter4;
  private JTextField tCenter5;
  private JTextField tCenter6;
  private JTextField tBottom1;
  private JTextField tBottom2;
  private JTextField tBottom3;
  private JTextField tBottom4;
  private JTextField tBottom5;
  private JTextField tBottom6;
  private JTextField tYouWon;
  int t1, t2, t3, t4, t5, t6 = 0;
 
  public void init ( )
  {
    Container c = this.getContentPane ();
    myWindow = new JFrame ();
    myWindow.setSize ( 400, 500 );
    c.setLayout ( new BorderLayout () );
    c.add ( TopPanel    (), BorderLayout.NORTH );
    c.add ( CenterPanel (), BorderLayout.CENTER );
    c.add ( BottomPanel (), BorderLayout.SOUTH );
  }
 
  private JPanel TopPanel ()
  {
    JPanel pnl = new JPanel ();
    Font   fnt = new Font ( null, Font.PLAIN, 20 );
    JLabel lbl = new JLabel ( "$1 Million Lottery" );
    lbl.setFont ( fnt );
    pnl.add ( lbl );
    return pnl;
  }
 
  private JPanel CenterPanel ()
  {
    JPanel  pnl = new JPanel ();
    pnl.setLayout ( new GridLayout ( 4, 1 ) );
    pnl.add ( FirstPanel () );
    pnl.add ( SecondPanel() );
    pnl.add ( ThirdPanel () );
    pnl.add ( FourthPanel() );
    return pnl;
  }
 
  private JPanel FirstPanel ()
  {
    JPanel  pnl =     new JPanel ();
    JLabel lblFirst = new JLabel ( "Your Drawing :    " );
    tTop1 = new JTextField ( 1 );
    tTop2 = new JTextField ( 1 );
    tTop3 = new JTextField ( 1 );
    tTop4 = new JTextField ( 1 );
    tTop5 = new JTextField ( 1 );
    tTop6 = new JTextField ( 1 );
    pnl.add ( lblFirst );
    pnl.add ( tTop1 );
    pnl.add ( tTop2 );
    pnl.add ( tTop3 );
    pnl.add ( tTop4 );
    pnl.add ( tTop5 );
    pnl.add ( tTop6 );
    return pnl;
  }                    
                     
  private JPanel SecondPanel ()
  {
    JPanel  pnl =      new JPanel ();
 
    JLabel lblSecond = new JLabel ( "Right numbers : " );
    tCenter1 = new JTextField ( 1 );
    tCenter2 = new JTextField ( 1);
    tCenter3 = new JTextField ( 1 );
    tCenter4 = new JTextField ( 1 );
    tCenter5 = new JTextField ( 1 );
    tCenter6 = new JTextField ( 1 );
    pnl.add ( lblSecond );
    pnl.add ( tCenter1 );
    pnl.add ( tCenter2 );
    pnl.add ( tCenter3 );
    pnl.add ( tCenter4 );
    pnl.add ( tCenter5 );
    pnl.add ( tCenter6 );
    return pnl;
  }                    
                     
  private JPanel ThirdPanel ()
  {
    JPanel  pnl     = new JPanel ();
   
    JLabel lblThird = new JLabel ( "Matched :            " );
    tBottom1 = new JTextField ( 1 );
    tBottom2 = new JTextField ( 1 );
    tBottom3 = new JTextField ( 1 );
    tBottom4 = new JTextField ( 1 );
    tBottom5 = new JTextField ( 1 );
    tBottom6 = new JTextField ( 1 );
    pnl.add ( lblThird );
    pnl.add ( tBottom1 );
    pnl.add ( tBottom2 );
    pnl.add ( tBottom3 );
    pnl.add ( tBottom4 );
    pnl.add ( tBottom5 );
    pnl.add ( tBottom6 );
    return pnl;
  }                    
 
  private JPanel FourthPanel ()
  {
    JPanel  pnl     = new JPanel ();
    JLabel lblYouWon = new JLabel ( "Win / Lose : " );
    tYouWon = new JTextField ( 12);
    tYouWon.setBackground ( Color.white);
    tYouWon.setEditable (false);
    pnl.add ( lblYouWon );
    pnl.add ( tYouWon );
    return pnl;
  }          
   private JPanel BottomPanel ()
  {
      JPanel pnl = new JPanel ();
      JButton bDraw  = new JButton ( "Draw" );
      JButton bMatch = new JButton ( "Match" );
      JButton bReset = new JButton ( "Reset" );
      bDraw.setActionCommand  ( "draw" );
      bMatch.setActionCommand ( "match" );
      bReset.setActionCommand ( "reset" );
      ButtonListener bl  = new ButtonListener ();
      bDraw.addActionListener  ( bl );
      bMatch.addActionListener  ( bl );
      bReset.addActionListener ( bl );
      pnl.add ( bDraw );
      pnl.add ( bMatch );
      pnl.add ( bReset );
      return pnl;
  }
   
   private void Reset ()
   {
     tTop1.setText ( " " );
     tTop2.setText ( " " );
     tTop3.setText ( " " );
     tTop4.setText ( " " );
     tTop5.setText ( " " );
     tTop6.setText ( " " );
     tCenter1.setText ( " " );
     tCenter2.setText ( " " );
     tCenter3.setText ( " " );
     tCenter4.setText ( " " );
     tCenter5.setText ( " " );
     tCenter6.setText ( " " );
     tYouWon.setText   ( " " );
     tBottom1.setText (" ");
     tBottom2.setText (" ");
     tBottom3.setText (" ");
     tBottom4.setText (" ");
     tBottom5.setText (" ");
     tBottom6.setText (" ");
   }
   
   
   private  class ButtonListener implements ActionListener
  {
    public void actionPerformed ( ActionEvent e )
    {
            int num = 0;
           
            if ( "draw".equals ( e.getActionCommand () ) )
            {
              try
              {
              t1 = (int) (Math.random () * 10 );
       
              tTop1.setText ( "" + t1 );
             
              t2 = (int) (Math.random () * 10 );
              tTop2.setText ( "" + t2 );
             
              t3 = (int) (Math.random () * 10 );
              tTop3.setText ( "" + t3 );
             
              t4 = (int) (Math.random () * 10 );
              tTop4.setText ( "" + t4 );
             
              t5 = (int) (Math.random () * 10 );
              tTop5.setText ( "" + t5 );
             
              t6 = (int) (Math.random () * 10 );
              tTop6.setText ( "" + t6 );
              }
              catch (NumberFormatException ne )
              {
                System.out.println (ne);
              }
           }    
     
           else if ( "match".equals ( e.getActionCommand () ) )
           {
               tCenter1.setText ( "" + theNumber[0] );
               tCenter2.setText ( "" + theNumber[1] );
               tCenter3.setText ( "" + theNumber[2] );
               tCenter4.setText ( "" + theNumber[3] );
               tCenter5.setText ( "" + theNumber[4] );
               tCenter6.setText ( "" + theNumber[5] );
               int count = 0;
               
                if (  t1 == theNumber[0] )
                {
               
                 tBottom1.setText ("N");
                 count++;
                }
                else
                  tBottom1.setText (" ");
               
                if (  t2 == theNumber[1] )
                {
                 tBottom2.setText ("N");
                 count++;
                }
                else
                  tBottom2.setText (" ");
                 
                if (  t3 == theNumber[2] )
                {
                 tBottom3.setText ("N");
                  count++;
                }
                else
                  tBottom3.setText (" ");
                             
                if (  t4 == theNumber[3] )
                {
                 tBottom4.setText ("N");
                  count++;
                }
                else
                  tBottom4.setText (" ");
                 
                if (  t5 == theNumber[4] )
                {
                   tBottom5.setText ("N");
                    count++;
                }
                else
                  tBottom5.setText (" ");
                           
                if (  t6 == theNumber[5] )
                {
                 tBottom6.setText ("N");
                  count++;
                }
                else
                  tBottom6.setText (" ");
                 if ( count > 5 )
                   tYouWon.setText ( " Bingo. You won $1 million." );
                 else
                   tYouWon.setText ("    " + count + " number(s) right");
           }
         
           else if ( "reset".equals ( e.getActionCommand () ) )  
           {
               Reset ();
           }
     }  
   }                  
}                        
                     
  ________________________________  The end of my code ____________________________________                  
                     
   
 
 
0
duta
Asked:
duta
  • 3
  • 2
1 Solution
 
objectsCommented:
getSource() returns the source of the event (ie. what generated it)
getActionCommand() returns the action command property

so in your case if you want to check if a specific button was pressed use getSource(),
if you want to know if a button with a certain command is pressed the use getActionCommand()

your use of content pane can stay as it is
0
 
dutaAuthor Commented:
TO: objects:
Thank you so much for your kind, prompt response.

For my code, which command (between getSource and getActionCommand) is better to use?
If I want to replace getActionCommand with getSource (if I may or if I have to), how do I need to modify the code?
I have never used getSource, I am totally dark about how to use it.

Thanks again!

duta
0
 
objectsCommented:
getActionCommand() allows more than one button to perform the same action making it more flexible
i don't see a need to change it
0
 
dutaAuthor Commented:
TO: objects:

Thanks again for your kind, prompt response.

By the way, can you kindly answer my second question?

My second question is:

Someone also said that, with Java 5, content panes are no longer needed, and I need to remove the creation of the content pane and all later reference to it.


0
 
objectsCommented:
i did above, what you already have is fine.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now