In my function called create() i am trying to instanciate an object of class note, which is an extention of JLabel. I then want to add this object to a JLayeredPane (known as lp) at depth 1, in order to show it above my background image. Instanciation goes fine but the newNote object is not displayed.
Here is the code for the note class:
import javax.swing.ImageIcon;
import javax.swing.JLabel;
//This class will be used to instanciate new notes for the user
public class note extends JLabel{
final static ImageIcon image = new ImageIcon(note.class.getResource("semibreve1-removebg-preview.png"));
//attributes
private String length;
private String note1;
private String code;
private String val;
//Constructor
note(String myLength, String myNote){
super("",image,LEADING);
this.length = myLength;
this.note1 = myNote;
generateCode();
//System.out.println("note creation confirmed: "+code);
}
//Getters and setters below
And here is the code for my create function.
public void create(l,n){
note newNote = new note(l,n);
noteObjects.add(newNote);
newNote.setBounds(0,0,newNote.image.getIconWidth(),newNote.image.getIconHeight());
newNote.setSize(newNote.getPreferredSize());
lp.add(newNote,1);
}
Thanks for your time.
As @Hovercraft comments, the problem is likely somewhere else—an image that silently fails to load or an errant component boundary. A minimal example will allow you to study the problem is isolation and use the result to refine your full program.
Starting from this example, I added these lines to the
LayerPanelconstructor to get the image belowThis leverages the panel's layout to position the label in the top right; in contrast, the implementation of
paintComponent()draws in a fixed position at the bottom left.Going forward, critically examine the need to extend
JLabel. As shown here and here, theJLabelitself can control the relative position of text and icon; and the parentJComponentcan store arbitrary, component-specific properties.