2

I have jframe with 2 jpanels. I have some components on first jpanel and button on second. When i click on jbuton (call method removeAll) on first panel it removes all components(i call method revalidate(), repaint()) within the that jpanel then i show second panel with components. Long story short, i remove first jpanel to show second, and second to show first on jframe, ... Switching between the jpanels work fine, button when i switch back to first jpanel cursor hand type doesn't show instead normal cursor appear. Hand cursor type work's just fine for the first time but when i switch back to first jpanel it is back to default ...

Implementation of button:

        button = new JButton(icon);
        button.setText("OK");
        button.setHorizontalTextPosition(SwingConstants.LEFT);
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

When i switch to second jpanel and then back to first jpanel the cursor on button doesn't appear ("hand cursor") of course.

I want that button keep hand type of cursor when i switch between jpanels on frame ...

slodeveloper
  • 242
  • 3
  • 21
  • @dic19 Sounds like an ugly hack. – FINDarkside May 20 '15 at 20:46
  • 4
    I'm not sure I believe that you need a MouseListener. Please write a [minimal example program](http://stackoverflow.com/help/mcve) that we can test and modify that shows your problem for us. Note that if this were my program, I'd simply swap JPanels using a CardLayout. – Hovercraft Full Of Eels May 20 '15 at 20:47
  • I apply mouse listener to button but it doesn't set cursor to hand type ... – slodeveloper May 20 '15 at 20:54
  • Can you give me some good examples how to implement cardlayout for switching between jpanels? I am using MigLayout ... – slodeveloper May 20 '15 at 20:58
  • I guess something strange happens when i switch between jpanel by removing components... I think that my approach to switching between jpanels by removing components is not right... – slodeveloper May 20 '15 at 21:07

1 Answers1

4

For the best example of swapping with CardLayout, check the CardLayout tutorial, but this example shows no problem with the button's cursor after swapping JPanels:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class TestHandCursor extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = 300;

   // the cardlayout:
   private CardLayout cardLayout = new CardLayout();

   // the cardlayout using JPanel
   private JPanel cardHolderPanel = new JPanel(cardLayout);

   // the action that a button uses to swap cards
   private SwapCardAction swapCardAction = new SwapCardAction("Swap View");
   private JButton swapButton = new JButton(swapCardAction);

   public TestHandCursor() {
      // set up button's cursor
      swapButton.setFocusPainted(false);
      swapButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

      // dummy JPanels to add to the CardLayout-using JPanel
      JPanel panel1 = new JPanel();
      panel1.add(new JLabel("Panel 1"));
      panel1.add(new JButton("Button 1"));

      String[] items = { "Stormy Monday", "Tuesday's Just as Bad",
            "Wednesday's Worse", "Thursday's Oh So Sad" };
      JPanel panel2 = new JPanel();
      panel2.add(new JLabel("Panel 2"));
      panel2.add(new JComboBox<String>(items));

      JPanel panel3 = new JPanel();
      panel3.add(new JLabel("Panel 3"));

      // add above JPanels to the cardHolderPanel:
      cardHolderPanel.add(panel1, "panel 1");
      cardHolderPanel.add(panel2, "panel 2");
      cardHolderPanel.add(panel3, "panel 3");

      // panel to hold JButton
      JPanel bottomPanel = new JPanel();
      bottomPanel.add(swapButton);

      // add all to the main JPanel
      setLayout(new BorderLayout());
      add(cardHolderPanel, BorderLayout.CENTER);
      add(bottomPanel, BorderLayout.PAGE_END);
   }

   // let's make our GUI a little bigger
   @Override
   public Dimension getPreferredSize() {
      Dimension superSize = super.getPreferredSize();
      if (isPreferredSizeSet()) {
         return superSize;
      }

      int prefW = Math.max(superSize.width, PREF_W);
      int prefH = Math.max(superSize.height, PREF_H);
      return new Dimension(prefW, prefH);
   }

   // button Action to swap "cards" in our CardLayout
   private class SwapCardAction extends AbstractAction {
      public SwapCardAction(String name) {
         super(name);
         int mnemonic = (int) name.charAt(0);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         cardLayout.next(cardHolderPanel);
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("TestHandCursor");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new TestHandCursor());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373