Swing:如何将所有事件从子组件转发到父容器? [英] Swing: How to achieve forwarding of all events from subcomponents to parent container?

查看:213
本文介绍了Swing:如何将所有事件从子组件转发到父容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的方法来使一个 Swing组件将所有收到的
事件转发给其父容器
(或者甚至所有父级到root)。



编辑

我需要在哪里?我有一个图编辑器。组件必须转发按键和
鼠标点击(只要用户单击该组件的子元素
,就将其设置为活动)。



<首先,让我介绍我现有的解决方案。这是一个解决方法。

  public interface IUiAction {
void perform(Component c);
}

public static void performRecursiveUiAction(Container parent,IUiAction action){
if(parent == null){
return;
}

for(Component c:parent.getComponents()){
if(c!= null){
action.perform(c);

}

(组件c:parent.getComponents()){
if(c instanceof Container){
performRecursiveUiAction((Container) c,行动);
}
}
}

/ **
* 1)将监听器添加到容器和所有现有组件(递归)。
* 2)通过向容器添加一个ContainerListener,确保所有进一步添加的
*组件也将获得所需的监听器。
*
*有用的示例:确保整个组件
* tree中的每个组件都将在鼠标单击时作出反应。
* /
public static void addPermanentListenerRecursively(Container container,
final IUiAction adder){

final ContainerListener addListener = new ContainerAdapter(){
@Override
public void componentAdded(ContainerEvent e){
adder.perform(e.getChild());
}
};

//步骤1)
performRecursiveUiAction(container,adder);

//步骤2)
performRecursiveUiAction(container,new IUiAction(){
@Override
public void perform(Component c){
if( c instanceof Container){
((Container)c).addContainerListener(addsListener);
}
}
});
}

用法:

  addPermanentListenerRecursively(someContainer,
new IUiAction(
@Override
public void perform(Component c){
c.addMouseListener(somePermanentMouseListener);
}

);

通过查看代码,你会说这是一个很好的概念吗?

我目前的概念的问题是:它只转发事件,手动指定了一个监听器。



你能建议一个更好的吗?

解决方案

如果您的视图中的每个组件都处理相同的鼠标事件,这看起来会有点棘手。即,如果用户拖动项目1 ,项目2 也将处理这些事件?如果我理解正确,那么您正在寻找这个 this.parent 这个。 parent.parent 处理鼠标操作。在这种情况下,递归将会上升,而不是下降。



您可以创建一个接口,如下所示:

  public interface MyInterface(){
public void performSpecialAction(Event event);
}

然后让您的容器实现此界面。您的组件将需要将此调用合并到适当的事件处理中:

  public static void performRecursiveUiAction(Component comp,Event event) {
if(comp.getParent()== null){
return;
}
if(comp.getParent()instanceof MyInteface){
((MyInterface)comp.getParent())。performSpecialAction(event);
}
ThisUtility.performRecursiveUiAction(comp.getParent(),event);

}


I'm looking for a straightforward way to make a Swing component forward all received events to its parent container (or even all parents up to root).

EDIT:
Where do I need this? I have a diagram editor. Components must forward key press and mouse clicks (to set themselves as "active" as soon as the user clicks a subelement of that component).

First, let me present my existing solution for this. It's a bit of a workaround.

public interface IUiAction {
 void perform(Component c);
}

public static void performRecursiveUiAction(Container parent, IUiAction action) {
 if (parent == null) {
  return;
 }

 for (Component c : parent.getComponents()) {
  if (c != null) {
   action.perform(c);
  }
 }

 for (Component c : parent.getComponents()) {
  if (c instanceof Container) {
   performRecursiveUiAction((Container) c, action);
  }
 }
}

/**
* 1) Add listener to container and all existing components (recursively).
* 2) By adding a ContainerListener to container, ensure that all further added
* components will also get the desired listener.
*
* Useful example: Ensure that every component in the whole component
* tree will react on mouse click.
*/
public static void addPermanentListenerRecursively(Container container,
  final IUiAction adder) {

 final ContainerListener addingListener = new ContainerAdapter() {
  @Override
  public void componentAdded(ContainerEvent e) {
   adder.perform(e.getChild());
  }
 };

 // step 1)
 performRecursiveUiAction(container, adder);

 // step 2)
 performRecursiveUiAction(container, new IUiAction() {
  @Override
  public void perform(Component c) {
   if (c instanceof Container) {
    ((Container) c).addContainerListener(addingListener);
   }
  }
 });
}

Usage:

addPermanentListenerRecursively(someContainer,
  new IUiAction(
    @Override
    public void perform(Component c){
      c.addMouseListener(somePermanentMouseListener);
    }
  )
);

By looking over the code, would you say it's a good concept?
The problem with my current concept is: It's forwarding only events, for which a listener was specified manually.

Can you suggest a better one?

解决方案

This looks like it would get a bit tricky if every component in your view handled the same mouse event. ie, if the user drags item 1, item 2 would process those events as well? if i understand correctly, you are looking to have this and this.parent and this.parent.parent handle mouse actions on this. in that case the recursion would be up, and not down.

you could create an Interface like this:

public interface MyInterface() {
   public void performSpecialAction(Event event);
}

you then have your containers implement this interface. your components would then need to incorporate this call to their appropriate event handling:

public static void performRecursiveUiAction(Component comp, Event event) {
  if (comp.getParent() == null) {
     return;
  }
  if (comp.getParent() instanceof MyInteface) {
     ((MyInterface)comp.getParent()).performSpecialAction(event);
  }
  ThisUtility.performRecursiveUiAction(comp.getParent(), event);

}

这篇关于Swing:如何将所有事件从子组件转发到父容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