Solved

Change color on focus

Posted on 2004-04-02
11
290 Views
Last Modified: 2010-03-31
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
}
}
}
}
0
Comment
Question by:Drop_of_Rain
11 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 10746539
Use a FocusListener that and set the colour in the focuseGained() method.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10746564
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
 
LVL 15

Accepted Solution

by:
Javatm earned 125 total points
ID: 10746572
Hi friend;

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

Hope that helps . . .
Javatm
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10746577
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
 

Author Comment

by:Drop_of_Rain
ID: 10746579
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:Javatm
ID: 10746596
My solution works for any panel's so you can try it and see the results.
0
 

Author Comment

by:Drop_of_Rain
ID: 10746608
Thanks You guys are very active tonight, I will have to take advantage of this!
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10746616
Sure, I'm pretty active today but your also fast and did saw my other solutions
on some of your questions.
0
 

Author Comment

by:Drop_of_Rain
ID: 10746655
Yes I did that is why I made the comment!

Thanks
0
 

Author Comment

by:Drop_of_Rain
ID: 10961239
Thanks for the support
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 83
best (free) software to access postgres db (java) 1 33
ForLoop Example 3 40
varialbe initialization 11 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now