Javafx和Observer模式 - 更新UI(Javafx and the Observer pattern - updating a UI)
我正在尝试在JavaFx应用程序中实现Observer模式。 我从来没有问过这个问题,但这让我有点疯狂。
本质上我正在尝试使用Observer模式来监视一个解析电话号码文件的类,并在解析文件时自动更新UI。
在我回答我的问题之前,这是我的代码:
抽象类Observer.java
public abstract class Observer { public PhoneBook numbers; public abstract void update(); }
我有一个实现这个的类:
public class PhoneBookObserver extends Observer { public PhoneBookObserver(PhoneBook numbers) { this.numbers = numbers; this.numbers.attach(this); } @Override public void update() { System.out.println(""NUMBER - : " + numbers.GetNumbers()); } }
在进行解析的类中,我创建了一个新的PhoneBookObserver
public PhoneBook ParsePhoneBook() { PhoneBook nums= new PhoneBook(); PhoneBookObserver p = new PhoneBookObserver(nums); // ... Parsing of file - works fine return nums; }
目前运行并输出我在PhoneBookObserver中的update()的println。
我的问题是:
- PhoneBookObserver的更新方法可以为我更新我的UI吗? 它如何访问控制器中的JavaFx元素?
- 我可以让我的控制器成为观察者,覆盖update()并使用它来更新我的控制器中的UI元素吗? 那不好吗?
I am trying to implement the Observer pattern in a JavaFx application. I've never asked a question here but this is driving me a bit crazy.
Essentially I'm trying to use the Observer pattern to monitor a class that's parsing a file of phone numbers, and update the UI automatically as the file is parsed.
Before I get to my questions, here is my code:
Abstract class Observer.java
public abstract class Observer { public PhoneBook numbers; public abstract void update(); }
I have a class that implements this:
public class PhoneBookObserver extends Observer { public PhoneBookObserver(PhoneBook numbers) { this.numbers = numbers; this.numbers.attach(this); } @Override public void update() { System.out.println(""NUMBER - : " + numbers.GetNumbers()); } }
In the class doing the parsing, I've created a new PhoneBookObserver
public PhoneBook ParsePhoneBook() { PhoneBook nums= new PhoneBook(); PhoneBookObserver p = new PhoneBookObserver(nums); // ... Parsing of file - works fine return nums; }
Currently this runs and my println from update() in PhoneBookObserver is output.
My questions are:
- Can the update method of PhoneBookObserver update my UI for me? How would it access JavaFx elements in my controller?
- Can I just make my controller an observer, override update() and use that to update my UI elements from within my controller? Is that bad?
原文:https://stackoverflow.com/questions/42566161
最满意答案
首先,
id
属性在单个HTML页面上应该是唯一的。 在你的情况下,id
compleave
和searchbutton
重复两次。设置值使用
$('#ltype').val($(this).val());
以下是合并代码:
$(document).ready(function(){ $('#compleave').on('click', function(event) { $('#show1').toggle(); $("#but").remove(); $('#ltype').val($(this).val()); $("#apply").click(funcapply); }); });
更新
要获取单击的每个离开按钮的值,请将基于id的事件更改为类,
.appButton
替换为.appButton
$(document).ready(function(){ $('.appButton').on('click', function(event) { $('#show1').toggle(); $("#but").remove(); $('#ltype').val($(this).val()); $("#apply").click(funcapply); }); });
jsfiddle中的工作示例
First of all
id
attribute should be unique on a single HTML page. In your caseid
compleave
andsearchbutton
are repeated twice.To set a value use
$('#ltype').val($(this).val());
Below is the merged code:
$(document).ready(function(){ $('#compleave').on('click', function(event) { $('#show1').toggle(); $("#but").remove(); $('#ltype').val($(this).val()); $("#apply").click(funcapply); }); });
Update
To get value of each leave button clicked, change id based event to class i.e. replace
#compleave
with.appButton
$(document).ready(function(){ $('.appButton').on('click', function(event) { $('#show1').toggle(); $("#but").remove(); $('#ltype').val($(this).val()); $("#apply").click(funcapply); }); });
Working example in jsfiddle
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
如何通过按钮将值传递给android代码中的另一个活动?(How to pass a value through button to another activity in android code?)[2023-04-20]
您正在配置文件活动中创建新的Intent。 实际上,您应该使用getIntent()来接收调用该活动的意图。 这将返回一个带有“name”额外值的intent对象。 编辑:实际上,我无法确切地告诉您在哪里尝试读取仪表板发送的值。 在ProfileActivity中你应该有 Intent intent = getIntent(); String name = intent.getStringExtra("name"); 然后,您可以使用“配置文件”中的“仪表板”中的名称。 另请参阅为ProfileActiv ...