Solved

Java Swing:  getSource vs. getActionCommand

Posted on 2006-12-01
5
3,494 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
  • 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 108
ForLoop Example 3 49
iterator/ListIterator approach 17 27
CSV file parsing thru Java 13 31
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

809 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