Solved

Change color on focus

Posted on 2004-04-02
11
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

749 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