+2 Developer John · October 25, 2014
How can I use Java Random to loop through 6 variables?
For an example: 
I have a Gui windows, and I want it to choose one color out of the six declared in Java Random.

Post a Reply


- page 1
Oldest  Newest  Rating
+3 Alex Sweps · October 26, 2014
Color[] myColors = {Color.BLACK, Color.BLUE, Color.WHITE, Color.GREEN, Color.RED, Color.PINK};

Random rand = new Random();


Essentially you have an array of colors and you use Random to pick a number between 1 and 6 from the array. Reason its nextInt(5) is because it will count 0 as the first index in the array.
0 Developer John · October 28, 2014
How would you define the setColor method?
+2 Alex Sweps · October 28, 2014
Wherever you want to set the random color just use

myColors[rand.nextInt(5)] instead of setting it like you normally would with Color.GREEN etc

0 Developer John · October 29, 2014
I have no idea what I'm doing wrong here:

public class HandlerClass3 implements ActionListener{
        public void actionPerformed(ActionEvent event){
        Random random = new Random();
        Color[] colors = {Color.ORANGE, Color.RED, Color.MAGENTA, Color.GREEN, Color.CYAN, Color.BLUE};
        Button1 but1 = new Button1();
//I use HIDE_ON_CLOSE so that all of the program doesn't shut down, just the window hides
        public class Button1 extends JFrame{
            public Button1(){
                super("Button1 displays a random color");
                setLayout(new FlowLayout());
0 Alex Sweps · October 29, 2014
Il fix up your code later. Dont really have time atm but I will get to it soon. 
0 Developer John · October 29, 2014
K, thanks!
+1 Alex Sweps · October 29, 2014
Ok, so iv written the program for you to help you understand action listeners. You action handler is doing way too much. When you want a button to do only one thing like change the color that is the only code it should have in it. The way you want to approach any project is start with the basics and test each step of the way. For this program I started with just getting a JFrame on screen. Tested it. Then went about adding a button. Then I changed the color of the button to make sure it would work. Then created the random setColor method and tested it by changing the color of the button in the constructor: button.setBackground(setColor());

I add my action handlers a bit different because to me it looks nicer and I find it easier to keep track of. Dont straight copy paste this code. I recommend writing it line by line and testing each step of the way to get used to coding like that. You will catch errors as they happen and wont get to the end with 100000 errors and wonder where they are :P

Run my code below and let me know if you have any questions:


import javax.swing.JFrame;

public class Main{

public static void main(String[] args){

new Gui();


import java.awt.Button;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;

public class Gui extends JFrame{

Color[] myColors = {Color.ORANGE, Color.RED, Color.MAGENTA, Color.GREEN, Color.CYAN, Color.PINK};
Random rand = new Random();
Button button = new Button("Button 1");

//Constructor. Create this first and test it each step of the way. 
public Gui() {
super("Button1 displays a random color");
setSize(500, 300);
button.addActionListener(new changeColor()); //When I add the action listener im creating a new changeColor class and that calls the action performed. Dont add this at first. Do it later once youv tested your program is working as expected.

//Create this second and add it to your constructor
private Color setColor(){
return myColors[rand.nextInt(6)];

//Finally add the action listener and test. 
class changeColor implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
0 Developer John · October 30, 2014
For some reason, it tells me that colors and random cannot be resolved:

        public Color setColor(){
        return colors[random.nextInt(5)];
        //5 because the array starts at 0. If I put six, there would be no such thing.
0 Alex Sweps · October 30, 2014
Did you name your Random, rand or random?

Random rand = new Random();
Random random = new Random();
-1 Developer John · October 30, 2014
Random random = new Random();

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator