user3584711
user3584711

Reputation: 21

Make multiple JTextFields change color Java

I'm trying to make randomized JTextField's yellow by using Arrays. I've only managed to make 1 textfield yellow, and then white after 1 sec at each click. What I want to do is when you press the button the first randomized Textfield get yellow and then white. And when you press the second time the first randomzied textfield AND the seconds will turn yellow and then White and so on.. The array works as you can see I Printing it and when you press the button it prints all the randomized numbers in order.

The problem is that JTextField can't handle int's, and it apparently have to be "Final" so it makes it very hard to make multiply JTextField's yellow. I pasted my arrays to you so that you can get a better understanding what I'm trying to do. Does anyone know the solution?

//my Arrays

JTextField[] boxes = new JTextField[9]; //Array for the textfields
int[] clicked = new int[100];
int clickAmount = 0;

//At startup it fills the boxes array with the textfield:

 boxes[0] = textfield1;
 boxes[1] = textfield2;
 boxes[2] = textfield3;
 boxes[3] = textfield4;
 boxes[4] = textfield5;
 boxes[5] = textfield6;
 boxes[6] = textfield7;
 boxes[7] = textfield8;
 boxes[8] = textfield9;


 public void timePaus (final JTextField textfield) {

     new Timer(1000, new ActionListener() {
         public void actionPerformed (ActionEvent e) {
             textfield.setBackground(Color.white);
             // stop the timer
             ((Timer) e.getSource()).stop();
         }

     }).start();  

}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)  {   //Button

    int randomint = this.randomBox(); //Finds a number between 0-8
    final JTextField ThatTextfield = boxes[randomint]; //Puts a textfield into an array
    clicked[clickAmount] = randomint+1; //adds textfield number into an array
    clickAmount++;

    ThatTextfield.setBackground(Color.yellow); //make choosen textfield yellow


    for (int i = 0; i < clickAmount; i++)
    {
        timePaus(ThatTextfield); //make choosen textfield  white after 1 sec
        System.out.println(clicked[i]);  
    }
}

Upvotes: 1

Views: 276

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Considerations:

  • Place your JTextFields in an ArrayList<JTextField>, but create them in a for loop for the sake of avoiding unnecessary repetation.
  • When you want to randomize the ArrayList, call Collections.shuffle(myTextFieldList) on your List of JTextFields.
  • Give your class an int maxIndex field.
  • In your Swing timer, iterate through the now randomized List of JTextFields, displaying each one up to the value held by the maxIndex and not above the size of the List.
  • Increment the maxIndex in the timer.
  • Alternatively, you could create two more ArrayList<JTextField>, one to shuffle the collected text fields. Then remove the 0th JTextField from this list in your button's listener, and place it into the other ArrayList, and then iterate through the 2nd ArrayList's JTextfield in your Timer.

Upvotes: 3

Related Questions