Unable to set JLayeredPane mouse cursor

I ran into a problem that I cannot understand and cannot find an answer anywhere on the Internet.

I have a JLayeredPane, and when I have only one child panel, I can set the cursor correctly using setCursor (). The cursor appears and everything is in order. But when I add an extra JPanel to JLayeredPane, the cursor no longer displays

for example, this works:

m_layeredPane = new JLayeredPane();
m_layeredPane.setLayout(new WBLayoutManager());
m_layeredPane.add(m_mediaPanel, new Integer(0));
// m_layeredPane.add(m_whiteboardPanel, new Integer(1));

m_layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // WORKS

but this is not so:

m_layeredPane = new JLayeredPane();
m_layeredPane.setLayout(new WBLayoutManager());
m_layeredPane.add(m_mediaPanel, new Integer(0));
m_layeredPane.add(m_whiteboardPanel, new Integer(1));

m_layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // FAILS

Does anyone know how I can get custom cursors working in JLayeredPane

+3
source share
4 answers

If you look at the source code javax.swing.JLayeredPane, you will see its constructor defined as follows:

public JLayeredPane() {
    setLayout(null);
}

, . , ( , ), JLayeredPane.

+3

, . setCursor JLayeredPane :

this.getParent().setCursor( Cursor.getDefaultCursor() );

"this" - , . JLayeredPane ( ).

+1

.

3 , , , , .

, .

0

Have you tried using the first working code, but put m_mediaPanel at level 1? This probably won't work either. I think this is due to the fact that the panel that is on top defines the cursor. At level 0, this laminated panel itself can determine this.

0
source

Source: https://habr.com/ru/post/1748423/


All Articles