Повторная отправка MouseEvent через JScrollPane с ненормальным поведением
Я немного изменил код здесь: /questions/7010303/mouseevent-poteryan-v-jscrollpane/7010324#7010324
Так как есть известная ошибка с JScrollPane, не передающей MouseEvent, я сделал обходной путь с FakeMouseListener.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class LostMouseEvent {
private JPanel panel1;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LostMouseEvent();
}
});
}
public LostMouseEvent() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
panel1 = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
};
JPanel panel2 = new JPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 300);
}
};
JScrollPane pane = new JScrollPane(panel2);
panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
panel2.setBorder(BorderFactory.createLineBorder(Color.green));
panel1.setLayout(new CircleLayout());
panel1.add(pane);
frame.add(panel1);
MouseListener rml = new RealMouseListener();
panel1.addMouseListener(rml);
MouseListener fml = new FakeMouseListener();
pane.addMouseListener(fml);
frame.pack();
frame.setVisible(true);
}
});
}
private class RealMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent me) {
System.out.println(me);
Point point = me.getPoint();
System.out.println(panel1.getComponentAt(point));
System.out.println(panel1.getComponent(0));
}
}
private class FakeMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent me) {
panel1.dispatchEvent(me);
}
}
}
Теперь, если щелкнуть внутри зеленой границы с левой стороны рядом с границей, я получу:
java.awt.event.MouseEvent[MOUSE_PRESSED,(9,169),absolute(66,248),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on javax.swing.JScrollPane[,49
LostMouseEvent$2$1[,0,0,600x400,layout=CircleLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@633d51
javax.swing.JScrollPane[,49,49,503x303,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,
Если я нажимаю в середине, я получаю:
java.awt.event.MouseEvent[MOUSE_PRESSED,(247,147),absolute(304,226),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on javax.swing.JScrollPane[,
javax.swing.JScrollPane[,49,49,503x303,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.
javax.swing.JScrollPane[,49,49,503x303,layout=javax.swing.ScrollPaneLayout$UIResource,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.
Это почему? Вам нужно изменить координаты MouseEvent при повторной отправке?
Код для CircleLayout есть
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
public class CircleLayout implements LayoutManager {
private int heightGap;
public CircleLayout () {
heightGap = 0;
}
public CircleLayout (int heightGap) {
this.heightGap = heightGap;
}
/**
* Arranges the parent's Component objects in either an Ellipse or a Circle.
* Ellipse is not yet implemented.
*/
public void layoutContainer (Container parent) {
int x, y, w, h, s, c;
int childCompNum = parent.getComponentCount();
int parentWidth = (int)parent.getSize().width;
int parentHeight = (int)parent.getSize().height;
int centerX = (int) (parentWidth / 2);
int centerY = (int) (parentHeight / 2);
double angleOffset = 0.5 * Math.PI;
Component childComp = null;
for (int i = 0; i < childCompNum; i++) {
childComp = parent.getComponent(i);
w = childComp.getPreferredSize().width;
h = childComp.getPreferredSize().height;
if (childCompNum == 1) {
x = centerX - (int)w / 2;
y = centerY - (int)h / 2;
} else {
c = (int) (centerX * Math.cos((2 * i * Math.PI + angleOffset) / childCompNum));
s = (int) (centerY * Math.sin((2 * i * Math.PI + angleOffset) / childCompNum));
x = c + centerX - (int)w / 2;
y = s + centerY - (int)h / 2;
if (x + w > parentWidth) {x = (int)(parentWidth - w); }
if (y + h + heightGap > parentHeight) {y = (int)(parentHeight - h -heightGap); }
if (x < 0) {x = 0; }
if (y < 0) {y = 0; }
}
childComp.setBounds(x, y, w, h);
}
}
/** For compatibility with LayoutManager interface */
public void addLayoutComponent (String name, Component comp) {}
public Dimension preferredLayoutSize(Container target) {
return target.getSize();
}
public Dimension minimumLayoutSize(Container target) {
return target.getSize();
}
public void removeLayoutComponent(Component comp) {}
}
UPD: я изменил FakeMouseListener для
public void mousePressed(MouseEvent me) {
JScrollPane pane = (JScrollPane)me.getSource();
MouseEvent newMe = SwingUtilities.convertMouseEvent(pane, me, pane.getViewport());
panel1.dispatchEvent(me);
}
Но есть такое же поведение.
1 ответ
Код
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class LostMouseEvent {
private JPanel panel1;
private JPanel panel2 = new JPanel();
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
LostMouseEvent lostMouseEvent = new LostMouseEvent();
}
});
}
public LostMouseEvent() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
panel1 = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
};
panel2 = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 300);
}
};
JScrollPane pane = new JScrollPane(panel2);
panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
panel2.setBorder(BorderFactory.createLineBorder(Color.green));
panel1.setLayout(new CircleLayout());
panel1.add(pane);
frame.add(panel1);
MouseListener rml = new RealMouseListener();
panel1.addMouseListener(rml);
MouseListener fml = new FakeMouseListener();
panel2.addMouseListener(fml);
frame.pack();
frame.setVisible(true);
}
});
}
private class RealMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent me) {
Rectangle rec = SwingUtilities.convertRectangle(panel2, panel2.getVisibleRect(), panel1);
System.out.println(me);
Point point = me.getPoint();
System.out.println(panel1.getComponentAt(point));
System.out.println(panel1.getComponent(0));
}
}
private class FakeMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent me) {
JScrollPane pane = (JScrollPane) me.getSource();
MouseEvent newMe = SwingUtilities.convertMouseEvent(pane, me, pane.getViewport());
panel1.dispatchEvent(me);
}
}
}
возвращается
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: AAA_Format.LostMouseEvent$2$2 cannot be
приведение к javax.swing.JScrollPane в AAA_Format.LostMouseEvent$FakeMouseListener.mousePressed(LostMouseEvent.java:95) в java.awt.Component.processMouseEvent(Component.java:6264) в javax.swing.JComponent.jjavaMase: JComponent.proM 3267) на java.awt.Component.processEvent(Component.java:6032) на java.awt.Container.processEvent(Container.java:2041) на java.awt.Component.dispatchEventImpl(Component.java:4630) на java.awt.Container.dispatchEventImpl(Container.java:2099) в java.awt.Component.dispatchEvent(Component.java:4460) в java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) в java.awt.pat.Wight(Container.java:4235) в java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) в java.awt.Container.dispatchEventImpl(Container.java:2085) в java.awt.Window.dispatchEventImpl(Window.java:2478) в java.awt.Component.dispatchEvent(Component.java:4460) в java.awt.EventQueue.dispatchEvent(EventQueue.java:599) в java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) в java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) в java.awt.EventDispatchThread.pumpEvenD.java:169) в java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) в java.awt.EventDispatchThread.run(EventDispatchThread.java:122).
РЕДАКТИРОВАТЬ
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class LostMouseEvent {
private JPanel panel1;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new LostMouseEvent();
}
});
}
public LostMouseEvent() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
panel1 = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 400);
}
};
JPanel panel2 = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 300);
}
};
JScrollPane pane = new JScrollPane(panel2);
panel1.setBorder(BorderFactory.createLineBorder(Color.blue));
panel2.setBorder(BorderFactory.createLineBorder(Color.green));
panel1.setLayout(new CircleLayout());
panel1.add(pane);
frame.add(panel1);
MouseListener rml = new RealMouseListener();
panel1.addMouseListener(rml);
MouseListener fml = new FakeMouseListener();
pane.addMouseListener(fml);
frame.pack();
frame.setVisible(true);
}
});
}
private class RealMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent me) {
System.out.println(me);
Point point = me.getPoint();
System.out.println(me.getX());
System.out.println(me.getXOnScreen());
System.out.println(me.getY());
System.out.println(me.getYOnScreen());
}
}
private class FakeMouseListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent me) {
JScrollPane pane = (JScrollPane) me.getSource();
MouseEvent newMe = SwingUtilities.convertMouseEvent(pane.getViewport(), me, panel1);
System.out.println(newMe.getX());
System.out.println(newMe.getXOnScreen());
System.out.println(newMe.getY());
System.out.println(newMe.getYOnScreen());
panel1.dispatchEvent(me);
}
}
}
РЕДАКТИРОВАТЬ 2
более логичным можно заменить внутри FakeMouseListener, из
JScrollPane pane = (JScrollPane) me.getSource();
MouseEvent newMe = SwingUtilities.convertMouseEvent(pane.getViewport(), me, panel1);
в
JPanel panel2 = (JPanel) me.getSource();
MouseEvent newMe = SwingUtilities.convertMouseEvent(panel2, me, panel1);