Hi Folks!
I got to game (know as concentration or memory)code for my assignment.
I have found many similar tasks, but mine is a bit different.
Our teacher asked us to implement comboBox that we could switch
between images and text version.
So far I have made image one, and works fine.
Problem occurs while I'm trying to switch between them.
I mean switching isn't working so bad - it displays(resets)
image version and while switching to text shows in the command line 'choice2'
(that's what I set, as text version isn't ready yet)

It all would be great(as actually program works fine to me), but since I have
implemented comboBox it shows lots of errors in command line window.
I have taken screen shot of those errors - image file is in the attachments.

Code goes here:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import static java.util.Collections.*;

public class Assignment extends JFrame implements ActionListener
{
    private JButton[] gameButton = new JButton[16];
    private ArrayList<ImageIcon> gameList = new ArrayList<ImageIcon>();
    private JComboBox combo1;
    private String choices[] = {"Images version", "Font version"};
    private int counter = 0;
    private int score = 0;
    private int[] buttonID = new int[2];
    private ImageIcon[] buttonValue = new ImageIcon[16];
    //Icon bugIcon = new ImageIcon("bug1.gif");
    ImageIcon image1 = new ImageIcon(".\\img\\image1.jpg");
    ImageIcon image2 = new ImageIcon(".\\img\\image2.jpg");
    ImageIcon image3 = new ImageIcon(".\\img\\image3.jpg");
    ImageIcon image4 = new ImageIcon(".\\img\\image4.jpg");
    ImageIcon image5 = new ImageIcon(".\\img\\image5.jpg");
    ImageIcon image6 = new ImageIcon(".\\img\\image6.jpg");
    ImageIcon image7 = new ImageIcon(".\\img\\image7.jpg");
    ImageIcon image8 = new ImageIcon(".\\img\\image8.jpg");
    ImageIcon card = new ImageIcon(".\\img\\card.jpg");

    JLabel plainLabel = new JLabel();

	public JButton buttonC;
	JPanel panel = new JPanel();

	Container c = getContentPane();

    public Assignment()
    {
        init();
        panel();
        setArrayList();
        setTitle("Assignment 1: GUI Programming ");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public void setArrayList()
    {
        gameList.add(image1);
        gameList.add(image2);
        gameList.add(image3);
        gameList.add(image4);
        gameList.add(image5);
        gameList.add(image6);
        gameList.add(image7);
        gameList.add(image8);
        gameList.add(image1);
        gameList.add(image2);
        gameList.add(image3);
        gameList.add(image4);
        gameList.add(image5);
        gameList.add(image6);
        gameList.add(image7);
        gameList.add(image8);
        shuffle(gameList);
    }

    public void init()
    {

        for (int j = 0; j < gameButton.length; j++)
        {
            gameButton[j] = new JButton();
            gameButton[j].addActionListener(this);
        	gameButton[j].setIcon(card);
        }



    buttonC = new JButton("Reset game");
	buttonC.addActionListener(this);
	panel.add(buttonC);
    c.add(panel, BorderLayout.SOUTH);
	panel.add(plainLabel);

	Font plainFont = new Font("Arial", Font.ITALIC, 14);
	plainLabel.setText("Score " +score);

    plainLabel.setFont(plainFont);

    plainLabel.setToolTipText("Score " + score);

		combo1 = new JComboBox(choices);
		combo1.addActionListener(this);
	    panel.add(combo1);
//	  	c.add(panel);

}
    public void panel()

    {
        JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new GridLayout(4, 4));

        for (int j = 0; j < gameButton.length; j++)
        {
            gamePanel.add(gameButton[j]);

        }

        add(gamePanel, BorderLayout.CENTER);
    }

    public boolean sameValues()
    {

        if (buttonValue[0] == buttonValue[1])
        {
            return true;
        }

        return false;
    }

    public void actionPerformed(ActionEvent e)
    {

        for (int i = 0; i < gameButton.length; i++)
        {

            if (gameButton[i] == e.getSource())
            {
                gameButton[i].setIcon(gameList.get(i));
                gameButton[i].setEnabled(true);
                counter++;

                if (counter == 3)
                {

                    if (sameValues())
                    {
                        gameButton[buttonID[0]].setEnabled(false);
                        gameButton[buttonID[1]].setEnabled(false);

                    	score = score + 1;
                		plainLabel.setText("Score "+score);

                    }
                    else
                    {
                        gameButton[buttonID[0]].setEnabled(true);
                        gameButton[buttonID[0]].setIcon(card);
                        gameButton[buttonID[1]].setEnabled(true);
                        gameButton[buttonID[1]].setIcon(card);


                    }

                    counter = 1;
                }

                if (counter == 1)
                {
                    buttonID[0] = i;
                    buttonValue[0] = gameList.get(i);

                }

                if (counter == 2)
                {
                    buttonID[1] = i;
                    buttonValue[1] = gameList.get(i);

                }

            }

        }
		if(e.getSource()== buttonC) //reset button ;)
			{

    		 new Assignment();
   			}

		if (combo1 == (JComboBox)e.getSource());
		int Selection;

		Selection = combo1.getSelectedIndex();
		    if (Selection == 0)
		    {
		      new Assignment();
		    }
		    else if (Selection == 1)
		    {
      System.out.println("choice2");
  }



		/*

		if (combo1 == (JComboBox)e.getSource());
         //String choices = (String)combo1.getSelectedItem();


		//if(combo1.getSelectedItem() == "Images version")
		{
		System.out.println("choice1");
		new Assignment();
		}

		*/

		}


