0

I am having issues while trying to make a highlighting 'label' that changes its icon, okay, so when MouseEntered event is being called for one jLabel, every nearby label's event is also being called and their icon is being changed. I've tried to disable that by using variable to deny changing other jLabel icons but it remains the same like it's being called at the same moment without letting the program storing values in variable and performing if checks, here's the code:

private int OverlayButton = -1;

private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {                                     
    SetButton( 1 );
}                                    

private void jLabel1MouseExited(java.awt.event.MouseEvent evt) {                                    
    ResetButton( 1 );
}                                   

private void jLabel2MouseEntered(java.awt.event.MouseEvent evt) {                                     
    SetButton( 2 );
}                                    

private void jLabel2MouseExited(java.awt.event.MouseEvent evt) {                                    
    ResetButton( 2 );
}                                   

private void jLabel3MouseEntered(java.awt.event.MouseEvent evt) {                                     
    SetButton( 3 );
}                                    

private void jLabel3MouseExited(java.awt.event.MouseEvent evt) {                                    
    ResetButton( 3 );
}    

public void SetButton( int button ) {

    if( OverlayButton == -1 ) {
        OverlayButton = button;
        System.out.println( "SetButton method | (BUTTON-ID:"+ button+ ") ." );
        switch( button ) {
            case 1:         {
                jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/SecondaryCalendar.png")));
            }
            case 2:         {
                jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/SecondaryNotification.png")));
            }
            case 3:         {
                jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/medal (1).png")));
            }
            case 4:         {

            }
        }
    }
    else {}
}

public void ResetButton( int button ) {

    if( OverlayButton != -1 ) {
        switch( button ) {
            case 1:         {
                jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/calendar-with-a-clock-time-tools.png")));
            }
            case 2:         {
                jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/notifications-button.png")));
            }
            case 3:         {
                jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/medal (2).png")));
            }
        }
        System.out.println( "ResetButton method | (BUTTON-ID:"+ button+ ") | Setting OverlayButton to -1." );
        OverlayButton = -1;
    }
}

I've also tried using resetting icons under each event for different jLabels, but unsuccessfuly.

basha
  • 142
  • 2
  • 11
  • 4
    The code, you've posted give us no hint about what's wrong in your code. Please provide a [mcve] so we can better understand what you mean and can debug it to find your error. – Sergiy Medvynskyy Sep 18 '18 at 18:45
  • A tip to make that MCVE suggested by @SergiyMedvynskyy.. One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Sep 18 '18 at 20:58

1 Answers1

5

Add break in your case my friend.. Add break.

               case 1: {
                    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/SecondaryCalendar.png")));
                    break;
                }
                case 2:         {
                    jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/SecondaryNotification.png")));
                    break;
                }
                case 3:         {
                    jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Agendicus/medal (1).png")));
                    break;
                }

Omitting the break statement cause each subsequent case to be executed.

For posterity, I want to point that the the switch statement was unnecessary and therefore the error you encountered was completely avoidable. Had you used, for one example, a custom class that implements MouseListener and takes the icon paths you wish to transition between as arguments your code would have been easier for others to follow when you have questions and need help. Here's an example that eliminates the source of your problem.

public static class LabelListener implements MouseListener {
    private ImageIcon newIcon;
    private ImageIcon defaultIcon;
    private JLabel label;
    public LabelListener(JLabel label, String newIconPath, String defaultIconPath) throws IOException{
            this.label = label;
            this.label.setSize(100, 100);
            this.newIcon = new ImageIcon(newIconPath);
            this.defaultIcon = new ImageIcon(defaultIconPath);
            this.label.setIcon(this.defaultIcon);
    }
    @Override
    public void mouseEntered(MouseEvent evt){
            this.label.setIcon(this.newIcon);
    }
    @Override
    public void mouseExited(MouseEvent evt){
            this.label.setIcon(this.defaultIcon);
    }
    @Override
    public void mousePressed(MouseEvent evt){
    }
    @Override
    public void mouseClicked(MouseEvent evt){
    }
    @Override
    public void mouseReleased(MouseEvent evt){
    }
}

Good that you have written the correct way of implementing the listener. However the problem that he had in his code is of not using break. If you are using switch-case, you need to use break. I think it is just a miss.

Shahid
  • 481
  • 1
  • 8
  • 22
  • 1
    Thanks alot! I never really saw proper usage of break; in switch statements, thanks for pointing it out. Adding break; for each case solved the problem. – basha Sep 19 '18 at 13:43