Unable to set mouse cursor JLayeredPane
I'm running into a problem that I can't seem to figure out nor find the answer anywhere on the web.
I've got a JLayeredPane and when it only has one child Panel I am able to correctly set the cursor using setCursor(). The cursor shows up and everything is fine. But when I add an additional JPanel into the JLayeredPane the cursor no longer shows up
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 doesn't:
m_layeredPane = new JLayeredPane();
m_layeredPane.setLayout(new WBLayoutManager());
m_layeredPane.a开发者_JAVA技巧dd(m_mediaPanel, new Integer(0));
m_layeredPane.add(m_whiteboardPanel, new Integer(1));
m_layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // FAILS
Anyone know how i can get custom cursors working within a JLayeredPane
If you take a look at javax.swing.JLayeredPane
source code, you will see its constructor defined like that:
public JLayeredPane() {
setLayout(null);
}
which clearly indicates that it needs to handle components layout by itself.
Hence you can guess (although it is not documented, I would consider it a documentation bug) that you should not change the layout of JLayeredPane
.
While the topic is old, none of the answers was satisfying. I resolved the problem calling to the setCursor method of the JLayeredPane in this way:
this.getParent().setCursor( Cursor.getDefaultCursor() );
Where "this" is the component I want to change the cursor to. Its parent is the JLayeredPane (since it is added to it).
Works fine for me when using the demo code the the How to Use Layered Panes tutorial.
Based on the 3 lines of code the only difference I can see from the tutorial is that you are using a layout manager.
Compare your code with the tutorial to find other differences.
Have you tried taking the first working code, but placing the m_mediaPanel on level 1? This probably won't work either. I think this is due to the fact that the panel that is on top determines the cursor. On level 0 the layered pane itself can determine this.
精彩评论