在视口组件上使用JScrollPane鼠标侦听器

时间:2012-07-13 20:49:23

标签: java swing

我有JScrollPane可以将 viewportView 设置为一系列不同的面板。我想在单击其视口中的任何其他组件时获取JScrollPane组件。如果我向JScrollPane添加MouseListener,当我直接点击窗格的边框时,它会收到我的鼠标事件,但是当我点击组件时却没有。

添加侦听器并最终找到封闭的scrollPane的正确方法是什么?我不一定提前知道我在视口中显示的面板上的所有组件 - 只是它们将在JPanel的某个子类上。

import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.MouseInputAdapter;

import net.miginfocom.swing.MigLayout;

public class TestScrollPane extends MouseInputAdapter{
    public void mouseEntered(MouseEvent arg0) {System.out.println("Entered " + arg0.getComponent());}
    public void mouseExited(MouseEvent arg0) {System.out.println("Exited " + arg0.getComponent());}
    public void mousePressed(MouseEvent arg0) {System.out.println("Pressed " + arg0.getComponent());}
    public void mouseReleased(MouseEvent arg0) {System.out.println("Released " + arg0.getComponent());}

    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setLayout(new MigLayout());
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TestPane pane1 = new TestPane("Scroll Pane 1");
        TestPane pane2 = new TestPane("Scroll Pane 2");
        frame.add(pane1, "push,grow");
        frame.add(pane2, "push, grow");
        TestMouseListener listener = new TestMouseListener();
        pane1.addMouseListener(listener);
        pane1.addMouseMotionListener(listener);
        pane2.addMouseListener(listener);
        pane2.addMouseMotionListener(listener);
        frame.setVisible(true);
    }
}

class TestPanel2 extends JPanel {
    String name;
    TestPanel2(String name){ 
        this.name = name;
        setLayout(new MigLayout());
        JTextArea area = new JTextArea();
        area.append(name);
        add(area, "push, grow");
    }
    public String toString(){ return name; }
}
class TestPane extends JScrollPane {
    String name;
    TestPane(String name){ 
        this.name = name; 
        TestPanel2 panel = new TestPanel2(name + " panel");
        setViewportView(panel);
    }
    public String toString(){ return name; }
}

在这个例子中,我得到鼠标进入和退出事件,但我只能通过单击文本区域周围的边框来获取鼠标单击事件。即使我更改了TestPane类以将侦听器添加到其viewportView面板,我也无法分辨textArea中发生了什么。

class TestPane extends JScrollPane {
    String name;
    TestPane(String name){ 
        this.name = name; 
        TestPanel2 panel = new TestPanel2(name + " panel");
        TestMouseListener listener = new TestMouseListener();
        panel.addMouseListener(listener);
        panel.addMouseMotionListener(listener);
        setViewportView(panel);
    }
    public String toString(){ return name; }
}

但是,我无法知道JPanel上的内容,因此我无法更深入地手动添加侦听器。

3 个答案:

答案 0 :(得分:3)

另一种可能的方法是使用AWTEventListener,然后冒泡父树以查看是否已按下您感兴趣的组件或保持已按下的子组件。例如:

import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.MouseInputAdapter;

// import net.miginfocom.swing.MigLayout;

public class TestScrollPane extends MouseInputAdapter {
   public void mouseEntered(MouseEvent arg0) {
      System.out.println("Entered " + arg0.getComponent());
   }

   public void mouseExited(MouseEvent arg0) {
      System.out.println("Exited " + arg0.getComponent());
   }

   public void mousePressed(MouseEvent arg0) {
      System.out.println("Pressed " + arg0.getComponent());
   }

   public void mouseReleased(MouseEvent arg0) {
      System.out.println("Released " + arg0.getComponent());
   }

   public static void main(String[] args) {
      JFrame frame = new JFrame();
      // frame.setLayout(new MigLayout());
      frame.getContentPane().setLayout(new FlowLayout());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      TestPane pane1 = new TestPane("Scroll Pane 1");
      TestPane pane2 = new TestPane("Scroll Pane 2");
      frame.add(pane1, "push,grow");
      frame.add(pane2, "push, grow");
      // !! TestMouseListener listener = new TestMouseListener();
      TestScrollPane listener = new TestScrollPane();
      pane1.addMouseListener(listener);
      pane1.addMouseMotionListener(listener);
      pane2.addMouseListener(listener);
      pane2.addMouseMotionListener(listener);
      frame.pack();
      frame.setVisible(true);

      Toolkit.getDefaultToolkit().addAWTEventListener(
            listener.createAWTWindowListener(), AWTEvent.MOUSE_EVENT_MASK);

   }

   private AWTEventListener createAWTWindowListener() {
      AWTEventListener awt1 = new AWTEventListener() {

         @Override
         public void eventDispatched(AWTEvent e) {
            if (MouseEvent.MOUSE_PRESSED == e.getID()) {
               MouseEvent event = (MouseEvent) e;
               Component comp = event.getComponent();

               if (comp != null) {
                  String scrollPanelName = recursivelyCheckForScrollPanel(comp);
                  if (scrollPanelName != null) {
                     System.out.println("TestPane pressed. Name: " + scrollPanelName);
                  } else {
                     System.out.println("TestPane not pressed");
                  }
               }
            }
         }

         private String recursivelyCheckForScrollPanel(Component comp) {
            if (comp instanceof TestPane) {
               return comp.toString();
            } else {
               comp = comp.getParent();
               if (comp != null) {
                  return recursivelyCheckForScrollPanel(comp);
               }
            }
            return null;
         }
      };
      return awt1;
   }
}

class TestPanel2 extends JPanel {
   String name;

   TestPanel2(String name) {
      this.name = name;
      // setLayout(new MigLayout());
      JTextArea area = new JTextArea(5, 20);
      area.append(name);
      add(area, "push, grow");
   }

   public String toString() {
      return name;
   }
}

class TestPane extends JScrollPane {
   String name;

   TestPane(String name) {
      this.name = name;
      TestPanel2 panel = new TestPanel2(name + " panel");
      setViewportView(panel);
   }

   public String toString() {
      return name;
   }
}

注意:有关详细信息,请参阅this question和StanislovL以及mkorbel的答案。

答案 1 :(得分:1)

您应该将鼠标侦听器添加到JScrollPane的视图而不是滚动窗格本身,因为滚动窗格仅包含角和滚动条。

yourJScrollPane.getViewport().getView().addMouseListener(yourMouseListener);

此代码段将鼠标侦听器添加到JScrollPane的一个视口组件中。

答案 2 :(得分:0)

你要遇到的问题是,如果面板包含内容也有鼠标监听器,你将永远不会收到这些事件的通知(鼠标事件往往被链中较高的人使用)

你可以使用addNotify,类似于JTable的工作方式。然后,您将遍历父链,直到您可以使用所需的组件。

这当然假设您没有使用鼠标侦听器...