首页 \ 问答 \ Javafx和Observer模式 - 更新UI(Javafx and the Observer pattern - updating a UI)

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
更新时间:2023-09-06 16:09

最满意答案

首先, id属性在单个HTML页面上应该是唯一的。 在你的情况下, id compleavesearchbutton重复两次。

设置值使用

$('#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 case id compleave and searchbutton 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

相关问答

更多
  • 您正在配置文件活动中创建新的Intent。 实际上,您应该使用getIntent()来接收调用该活动的意图。 这将返回一个带有“name”额外值的intent对象。 编辑:实际上,我无法确切地告诉您在哪里尝试读取仪表板发送的值。 在ProfileActivity中你应该有 Intent intent = getIntent(); String name = intent.getStringExtra("name"); 然后,您可以使用“配置文件”中的“仪表板”中的名称。 另请参阅为ProfileActiv ...
  • 鉴于这个HTML:
    一种方法是为每个要引入的字段设置一个参数,并将参数的值设置为您在C#应用程序中想要的值。 在报告中,您将设置每个文本框的值作为参数,它应该可以正常工作。 或者,如果您正在使用RDLC文件(您是),则可以将数据放入数据集并将其传递给报告,然后使报告中的每个字段成为数据集中的一列 One way would be to setup a parameter for each of the fields you want to bring in and set the value of the parameter ...
  • 问题可能在于您没有在正确的名称comment_id中使用控制器中的参数 The problem can be that you haven't permited param in controller with the right name comment_id
  • 既然你动态地添加它,你需要在这里进行event delegation 。 所以只需添加点击如下: $(document).on('click',"#btn",function(){ alert("The button was clicked."); }); 演示在这里 Since you are adding it dynamically you need event delegation here. So just add click as below: $(document).on('clic ...
  • 当asp.net mvc渲染你的ActionLink ,它将生成一个链接,其中包含模型上的参数。 事件你改变了模型的值,它不会改变ActionLink产生的输出值。 鉴于此,您必须重新选择该值,尝试生成没有参数的ACtionLink: @Html.ActionLink("PrintMed", "GetMedEdVerificationReport", "Student", null, new { @class = "button print_selected print_selectedMedEd disa ...
  • 使用名为“keyup”的事件,您可以根据该事件更改单选按钮的值。 请尝试以下代码: