Solved

Change color on focus

Posted on 2004-04-02
6
736 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

747 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