我正在尝试在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};
工作?
答案 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文件的加载过程就像这样:
private TextField[] paths = {path1,path2,path3,path4,path5,path6,path7,path8,path9,path10};
(如果存在)。上述代码在步骤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);
}