+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.
+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

+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) {
+1 Alex Sweps · November 12, 2014
Yeha, sorry iv been busy the last few days. Go back to your original problem of your handler running all of the change colors at the same time. All you need is a way to make them do it for the button you pressed.

Try some things out and let me know what you come up with. Watch some more tutorials. If I keep posting working code you will never learn how to solve the problems.

Think about using if statements and having it check what button is being pressed.
+1 Alex Sweps · October 31, 2014
Yeha I meant type mine out so your fingers are coding and you can try and understand whats going as you type each statement. Thats what I meant by exact. The code I wrote will change to a random color when you click the button. Type it exact and ask me anything you dont understand. 

And keep it in your saved projects so you can refer to it when you need an action handler again. 
0 Alex Sweps · November 7, 2014
Yeah that code was just typed in without checking to show you a way that you could achieve your goal. There are plenty of other ways so try and figure out how you can separate all each buttons action instead of running them all at once inside the handler.
0 Developer John · November 15, 2014
Okay, I thought you were creating  new String called button within those parameters. I understand now.
0 Developer John · November 10, 2014
Alex I don't know how to fix your code.
0 Developer John · October 28, 2014
How would you define the setColor method?
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());

Java / Android Development


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

Bucky Roberts Administrator