• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2523
  • Last Modified:

Make button change color when mouse moves over it

I am trying to make  the color of my exit button ( jButtonExit  )change when I move a mouse over it ( aka I'm playing with events ).  I have the following event handler that makes my program not compile.  What am I doing wrong?
private void jButtonExitMouseEntered(java.awt.event.MouseEvent evt) {
    jButtonExit.setBackground(Color.BLUE);
}

Open in new window

0
dwcronin
Asked:
dwcronin
  • 2
  • 2
1 Solution
 
Bart CremersJava ArchitectCommented:
The code snippet you provide should compile. You'll need to provide more code to show the problem.
0
 
dwcroninAuthor Commented:
Bart Cr,
I am trying to learn NetBeans while I learn Java.  This code comes from following the NetBeans help files.  I'm telling you this to let you know I don't understand which files are important.  I zipped my entire directory for you but I think EE won't let me include my .java file in the archive ( I think it;s the most important one ).  Here it is below:  ( tell me if you need more )
/*
 * NumberAdditionUI.java
 *
 * Created on November 8, 2008, 8:06 AM
 */
package my.numberaddition;
 
/**
 *
 * @author  dwcronin
 */
public class NumberAdditionUI extends javax.swing.JFrame
{
 
    /** Creates new form NumberAdditionUI */
    public NumberAdditionUI()
    {
        initComponents();
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
 
        jPanel1 = new javax.swing.JPanel();
        jLabelFirstNumber = new javax.swing.JLabel();
        jLabelResult = new javax.swing.JLabel();
        jLabelSecondNumber = new javax.swing.JLabel();
        jTextFieldFirstNumber = new javax.swing.JTextField();
        jTextFieldSecondNumber = new javax.swing.JTextField();
        jTextFieldResult = new javax.swing.JTextField();
        jButtonAdd = new javax.swing.JButton();
        jButtonClear = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButtonExit = new javax.swing.JButton();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Number Addition"));
 
        jLabelFirstNumber.setText("First Number");
 
        jLabelResult.setText("Result");
 
        jLabelSecondNumber.setText("Second Number");
 
        jTextFieldFirstNumber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldFirstNumberActionPerformed(evt);
            }
        });
 
        jButtonAdd.setText("Add");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });
 
        jButtonClear.setText("Clear");
        jButtonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonClearActionPerformed(evt);
            }
        });
 
        jLabel1.setText("jLabel1");
 
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelFirstNumber, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabelSecondNumber, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabelResult, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jButtonAdd)
                                .addGap(41, 41, 41)
                                .addComponent(jButtonClear))
                            .addComponent(jTextFieldFirstNumber, javax.swing.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                            .addComponent(jTextFieldSecondNumber)
                            .addComponent(jTextFieldResult)))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
 
        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButtonAdd, jButtonClear});
 
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelFirstNumber)
                    .addComponent(jTextFieldFirstNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelSecondNumber)
                    .addComponent(jTextFieldSecondNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelResult)
                            .addComponent(jTextFieldResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(46, 46, 46)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonAdd)
                            .addComponent(jButtonClear)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(jLabel1)))
                .addContainerGap(30, Short.MAX_VALUE))
        );
 
        jButtonExit.setText("Exit");
        jButtonExit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jButtonExitMouseEntered(evt);
            }
        });
        jButtonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExitActionPerformed(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonExit, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonExit)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
 
        pack();
    }// </editor-fold>                        
 
private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {                                             
    jTextFieldFirstNumber.setText("");
    jTextFieldSecondNumber.setText("");
    jTextFieldResult.setText("");
}                                            
 
private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt) {                                            
    System.exit(0);
}                                           
 
private void jTextFieldFirstNumberActionPerformed(java.awt.event.ActionEvent evt) {                                                      
// TODO add your handling code here:
}                                                     
 
private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {                                           
 
 
 
    // First we define float variables.
    float num1, num2, result;
    // We have to parse the text to a type float.
    num1 = Float.parseFloat(jTextFieldFirstNumber.getText());
    num2 = Float.parseFloat(jTextFieldSecondNumber.getText());
    // Now we can perform the addition.
    result = num1 + num2;
    // We will now pass the value of result to jTextField3.
    // At the same time, we are going to
    // change the value of result from a float to a string.
    jTextFieldResult.setText(String.valueOf(result));
}                                          
 
private void jButtonExitMouseEntered(java.awt.event.MouseEvent evt) {                                         
    jButtonExit.setBackground(Color.BLUE);                                        
}
 
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NumberAdditionUI().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonClear;
    private javax.swing.JButton jButtonExit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelFirstNumber;
    private javax.swing.JLabel jLabelResult;
    private javax.swing.JLabel jLabelSecondNumber;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldFirstNumber;
    private javax.swing.JTextField jTextFieldResult;
    private javax.swing.JTextField jTextFieldSecondNumber;
    // End of variables declaration                   
 
}

Open in new window

0
 
Bart CremersJava ArchitectCommented:
Look like you're missing an import for java.awt.Color. You should add a line before the class declaration:


    import java.awt.Color;
   
    ...

    public class NumberAdditionUI extends ...


or you could use a fully-qualified name in the code like you've got in many other places:

    jButtonExit.setBackground(java.awt.Color.BLUE);

0
 
dwcroninAuthor Commented:
I think what Bart Cr said is right.  The import MAY have already been there though and I was just getting impatient.  When I got out of netbeans and restarted it - it worked properly and I don't think I added the line (it was already in my code).  It just needed (???) restarting.  Sorry - I don't know what just happened.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now