Solved

Change color on focus

Posted on 2004-04-02
6
766 Views
Last Modified: 2012-06-27
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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class BlocksAndHandStricks extends JPanel
{ // Opens class

    JPanel jPanel1;
    JPanel jPanel1A;
    JPanel jPanel1B;
    JPanel jPanel1C;
    JPanel jPanel2;
    JPanel jPanel2A;
    JPanel jPanel2B;
    JPanel jPanel2C;
    JPanel jPanel3;
    JPanel jPanel4;

    JLabel jLabel1;
    JLabel jLabel2;
    JLabel jLabel3;
    JLabel jLabel4;
    JLabel jLabel5;
    JLabel jLabel6;
    JLabel jLabel7;
    JLabel jLabel8;
    JLabel jLabel9;
    JLabel jLabel10;
    JLabel jLabel11;
    JLabel jLabel12;
   

    public BlocksAndHandStricks()
    {
        super();

Font f1 = new Font("Courier New", Font.BOLD, 15);

jPanel1 = new javax.swing.JPanel();
jPanel1A = new javax.swing.JPanel();
jPanel1B = new javax.swing.JPanel();


jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jPanel2A = new javax.swing.JPanel();
jPanel2B = new javax.swing.JPanel();
jPanel2C = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jPanel3 = new javax.swing.JPanel();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jPanel4 = new javax.swing.JPanel();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jLabel12 = new javax.swing.JLabel();

setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));
jPanel1.add(Box.createGlue());

add(jPanel1);
jPanel1.add(jPanel1A);
jPanel1.add(jPanel1B);
jPanel1A.setBorder(BorderFactory.createLoweredBevelBorder());
jPanel1B.setBorder(BorderFactory.createRaisedBevelBorder());


jPanel1A.add(new MyClass4());
jPanel1A.setBackground(Color.white);
jPanel1B.add(new MyClass4());
jPanel1B.setBackground(Color.yellow);


add(jPanel2);
jPanel2.add(Box.createGlue());
jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));
jPanel2.setBorder(BorderFactory.createLoweredBevelBorder());

jPanel2.add(jLabel4);


jPanel2.add(jLabel5);

jLabel6.setText("jLabel6");
jPanel2.add(jLabel6);



jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.Y_AXIS));


jLabel8.setText("jLabel8");
jPanel3.add(jLabel8);

jLabel9.setText("jLabel9");
jPanel3.add(jLabel9);

add(jPanel3);

jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.Y_AXIS));

jLabel10.setText("jLabel10");
jPanel4.add(jLabel10);

jLabel11.setText("jLabel11");
jPanel4.add(jLabel11);

jLabel12.setText("jLabel12");
jPanel4.add(jLabel12);

add(jPanel4);

  }
}

This is called from above class


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
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10746555
0
 
LVL 15

Accepted Solution

by:
Javatm earned 250 total points
ID: 10746580
This should solve your problem in you use the mouse :

  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: 10746601
Which class would I place this code in?  Classes above
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 15

Expert Comment

by:Javatm
ID: 10746610
Any class that you wanna use it :)
0
 

Author Comment

by:Drop_of_Rain
ID: 10746639
How do I place it in Myclass4 it is a JPanel and one isn't declared.

   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);
  }
  });
0
 

Author Comment

by:Drop_of_Rain
ID: 10746642
It isn't changing color when a checkbox is checked. How can I change the color of the checkbox?
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

726 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