简体   繁体   English

swt单选按钮的通用侦听器

[英]generic listeners for swt radio buttons

I'm developing a SWT app and in one particular form there are 14 pairs of Yes-No radio buttons. 我正在开发SWT应用程序,在一种特殊形式下,有14对“是/否”单选按钮。 Each of these pairs have a text box associated with them. 这些对中的每对都有一个与之关联的文本框。 So if a user selects Yes, the associated textbox should be editable else uneditable. 因此,如果用户选择是,则关联的文本框应可编辑,否则不可编辑。 I find writing 28 listeners for the radio buttons really daunting. 我发现为单选按钮编写28个听众确实令人生畏。 Since the radio buttons have nothing much to do than just rendering the textbox editable/uneditable I was hoping if there were some generic type of listeners in SWT that would be applicable to a set of radio buttons specified in an array or like that. 由于单选按钮只需要使文本框可编辑/不可编辑就无济于事,所以我希望SWT中是否有某种通用类型的侦听器适用于在数组中指定的一组单选按钮或类似的设置。 Are there any frameworks or shall I have to write individual listeners? 是否有任何框架,或者我必须编写个人侦听器?

Edit 编辑
I'm trying to fire an event only when the radio button is selected 我仅在选择单选按钮时才尝试触发事件

rdoExperience.addListener(SWT.CHECK, new RadioButtonSelection( txtExperience));

but SWT.CHECK is causing the event to be fired on mouse hover over radio button too. 但是SWT.CHECK也会导致事件在鼠标悬停在单选按钮上时触发。 I've tried using SWT.SELECTED too but it's not working either and I can't find other suitable SWT constants. 我也尝试过使用SWT.SELECTED,但是它也不起作用,也找不到其他合适的SWT常量。 W;hat should I use? 我应该用什么?

Good point. 好点子。 Sorry I don't know such thing. 对不起,我不知道这样的事。

However, you create one yourself: Instead of writing an anonymous listener for every button, you could write one - say MyButtonListener - and give it the button text box as an argument. 但是,您可以自己创建一个:不必为每个按钮编写匿名侦听器,而是可以编写一个(例如MyButtonListener )并将 按钮 文本框作为参数。 Than you instantiate MyButtonListener with the appropriate text box as an argument. 比起用适当的文本框作为参数实例化MyButtonListener Than in the Listeners appropriate callback method you enable or disable the text box. 比在侦听器中适当的回调方法中,您可以启用或禁用文本框。

Edit: My bad. 编辑:我不好。 Of course I meant you could give it your text box like radioBtn.addListener(SWT.SELECTED, new MyButtonListener(textfield1)); 当然,我的意思是您可以给它输入文本框,例如radioBtn.addListener(SWT.SELECTED, new MyButtonListener(textfield1));

You could create one SelectionListener and add it to each of the radio buttons. 您可以创建一个SelectionListener并将其添加到每个单选按钮中。 Then you can ascertain which button was pressed from the selection event and map that to a text box. 然后,您可以确定从选择事件中按下了哪个按钮,并将其映射到文本框。 For the mapping you could use an array or hashtable. 对于映射,您可以使用数组或哈希表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM