Link to home
Start Free TrialLog in
Avatar of Drop_of_Rain
Drop_of_Rain

asked on

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
}
}
}
}
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Drop_of_Rain
Drop_of_Rain

ASKER

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?
My solution works for any panel's so you can try it and see the results.
Thanks You guys are very active tonight, I will have to take advantage of this!
Sure, I'm pretty active today but your also fast and did saw my other solutions
on some of your questions.
Yes I did that is why I made the comment!

Thanks
Thanks for the support