Change color on focus

Hello everyone out there, another question.

I realized I need to ask you questions so you have them to answer!


How can I get the Jpanels color to get set on focus




import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JPanel {
  JLabel jLabel1;  
 
  ArrayList myArrayList = new ArrayList();

// Constructor
public MyClass4() {

    setLayout(new GridLayout(0,3)); // Setting MY layout...

add(jLabel1);  jLabel1.setText("Defensive Moves"); setBackground(Color.white);

    looprow:
        for (int row=0; row<20; row++) {
            for(int col=0; col<3; col++) {
                int n=(20*col+row+1);
                if (n>50) break looprow;
                JCheckBox check = new JCheckBox("" + n);

            myArrayList.add(check);
add(check); // Adding it to ME
}
}
}
}
Drop_of_RainAsked:
Who is Participating?
 
JavatmCommented:
Hi friend;

Here is something which will help :
http://java.sun.com/docs/books/tutorial/uiswing/events/focuslistener.html

Hope that helps . . .
Javatm
0
 
objectsCommented:
Use a FocusListener that and set the colour in the focuseGained() method.
0
 
Tommy BraasCommented:
Maybe you mean when the mouse pointer enters the the area of the JPanel! Add a class that implements the MouseListener interface to the JPanel, and you will receive MouseEvents like mouseEntered and mouseExited.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
JavatmCommented:
Here is what you do if its a mouse event :

  JPanel YourPanel = new JPanel();
 
  YourPanel.addMouseListener(new MouseAdapter() {
  public void mouseEntered(MouseEvent e) {
  YourPanel.setBackground(Color.blue);
  }
  public void mouseClicked(MouseEvent e) {
  YourPanel.setBackground(Color.red);
  }
  public void mouseExited(MouseEvent e) {
  YourPanel.setBackground(Color.white);
  }
  });

Hope that helps . . .
Javatm
0
 
Drop_of_RainAuthor Commented:
I will have about 12 JPanels with checkboxs with different colors. What do you think would be the best approach to help the user deal with all the checkbox groupings?
0
 
JavatmCommented:
My solution works for any panel's so you can try it and see the results.
0
 
Drop_of_RainAuthor Commented:
Thanks You guys are very active tonight, I will have to take advantage of this!
0
 
JavatmCommented:
Sure, I'm pretty active today but your also fast and did saw my other solutions
on some of your questions.
0
 
Drop_of_RainAuthor Commented:
Yes I did that is why I made the comment!

Thanks
0
 
Drop_of_RainAuthor Commented:
Thanks for the support
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.