Jump to content

JButton errors

- - - - -

  • Please log in to reply
5 replies to this topic

#1
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
Hey guys, I'm having trouble getting my JButtons to display the way I want them to. I want to have 4 buttons of different colors. The way I'm doing it now is to create an array of 4 buttons and give them values inside a for loop; the only problem is they all stay the same color. Here's my code: I think I have an error in my getNextColor method but I can't find it.


	Gui()

	{

		JPanel j = new JPanel();

		getContentPane().add(j);

		j.setLayout(null);

		

		int width = 35;

		JButton[] buttons = new JButton[4];

		for(int i = 0; i < 4; i++)

		{

			buttons[i] = new JButton();

			buttons[i].setBackground(getNextColor());

			buttons[i].setBounds(width,300,30,30);

			width += 60;

			buttons[i].addActionListener(new ActionListener()

			{

				public void actionPerformed(ActionEvent e)

				{

					System.exit(0);

				}

			});

			j.add(buttons[i]);

		}

				

		setTitle("Color-Changer");

		setSize(300,400);

		setLocationRelativeTo(null);

		setDefaultCloseOperation(EXIT_ON_CLOSE);

		setVisible(true);

	}


	private static Color getNextColor()

	{

		Color[] color = {Color.WHITE, Color.BLUE, Color.RED, Color.DARK_GRAY};

		for(int i = 0; i < 4; i++)

			return color[i];

		return Color.BLUE;

	}


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
If I'm reading this correctly, the getNextColor() method will always return Color.WHITE;

#3
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts

lethalwire said:

If I'm reading this correctly, the getNextColor() method will always return Color.WHITE;

Yeah, that's where my problem is. I don't know how to get the next call of getNextColor() to return the next index in the array.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
If the function can return a different value each time it is called with the same arguments, this is known as a "state", and as such you need to be saving some sort of value. I would recommend using a ColorDispenser object instead of this static method:
class ColorDispenser

{

    public ColorDispenser()

    {

        this(new Color[] {Color.WHITE, Color.BLUE, Color.RED, Color.DARK_GRAY});

    }


    public ColorDispenser(Color[] setToChooseFrom)

    {

        colors = setToChooseFrom;

        curSelection = 0;

    }


    public Color getNextColor()

    {

        if (curSelection >= colors.length) curSelection = 0;


        return colors[curSelection++];

    }


    private Color[] colors;

    private int curSelection;

}

Then use that in your code as a replacement to getNextColor() in your method. You'll need to construct a new instance of this class to use these methods of course!
Wow I changed my sig!

#5
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
ZekeDragon's code will work.
If you would like to keep your code contained within one class I might modify getNextColor to:
getNextColor( int index ) {

    return color[ index ]; // add this line and be sure to remove any extra unnecessary  lines

You can then slightly modify the existing code where you create the JButtons to

buttons[i].setBackground(getNextColor( i ));


Either case works, but ZekeDragon's example is very elegant.

#6
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
Thanks Zeke and lethalwire, I went with Zeke's approach and it worked great, thanks a lot.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users