JPanel + obsługa myszki

0

Witam,

Chciałbym aby po najechaniu myszką na panel uzyskać efekt podświetlenia. Problem polega na tym, że jeżeli w panelu umieszczę inne komponenty po najechaniu na nie myszką wywołują zdarzenie mouseExited skutkiem czego podświetlenie jest wyłączane.

Poniżej zamieszczam przykładowy kod (na razie jedynie zmiana koloru po najechaniu myszką):

package forum;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;


public class PanelTest extends JFrame implements Runnable {

    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 4378899322227713553L;

    /**
     * Panel.
     */
    private JPanel panel;

    /**
     * Pole tekstowe.
     */
    private JTextArea textArea;

    /**
     * Obsługa myszki.
     */
    private MouseListener mouseListener;

    /**
     * Konstruktor.
     */
    public PanelTest() {
        super("Test");
        this.setSize(400, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * Inicjuje kompoennty.
     */
    protected void initComponents() {
        this.panel = new JPanel();
        this.panel.setBackground(Color.LIGHT_GRAY);

        this.textArea = new JTextArea();
        this.textArea.setText("Testowy komponent tekstowy");
        this.textArea.setHighlighter(null);
        this.textArea.setEditable(false);
        this.textArea.setOpaque(false);
    }

    /**
     * Tworzy layout.
     */
    protected void createLayout() {
        this.panel.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.panel.add(this.textArea);

        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(this.panel, BorderLayout.CENTER);
    }

    /**
     * Instaluje obsługę akcji.
     */
    protected void installListeners() {
        this.mouseListener = new MouseAdapter() {

            /**
             * {@inheritDoc}
             */
            @Override
            public final void mouseEntered(final MouseEvent e) {
                PanelTest.this.panel.setBackground(
                        new Color(0x80, 0x00, 0x00, 100));
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public final void mouseExited(final MouseEvent e) {
                PanelTest.this.panel.setBackground(Color.LIGHT_GRAY);
            }
        };
        // podpinam
        this.panel.addMouseListener(this.mouseListener);
    }

    /**
     * Odinstalowuej obsługę akcji.
     */
    protected void uninstallListeners() {
        if (this.panel != null && this.mouseListener != null) {
            this.panel.removeMouseListener(this.mouseListener);
        }
        this.mouseListener = null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void run() {
        // inicjuje komponenty
        this.initComponents();
        // instaluje obsługę
        this.installListeners();
        // tworze layout
        this.createLayout();
        // wyśrodkowuje okno
        this.setLocationRelativeTo(null);
        // pokazuje okno
        this.setVisible(true);
    }

    /**
     * Main.
     *
     * @param args argumenty
     */
    public final static void main(final String[] args) {
        SwingUtilities.invokeLater(new PanelTest());
    }
}

Gdy najedzie się myszką na komponent tekstowy kolor tła zmienia się na standardowy.

0

Spróbuj coś takiego:

public final void mouseExited(final MouseEvent e) {
   Point point = getContentPane().getMousePosition();
   if(point != null && getContentPane().getComponentAt(point) == panel) return;

   PanelTest.this.panel.setBackground(Color.LIGHT_GRAY);
}

Choć na 100% to nie jestem pewien tego.

1 użytkowników online, w tym zalogowanych: 0, gości: 1