Search code examples
javaiconsjlabel

How to change the icon of jLabel in code in java?


im trying to change the icon of a jLabel named "mainDot". i have another folder in the source packages (named "Images") that contains the icons i want to put on the mainDot when I press it.

private void mainDotMousePressed(java.awt.event.MouseEvent evt) {                                     
    mainDot.setIcon(DOT_SMALL.jpg);
}

How would I be able to change the icon of mainDot?


Solution

  • public Test() {
        JLabel t=new JLabel();
        this.setLayout(new BorderLayout());
        t.setIcon(new ImageIcon("Path to image goes here"));
        this.add(t);
        this.pack();
        this.setVisible(true);  
    }
    

    this one is working. and as suggested use an actionlistener to do it on button press