绑定JComponent和ComponentUI委托之间的事件 [英] Binding events between JComponent and ComponentUI delegate

查看:240
本文介绍了绑定JComponent和ComponentUI委托之间的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始尝试创建正常的MVC Swing组件。我没有M和C的问题,但是V给了我一个我通常不能解决的问题。
问题是:Controller是组件的主类(例如MyComponent),它扩展了JComponent。 View是从ComponentUI类扩展的ui委托(MyCompanentUI)。所有委托做的都是在MyCompanent中添加JTextField,并在MyComponentModel和此字段之间提供数据绑定。它的工作很好。但是如何将事件从JTextField绑定到MyComponent?
如果用户想要处理某些事件,他将侦听器添加到MyComponent,但所有真实事件(鼠标,焦点,键等)都被JTextField拦截,关于哪个用户不太了解。
除了捕捉事件并用手将其翻译成原始组件,所以有什么常规的方法吗?还是有另一种方式来创建代表,我只是真的做错了?

I have started trying to create normal MVC Swing components. I have no problems with M and C, but V had thrown at me one problem which I cannot normally solve. The problem is: Controller is main class of the component (MyComponent, for example), and it extends JComponent. View is ui delegate (MyCompanentUI) extended from ComponentUI class. All what delegate does is adds JTextField in MyCompanent and provides data binding between MyComponentModel and this field. It works just fine. But how I can bind events from JTextField to MyComponent? If user wants to handle some events he adds listeners to MyComponent, but all real events (mouse, focus, keys, etc.) intercepted by JTextField, about which user does not really knows. So is there any normal way to do this, except catching events and translate it to original component by hands? Or is there another way to create delegate and I just really do it all wrong?

UPD:

谢谢为你的回应,垃圾。
但我心里有些不一样的东西。我正在谈论像事件继承这样的东西,就像在inheritPopupMenu方法一样。所以那么键,焦点或鼠标事件发生在组件中,它不会自己处理它,而是直接将其传递给父组件。但是似乎不可能,因为我注意到JSpinner有完全相同的问题 - 你几乎不能从这个组件获得任何事件通知。

Thanks for your response, trashgod. But I had something different in my mind. I was talking about something like "events inheritance", like in the case of "inheritsPopupMenu" method. So that then key, focus or mouse event happens to the component one does not process it itself, but directly transfer it to parent component. But it seems impossible, because I have noticed JSpinner has exactly the same issue - you cannot get almost any event notification from this very component.

推荐答案

如果您正在编写自己的 JComponent 子类,并希望允许自定义UI代理,我将从Kirill Grouchnikov的 如何编写自定义的Swing组件

If you are writing your own JComponent subclass and want to allow for custom UI delegates, I'd start with Kirill Grouchnikov's How to Write a Custom Swing Component.

如果您正在编写一个包含现有 JComponent 子类的组合,例如 JTextField ,看看是否可以利用 Action 实例。 oracle.com/javase/tutorial/uiswing/misc/keybinding.htmlrel =nofollow noreferrer> 如何使用密钥绑定 ScrollAction 是一个例子。您可以从组件的来源或使用 @ camickr's 方便的实用程序了解此类操作的名称, a href =http://tips4java.wordpress.com/2008/10/10/key-bindings/ =nofollow noreferrer> 密钥绑定

If you are writing a composite that includes an existing JComponent subclass, such as JTextField, see if you can leverage the existing Action instances described in How to Use Key Bindings. ScrollAction is an example. You can learn the names of such actions from the component's source(s) or using @camickr's handy utility seen in the article Key Bindings.

这篇关于绑定JComponent和ComponentUI委托之间的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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