-1

I am working on a program. In one of my panels there is a JLabel, which you can click and a dialog will open. So far so good, but if I compile my program to a jar and run it, the label is unclickable and all of my events are ignored, no exceptions, nothing.

In my IDE, the label and its Events are fine and all components work properly. My problem is: I don't know how to fix this.

I don't know if should post some code, but i think its not the code which causes the bug. Some ideas?

Here is a abstract form of my code:

public class TaskListPanel extends javax.swing.JPanel {

private javax.swing.JLabel lblAddTask;


/**
 * Creates new form TaskListPanel
 */
public TaskListPanel() {
    initComponents();

}
private void initComponents() {
lblAddTask.setIcon(new javax.swing.ImageIcon(getClass().getResource("/path/to/my/icon.png"))); // NOI18N
        lblAddTask.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblAddTaskMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lblAddTaskMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lblAddTaskMouseExited(evt);
            }
        });
}

//some more methods

private void lblAddTaskMouseEntered(java.awt.event.MouseEvent evt) {                                           
    lblAddTask.setIcon(new ImageIcon(getClass().getResource("/path/to/my/iconhighlighted.png/")));
}                                          

private void lblAddTaskMouseExited(java.awt.event.MouseEvent evt) {                                          
    lblAddTask.setIcon(new ImageIcon(getClass().getResource("/path/to/my/icon.png/")));
}                                         

private void lblAddTaskMouseClicked(java.awt.event.MouseEvent evt) { 
    //mydifferentdialog is a custom dialog, it works fine.
    mydifferentdialog dialog = new mydifferentdialog();
}                                          

}

None of my lblAddTask Events are activiated. Is it maybe a problem in my IDE (Netbeans) which causes the bug because it compiles my programm wrong to a jar?

Rubinum
  • 547
  • 3
  • 18
  • 4
    *"I dont know if should post some code, but i think its not the code which causes the bug"* - Without a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem, we are left with nothing but guess work and blind hope – MadProgrammer Jun 27 '14 at 06:52
  • give me a second, i am collecting some code to add it to my question. – Rubinum Jun 27 '14 at 06:54
  • 1
    Rather use a `JButton` (undecorated, if need be) with an `ActionListener` than a `JLabel` with `MouseListener`. Note also that 'some code' is usually not equal to a 'runnable example' as suggested by @MadProgrammer. Be sure to read the link for details. – Andrew Thompson Jun 27 '14 at 07:02
  • the weirdest thing is, that there is a similar label in the near of the problem label, which works fine, but there are no diffence neither in init the label nor in its events. My problem is now that i cannot write a example programm which got the same bug in it – Rubinum Jun 27 '14 at 07:08
  • there is to simple to fix those issues, have to 1) avoiding load any FileIO inside MouseEvents ---> prepare those Object (ImageIcon) as local variables, 2) call ImageIcon.flush(), 3) from MouseMotions Events isn't reapint() in API, have to call reapint() programatically – mKorbel Jun 27 '14 at 07:32
  • @mKorbel do you really think they are not excecuted because of my ImageIcons? but why is my `.lblAddTaskMouseClicked()` event not excecuted. it has no ImageIcon object within. – Rubinum Jun 27 '14 at 07:37
  • hmm i forgot to say that my declared panel in my post is nested in an other panel which is also nested in an other panel. is it neccessary? – Rubinum Jun 27 '14 at 07:39
  • @AndrewThompson with a jButton it all works fine, but i dont want to use a jButton because I want to have a specific design and I know it also works with a jLabel. Is the real point, that the jLabel uses the MouseEvent and the jButton the ActionEvent? But why is the MouseEvent not activated at this specific label and why is this not happening on other jLabels in my programm? – Rubinum Jun 27 '14 at 07:50
  • 1
    again re_read 1st. comment in this thread about SSCCE/MCVE, use UIManager.getIcon("OptionPane.errorIcon/informationIcon/warningIcon/questionIcon") for MouseEvents – mKorbel Jun 27 '14 at 07:50
  • 1
    *"..I know it also works with a jLabel."* 1) it is `JLabel` with a capital `J` (stop talking like your damned IDE). 2) Your question would suggest otherwise. 3) A label with a `MouseListener` won't (ever) react to keyboard input. 4) `JButton` can have different icons for default and 'hover' state (without any listeners whatsoever) -- So stop wasting everyone's time and ***post an MCVE.*** ..but 5) I strongly suspect the problem is that the images are not being found. and 6) A way to get image(s) for examples is to hot-link images in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Jun 27 '14 at 08:00
  • BTW - an MCVE includes imports and a main method to put it on-screen. – Andrew Thompson Jun 27 '14 at 08:01
  • @AndrewThompson **hey dont be so rude to me.** pls follow StackOverflows rules. >:( As i said, i can write you an MCVE but it dont will have my bug in it because i dont know how i produced it! Even if i replace my `ImageIcon` Statements with `System.out.println()` Statements, they will not be excecuted! – Rubinum Jun 27 '14 at 08:14

1 Answers1

-1

Okay the bug was really caused by the ImageIcon Statement:

lblAddTask.setIcon(new ImageIcon(getClass().getResource("/path/to/my/iconhighlighted.png/")));

Thanks to mKorble for his suggestion.

Rubinum
  • 547
  • 3
  • 18