-1

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.

  • 2
    If you don't get a decent answer soon, consider creating and posting a valid [mre] with your question. This is not your entire program but a smaller new program that compiles and runs for us, that demonstrates your problem directly for us, and yet is small enough to post in its entirety with your question. – Hovercraft Full Of Eels Aug 04 '21 at 17:22
  • 1
    Did you verify that `image` is not null? – VGR Aug 04 '21 at 18:48
  • 1
    Does it display on a regular JPanel? Get basic logic working first when using layout manager. Then get it working of a JLayeredPane which is more involved since you need to manually manage the size/location. – camickr Aug 04 '21 at 21:16
  • everything is fine, jyust needed to set the opacity to true using `this.setOpaque(true);` – My Cup Of Tea Aug 05 '21 at 19:00

1 Answers1

2

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 LayerPanel constructor to get the image below

this.setLayout(new FlowLayout(FlowLayout.RIGHT));
this.add(new JLabel(String.valueOf(n)), JLabel.RIGHT_ALIGNMENT);

This 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.

image

Going forward, critically examine the need to extend JLabel. As shown here and here, the JLabel itself can control the relative position of text and icon; and the parent JComponent can store arbitrary, component-specific properties.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045