Search code examples
javaswingjbuttonjlabel

JLabel is disappearing


I have been trying to look for the answer everywhere and I couldn't find a right solution so I am hoping that you guys will be able to help me. I have created a custom JButton just by overriding

paintComponent(Graphics g)

using drawImage.

And then I created JLabel,which I wanted to use to write on top of the JButton, because drawImage seemed to be writing over the Label, so I went for JLabel. So the problem that I am getting is that the JLabel actually doesn't come up on the JButton. I am using Eclipse and when I press Run JLabel just pops on the button and immediately disappears.

Below is the code:

private boolean initialize() {
    Color c = new Color(78,110,248);
    Color c1 = new Color(178,110,248);
    frame = new JFrame();
    frame.setBounds(100, 100, 800,600);
    frame.setBackground(c1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    /*Label that I have trouble with. The text should stay on the JButton*/
    lblNewLabel_1 = new JLabel("New label");
    lblNewLabel_1.setEnabled(false);
    lblNewLabel_1.setOpaque(true);
    lblNewLabel_1.setBounds(579, 252, 46, 14);
    frame.getContentPane().add(lblNewLabel_1);


    /*Custom buttons. Class "Button" just overrides paintComponent*/
    btnNewButton = new Button(/*PATH TO JPG*/);
    btnNewButton.setBackground(Color.WHITE);
    btnNewButton.setBounds(427, 213, 357, 90);
    frame.getContentPane().add(btnNewButton);

I have tried to do lblNewLabel_1.setOpaque(true), however this just made the JLabel stay slightly longer, so now I can actually see it, however it still disappears. Does anyone know how to possibly fix it?

EDIT: I updated the code and left only the relevant pieces. However, here is the whole code:

public class Moje {

private JFrame frame;
private Button btnNewButton;
private Label label_1;
private JLabel lblNewLabel;
private JLabel lblNewLabel_1;

/**
 * Launch the application.
 */
public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Moje window = new Moje();
                window.frame.setVisible(true);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public Moje() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private boolean initialize() {
    Color c = new Color(78,110,248);
    Color c1 = new Color(178,110,248);
    frame = new JFrame();
    frame.setBounds(100, 100, 800,600);
    frame.setBackground(c1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    // Label with customized background. It just overrides paintComponent
    myLabel lblNewLabel = new myLabel("PATH TO JPG");
    lblNewLabel.setBounds(427, 0, 357, 169);
    frame.getContentPane().add(lblNewLabel);

    // RELEVANT LABEL. SOURCE OF THE PROBLEM.
    lblNewLabel_1 = new JLabel("New label");
    lblNewLabel_1.setEnabled(false);
    lblNewLabel_1.setOpaque(true);
    lblNewLabel_1.setBounds(579, 252, 46, 14);
    frame.getContentPane().add(lblNewLabel_1);



    // JButton with customized background. It just overrides paintComponent.
    // THIS IS THE BUTTON, WHERE I NEED MY LABEL TO BE VISIBLE
    btnNewButton = new Button("PATH TO JPG");
    btnNewButton.setBackground(Color.WHITE);
    btnNewButton.setBounds(427, 213, 357, 90);
    frame.getContentPane().add(btnNewButton);

    // JTextPane with customized background. It just overrides paintComponent
    myPanel textPane = new myPanel("PATH TO JPG");
    textPane.setEnabled(false);
    textPane.setEditable(false);
    textPane.setBounds(0, 0, 427, 561);
    frame.getContentPane().add(textPane);

    // Jbutton with customized background. It just overrides paintComponent
    Button button = new Button("C:/Users/Filip/Documents/Programowanie/Java/moj/src/moj/Blue.jpg");
    button.setBackground(Color.WHITE);
    button.setBounds(427, 336, 357, 90);
    frame.getContentPane().add(button);

    // JBUtton with customized background. It just overrides paintComponent
    Button button_1 = new Button("C:/Users/Filip/Documents/Programowanie/Java/moj/src/moj/Blue.jpg");
    button_1.setBackground(Color.WHITE);
    button_1.setBounds(427, 448, 357, 90);
    frame.getContentPane().add(button_1);







}

}


Solution

  • I suggest you change your Button btnNewButton to JButton, and just add:

    btnNewButton.add(lblNewLabel_1); // or lblNewLabel, 
    

    and you would be able to modify it.