    public static void main(String[] args)
    {
        new Assignment();
    }

}

Can anyone advise, please?

Best regards
Tom

Folks, I'm stuck with this program.
Please advise :)

The error message says you cannot cast a JButton to a JComboBox at line 185. It's hard to imagine how that could be any clearer.
Line 185 says
if (combo1 == (JComboBox)e.getSource());
so if the event was triggered from a JButton that cast will fail.
Fortunately you don't need the cast, just like you didn't need it on line 179.

so, should I leave it like this, and just don't care? or is there anyway I could fix it?

Leave it like that if you want. It doesn't work, and crashes with an error message, but maybe that's OK?

Of course you can fix it. Look at the last line of my previous post. It tells you what you need to know, you just have to read it carefully and think about what to do with that information.

so, my code now looks like this
(let's say that this is line 179)

if(e.getSource()== buttonC) //reset button ;)
			{

    		 new Assignment();
   			}

		if(e.getSource()== "Images version")
		    {
		      new Assignment();
		    }
		if (combo1 == (JComboBox)e.getSource());
		{
		System.out.println("choice1");
		new Assignment();
		}
}

    public static void main(String[] args)
    {
        new Assignment();
    }

}

what happens now, I got rid of the errors, but my comboBox doesn't work - with other words - when I'm clicking on 'Image version' nothing happens.
Please help.

Why did you add that if test? It can't possibly work (an e.getSource() can never be a String), and what has that got to do with my post?
Go back to the previous version and remove the cast on line 185 which I already told you will cause the error and is unnecessary anyway..

so, if I get line 185 as "if (combo1 == e.getSource());"
than every time, I press any button(ie. card) program resets itself (is running 'new Assignment')

Debug logic errors like this by putting print statements into your code for each of the important if tests to see exactly which path is being taken through your code and what the values of the variables are. That will show you where its going wrong.

But in this particular case remove the ; immediately after your if test on 185. That completely changes the meaning in a bad way.

ok, so what I have done to fix it is line

if (e.getSource() instanceof JComboBox && combo1 == e.getSource())

now, it doesn't show any errors, but when I'm pressing either Image or Text version
it starts constructor new Assignment.

As I consider, now I need to implement kind of 'state changer' ?
is it right?
cause, how else would program know that I chose option form menu(combobox)
right?

You seem to be trying things at random here.
If combo1 == e.getSource() then obviously e.getSource() must be an instanceof JComboBox, so your change makes no difference to anything.

I already suggested how to use print statements to understand where your code is going wrong. I really have no further advice for the moment.

well what I've done now is:
Line 186:

if (combo1.getSelectedIndex() == 0)
		{
			System.out.println("choice 1");
		}

		if (combo1.getSelectedIndex() == 1)
		{
			System.out.println("choice 2");
		}

but now, it if I press any button, it always prints out the line
even if I don't pick anything from comboBox :(

ok, sorry for delay, but actually I was changing program.
thing with comboBox is been solved, problem that I have no is with 'refreshing'
window
while i'm switching between image and text(not finished yet) versions, screen doesn't seem to to change, I've tried things like revalidate or repaint, but with those screen looks ugly, some buttons are blinking and those that were pressed before are still pressed ;(
Can any one advise? I really have to finish this project ASAP

