Solved

Java Swing:  getSource vs. getActionCommand

Posted on 2006-12-01
5
3,589 Views
Last Modified: 2013-11-23
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
Comment
Question by:duta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 18057413
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
 

Author Comment

by:duta
ID: 18057457
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 18057892
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
 

Author Comment

by:duta
ID: 18058121
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
 
LVL 92

Expert Comment

by:objects
ID: 18059534
i did above, what you already have is fine.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question