Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Setting Icons and checking for matches

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Thanked 0 Times in 0 Posts

    Default Setting Icons and checking for matches

    I'm having some trouble figuring out how to set and check for matches in my program. It is a 4x4 grid matching game. Any help is appreciated.

        public class MemoryGUI extends JPanel
          JPanel icons;
          JPanel score;
       	//instance variables
          private JPanel symbolPanel, turnPanel;
          private IconHolder icons;
          private JLabel turnLabel;
          private JTextField turnField;
           public MemoryGUI()
             JPanel p = new JPanel("*");
             p.setLayout(new GridLayout(4,4));
             for (int i = 0; i < 16; i++)
                JButton button = icons.buttonAtIndex(i);
          // instantiate all above components
             JPanel symbolPanel = new JPanel();
             JPanel turnPanel = new JPanel();
             turnLabel = new JLabel("Turns left: ");
             turnField = new JTextField(4);
          // set button sizes and listeners
             icons.buttonAtIndex(0).setPreferredSize(new Dimension(100, 100));
             for(int i = 0; i < 16; i++){
                icons.buttonAtIndex(i).addActionListener(new FlipListener());
          	// set panel layouts
             this.setLayout(new BoxLayout(this, BOXLAYOUT.Y_AXIS));
             symbolPanel.setLayout(new GridLayout(4, 4));
          // you may set display of buttons using Font class and setFont methods, use for loop as done above for setting font
             b.setFont(new Font("sansserif", Font.BOLD, 32));
          		// add components to the panel with the symbol buttons
             for(int i = 0; i < 16; i++){
          // add components to panel with turn information
          //set the text of turnField (JTextField)
             textField = new JTextField(20);      
           private class FlipListener implements ActionListener{
              public void actionPerformed(ActionEvent event){
                // determine which button has been pressed
                for(int i = 0; i < 16; i++){
                   icons.buttonAtIndex(i).addActionListener(new FlipListener(i));
             	// if this is first icon, set as first
             	//otherwise, set as second and check for match
             				// (within else) check for match, if so reset flips and set both to complete
             				 // if not matched, reduce turns by 1 and unflip
             	// refresh turns and end if turns is 0

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Setting Icons and checking for matches

    I am guessing the problem has to do with FlipListener? First, you do not need to add the listener again (the for loop in the listener). You can retrieve the source of the event using the ActionEvent getSource, and then use this object to do what you need
    public void actionPerformed(ActionEvent e){
        Object o = e.getSource();
        if ( o instanceof JButton ){
            JButton button = (JButton)o;//the button that was clicked
            ///do what you need with the button.

    Hope this helps...

Similar Threads

  1. Using Icons in JOptionPane
    By Jchang504 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 19th, 2014, 02:28 PM
  2. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM
  3. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM
  4. Replies: 2
    Last Post: March 4th, 2009, 06:32 AM
  5. Replies: 1
    Last Post: December 30th, 2008, 07:30 AM