Best regards
Tom

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import static java.util.Collections.*;
public class Assignment extends JFrame implements ActionListener
{
    private JButton[] gameButton = new JButton[16];
    private ArrayList<ImageIcon> gameList = new ArrayList<ImageIcon>();
    private JComboBox combo1;
    private String choices[] = {"Images version", "Font version"};
    private int counter = 0;
    private int score = 0;
    private int[] buttonID = new int[2];
    private ImageIcon[] buttonValue = new ImageIcon[16];
    //Icon bugIcon = new ImageIcon("bug1.gif");
    ImageIcon image1 = new ImageIcon(".\\img\\image1.jpg");
    ImageIcon image2 = new ImageIcon(".\\img\\image2.jpg");
    ImageIcon image3 = new ImageIcon(".\\img\\image3.jpg");
    ImageIcon image4 = new ImageIcon(".\\img\\image4.jpg");
    ImageIcon image5 = new ImageIcon(".\\img\\image5.jpg");
    ImageIcon image6 = new ImageIcon(".\\img\\image6.jpg");
    ImageIcon image7 = new ImageIcon(".\\img\\image7.jpg");
    ImageIcon image8 = new ImageIcon(".\\img\\image8.jpg");
    ImageIcon card = new ImageIcon(".\\img\\card.jpg");
    JLabel plainLabel = new JLabel();
    public JButton buttonC;
    JPanel panel = new JPanel();
    private static final long serialVersionUID = 1L;
    Container c = getContentPane();
public Assignment()
    {
        init();
        this.imageInterface();
       // setArrayList();
        setTitle("Assignment 1: GUI Programming ");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }
private void imageInterface(){

JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new GridLayout(4, 4));
        for (int j = 0; j < gameButton.length; j++)
        {
            gamePanel.add(gameButton[j]);
        }
        add(gamePanel, BorderLayout.CENTER);
}
private void textInterface(){

JPanel gamePanel = new JPanel();
        gamePanel.setLayout(new GridLayout(4, 4));
        for (int j = 0; j < gameButton.length; j++)
        {

            gamePanel.add(gameButton[j]);
        }
        add(gamePanel, BorderLayout.CENTER);}
private void init()
{
        gameList.add(image1);
        gameList.add(image2);
        gameList.add(image3);
        gameList.add(image4);
        gameList.add(image5);
        gameList.add(image6);
        gameList.add(image7);
        gameList.add(image8);
        gameList.add(image1);
        gameList.add(image2);
        gameList.add(image3);
        gameList.add(image4);
        gameList.add(image5);
        gameList.add(image6);
        gameList.add(image7);
        gameList.add(image8);
        shuffle(gameList);

for (int j = 0; j < gameButton.length; j++)
        {
            gameButton[j] = new JButton();
            gameButton[j].addActionListener(this);
            gameButton[j].setIcon(card);
        }
    buttonC = new JButton("Reset game");
    buttonC.addActionListener(this);
    panel.add(buttonC);
    c.add(panel, BorderLayout.SOUTH);
    panel.add(plainLabel);
    Font plainFont = new Font("Arial", Font.ITALIC, 14);
    plainLabel.setText("Score " +score);
    plainLabel.setFont(plainFont);
    plainLabel.setToolTipText("Score " + score);
        combo1 = new JComboBox(choices);
        combo1.addActionListener(this);
        panel.add(combo1);
}
public boolean sameValues()
    {
        if (buttonValue[0] == buttonValue[1])
        {
            return true;
        }
        return false;
    }
    public void actionPerformed(ActionEvent e)
    {
        for (int i = 0; i < gameButton.length; i++)
        {
            if (gameButton[i] == e.getSource())
            {
                gameButton[i].setIcon(gameList.get(i));
                gameButton[i].setEnabled(true);
                counter++;
                if (counter == 3)
                {
                    if (sameValues())
                    {
                        gameButton[buttonID[0]].setEnabled(false);
                        gameButton[buttonID[1]].setEnabled(false);
                        score = score + 1;
                        plainLabel.setText("Score "+score);
                    }
                    else
                    {
                        gameButton[buttonID[0]].setEnabled(true);
                        gameButton[buttonID[0]].setIcon(card);
                        gameButton[buttonID[1]].setEnabled(true);
                        gameButton[buttonID[1]].setIcon(card);
                    }
                    counter = 1;
                }
                if (counter == 1)
                {
                    buttonID[0] = i;
                    buttonValue[0] = gameList.get(i);
                }
                if (counter == 2)
                {
                    buttonID[1] = i;
                    buttonValue[1] = gameList.get(i);
              }
            }
        }
        if(e.getSource()== buttonC) //reset button ;)
            {
             new Assignment();
               }
        if (combo1 == e.getSource())
                    if(combo1.getSelectedItem() == "Images version")
                    {
                        System.out.println("choice1");
                        this.imageInterface();
                    }
                    if(combo1.getSelectedItem() == "Font version")
                    {
                        System.out.println("choice2");
                        //new Assignment();
                        this.textInterface();
                    }

							//repaint();
					//panel.revalidate();

}

    public static void main(String[] args)
    {
        new Assignment();
    }
}

Folks, I'm stuck with this 'refreshing' problem since yesterday ;( .
I really need your help, I just can't fix it on my own.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.