在fxml中使用自定义控件 [英] Using custom Controls in fxml

查看:559
本文介绍了在fxml中使用自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经将javafx提供的默认 TableView< T> 类子类化,并创建了一个类 PersonTableView扩展了TableView< Person> 。该子类存在于java代码中,根本不使用fxml。它定义并封装了我专门为我的 Person 对象所需的行为。

Let's say I have subclassed the default TableView<T> class provided by javafx and created a class PersonTableView extends TableView<Person>. That subclass exists in java code and does not use fxml at all. It defines and encapsulates behaviour that I need specifically for my Person object.

现在我想使用一个实例我的自定义类在fxml文件中,就像我将使用所有默认控件一样。这正是我的问题,我不知道如何做到这一点,或者这是否是一个好的/共同的设计决定。

Now I want to use an instance of my custom class inside of an fxml file, just like I would use all the default controls. And that exactly is my problem, I don't know how I can do that, or if this is even a good / common design decision.

我想封装我的特定TableView在其自己的类中的行为,但布局应该在fxml中定义,因为它与逻辑无关,它只是化妆品。

I want to encapsulate the behaviour for my specific TableView inside its own class, but the layout should be defined in fxml as it has nothing to do with logic, it is only cosmetics.

我想象一个类似于它的语法和功能可以在.NET的WPF中找到,你可以像任何其他控件一样在标记中使用自定义类,因为xaml和c#比java和fxml更紧密耦合。

I imagine a kind of syntax and functionality like it can be found in WPF from .NET, where you can use your custom classes in markup like any other control, because xaml and c# are more tightly coupled than java and fxml.

从我目前的观点来看,我所描述的内容无法完成,而我最终只会使用非常少量的fxml和更多代码,即使对于刚刚布局的部分也是如此。例如,我不想使用这样的代码:

From my current Point of View, what I described cannot be done and I would instead end up using only a very small amount of fxml and a lot more code, even for the parts that are just layout. For Example I do not want to use code like this:

 AnchorPane.setRightAnchor(customControl, 65.0);

因为我相信在我的fxml中定义它是一个好主意。

because I believe that it is a good Idea to have this defined inside my fxml.

所以我的问题是,我如何实现上面所描述的内容;或者,如果这种情况不常见,那么获得类似功能的常用最佳实践方法是什么?

So my question is either, how do I implement what was just described above; or, if that is uncommon, what is the common, "best-practice" way to get similar functionality to what I just described?

推荐答案

这是你在找什么?这对我来说很好。

Is this what you were looking for? This works fine for me.

package numerictextfield;

import java.util.regex.Pattern;

import javafx.scene.control.IndexRange;
import javafx.scene.control.TextField;

public class NumericTextField extends TextField {

    private final Pattern intPattern = Pattern.compile("[0-9]*");

    public NumericTextField(String text) {
        super(text);
    }
    public NumericTextField() {
        super();
        this.insertText(0, "");
        this.replaceSelection("");
        this.replaceText(new IndexRange(0, 0), "");
        this.replaceText(0, 0, "");
    }

    @Override
    public void insertText(int index, String text) {
        if (intPattern.matcher(text).matches()) {
            super.insertText(0, text);
        }
    }

    @Override
    public void replaceSelection(String text) {
        if (intPattern.matcher(text).matches()) {
            super.replaceSelection(text);
        }
    }

    @Override
    public void replaceText(IndexRange range, String text) {
        if (intPattern.matcher(text).matches()) {
            super.replaceText(range, text);
        }
    }

    @Override
    public void replaceText(int start, int end, String text) {
        if (intPattern.matcher(text).matches()) {
            super.replaceText(start, end, text);
        }
    }

}

然后

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<?import numerictextfield.NumericTextField?>

<AnchorPane xmlns:fx="http://javafx.com/fxml" >
    <NumericTextField text="12345" >
        <AnchorPane.rightAnchor>65.0</AnchorPane.rightAnchor>
    </NumericTextField>
</AnchorPane>

这篇关于在fxml中使用自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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