JavaFX初始化TextFields数组

时间:2016-07-07 10:02:38

标签: java arrays javafx initialization

我正在尝试在JavaFX中创建TextFields数组但是收到错误。 我有10个TextFields:path1,path2 ... path10。

//initialization
@FXML
private TextField path1,path2,path3,path4,path5,path6,path7,path8,path9,path10;
@FXML
private TextField[] paths = {path1,path2,path3,path4,path5,path6,path7,path8,path9,path10};

但是,当我写

String text = paths[0].getText();
paths[1].setText(name);

这第一行让我得到NullPointerException。

我找到的解决方案是使用我的Сontroller的Initizlizatior,但这太丑了:

public void initialize(URL fxmlFileColation, ResourceBundle resources) {
    paths[0] = path1;
    paths[1] = path2;
    paths[2] = path3;
    paths[3] = path4;
    paths[4] = path5;
    paths[5] = path6;
    paths[6] = path7;
    paths[7] = path8;
    paths[8] = path9;
    paths[9] = path10;
}

如何在多行中摆脱手动分配并制作

@FXML
 private TextField[] paths = {path1,path2,path3,path4,path5,path6,path7,path8,path9,path10};

工作?

3 个答案:

答案 0 :(得分:2)

您不能使这个确切的代码工作。

中的数组创建
bind = '127.0.0.1:8001'
backlog = 2048
workers = 8
worker_class = 'sync'
worker_connections = 1000
timeout = 300
keepalive = 2
spew = False
daemon = False
pidfile = None
umask = 0
user = None
group = None
tmp_upload_dir = None
errorlog = 'gunicorn_error.log'
loglevel = 'info'
accesslog = 'gunicorn_access.log'
proc_name = None
def post_fork(server, worker):
    server.log.info("Worker spawned (pid: %s)", worker.pid)

def pre_fork(server, worker):
    pass

def pre_exec(server):
    server.log.info("Forked child, re-executing.")

def when_ready(server):
    server.log.info("Server is ready. Spawning workers")

def worker_int(worker):
    worker.log.info("worker received INT or QUIT signal")

    ## get traceback info
    import threading, sys, traceback
    id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# Thread: %s(%d)" % (id2name.get(threadId,""),
            threadId))
        for filename, lineno, name, line in traceback.extract_stack(stack):
            code.append('File: "%s", line %d, in %s' % (filename,
                lineno, name))
            if line:
                code.append("  %s" % (line.strip()))
    worker.log.debug("\n".join(code))

def worker_abort(worker):
    worker.log.info("worker received SIGABRT signal")

发生在初始化程序中。 fxml文件的加载过程就像这样:

  1. 创建控制器类实例(或使用现有实例)。
  2. 加载fxml内容,注入由加载器创建的对象。
  3. 在控制器上调用private TextField[] paths = {path1,path2,path3,path4,path5,path6,path7,path8,path9,path10}; (如果存在)。
  4. 上述代码在步骤1中执行,而要在数组中存储的对象稍后在步骤2中创建。

    您可以使用fxml创建initialize List并将其注入控制器(see my answer to Grouping together JavaFX FXML Objects

    来解决此问题。

    当然,您还可以使用TextField方法中的数组初始值设定项创建包含特定元素的TextField数组:

    initialize

答案 1 :(得分:0)

我认为你不能。您需要单独声明变量,以便FXML可以初始化变量。然后,您需要通过将每个元素设置为某个来使您的TextField数组工作。最好的方法是使用initialize(...)函数中的解决方案并将其用于它的意图 - 初始化。

答案 2 :(得分:0)

我使用的简单解决方案

 TextField[] txt = new TextField[beanFields.length];
 for (int i=0; i<=beanFields.length-1; i++) {
                TextField textField = new TextField();
                txt[i] = textField;
                textField.setPrefWidth(200);
                textField.setPrefHeight(32);
    }