bradforj287
bradforj287

Reputation: 1048

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.add(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

Upvotes: 4

Views: 556

Answers (4)

Igor Rodriguez
Igor Rodriguez

Reputation: 1246

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

Upvotes: 2

jfpoilpret
jfpoilpret

Reputation: 10519

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.

Upvotes: 3

Marc
Marc

Reputation: 3560

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.

Upvotes: 0

camickr
camickr

Reputation: 324207

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.

Upvotes: 0

Related Questions