Skip to content
Advertisement

GlassPane is not showing up on JFrame

I have been trying to implement a GlassPane for my games’ in-game HUD, but right now I cant seem to get the JFrame to set my GlassPane as its own i;ve used setGlassPane() and Ive been reading up a few examples trying to find my mistake, but nothing. So I wrote a SSCCE that demonstrates my problem. I have a JFrame to which I add a Jpanel with a label “TESTIING” then I initiate my galss pane and call setGlassPane() on my frames instance. My GlassPane has a MouseListener, a JPanel and 2 JLabels, and an overriden paint() however the MouseListener wont work the paint() wont show and my labels are not there (so basically my GlassPane is not being set as my frames new GlassPane)-

/*Main.java*/

import java.awt.EventQueue;

public class Main {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TestGlassPane testGlassPane=new TestGlassPane();
                testGlassPane.setVisible(true);
            }
        });
    }
}

/*TestGlassPane.java*/

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class TestGlassPane extends JFrame{
    private GlassGamePane m_glassPane;
    private JPanel drawingPanel;
    private JLabel testLabel;

    public TestGlassPane() {
        createUI();
    }
    private void createUI() {
        setTitle("Test GlassGamePane");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(800, 700);
        setResizable(false);
        setLocationRelativeTo(null);

        createComponents();
        //add components to frames content pane
        addComponentsToContentPane(getContentPane());


        //setting glassPane 
        m_glassPane = new GlassGamePane();
        //set opaque to false, i.e. make transparent 
        m_glassPane.setOpaque(false);
        m_glassPane.setVisible(true);

        getRootPane().setGlassPane(m_glassPane);
    }

    private void addComponentsToContentPane(Container contentPane) {
        drawingPanel.add(testLabel);
        contentPane.add(drawingPanel, BorderLayout.CENTER);
    }

    private void createComponents() {
        drawingPanel=new JPanel(new BorderLayout());
        testLabel=new JLabel("TESTIING");
    }
}

/*GlassGamePane.java*/

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class GlassGamePane extends JPanel implements MouseListener {

    private JPanel statusPanel;
    private JLabel healthLabel;

    public GlassGamePane() {
        createGlassPane();
    }

    private void createGlassPane() {
        setLayout(new BorderLayout());
        createComponents();
        statusPanel.add(healthLabel);
        add(statusPanel, BorderLayout.NORTH);
        addMouseListener(this);
    }

    private void createComponents() {
        statusPanel = new JPanel(new GridLayout(2, 6));
        healthLabel = new JLabel("Player Health:");
        healthLabel.setForeground(Color.RED);
        healthLabel.setBackground(Color.BLUE);

    }

    @Override
    public void paintComponent(Graphics g) {
        g.setColor(Color.red);
        //Draw an oval in the panel  
        g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        Toolkit.getDefaultToolkit().beep();
    }

    @Override
    public void mousePressed(MouseEvent me) {
        Toolkit.getDefaultToolkit().beep();
    }

    @Override
    public void mouseReleased(MouseEvent me) {
    }

    @Override
    public void mouseEntered(MouseEvent me) {
    }

    @Override
    public void mouseExited(MouseEvent me) {
    }
}

Thanks you.

Advertisement

Answer

It seems that you have to first add the custom GlassPane as your frames GlassPane before making the GlassPane visible! This code here seemed to work:

    //setting glassPane 
    m_glassPane = new GlassGamePane();
    setGlassPane(m_glassPane);
    //set opaque to false, i.e. make transparent 
    m_glassPane.setOpaque(false);
    m_glassPane.setVisible(true);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement