[英]How to iteratively add Components to a Swing GroupLayout ParallelGroup?
Is there a way to iterate over a List of Components and add them to a ParallelGroup in Swing GroupLayout?有没有办法遍历组件列表并将它们添加到 Swing GroupLayout 中的 ParallelGroup?
It seems difficult because there is no method to get hold of the ParallelGroup.这似乎很难,因为没有办法掌握 ParallelGroup。
Here is the code generating a List of Components (in this case, JCheckBoxes).下面是生成组件列表(在本例中为 JCheckBoxes)的代码。
List<JCheckBox> listCustomiseJCB = new ArrayList<>();
for (int w = 0; w < initialCMTableColumns.size(); w++) {
String heading = (String)initialCMTableColumns.get(w).getHeaderValue();
listCustomiseJCB.add(new JCheckBox(heading));
}
The List is working, but how can I iterate over the List to insert each JCheckbox into a GroupLayout's ParallelGroup? List 正在工作,但我如何遍历 List 以将每个 JCheckbox 插入 GroupLayout 的 ParallelGroup? For example, the below code won't compile.
例如,下面的代码将无法编译。
GroupLayout gl = new GroupLayout(jpnlCustomise);
jpnlCustomise.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);
GroupLayout.SequentialGroup hGroup = gl.createSequentialGroup();
hGroup
.addComponent(jbtnApply);
hGroup.addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
// ERRORS BEGIN HERE
{ for (JCheckBox c: listCustomiseJCB) {
.addComponent(c);
}});
// ERRORS END HERE
hGroup
.addComponent(jbtnCancel);
gl.setHorizontalGroup(hGroup);
Alternatively, does anyone know of a way to get hold of a ParallelGroup so that I could iteratively add Components to that group in a standalone for loop?或者,有谁知道获取 ParallelGroup 的方法,以便我可以在独立的 for 循环中迭代地将组件添加到该组?
I can see what you're trying to do and your confusion.我可以看到你想做什么和你的困惑。 You can only use anonymous class syntax with the new operator.
您只能将匿名 class 语法与 new 运算符一起使用。 ie
IE
new LinkedList<String>() {
{
add("bar");
}
};
However ParallelGroup instances can only be created with the factory method createParallelGroup(...).但是,ParallelGroup 实例只能使用工厂方法 createParallelGroup(...) 创建。
You'll have to use a temporary reference to the parallel group:您必须使用对并行组的临时引用:
ParallelGroup pGroup = gl
.createParallelGroup(GroupLayout.Alignment.CENTER);
hGroup.addGroup(pGroup);
for (JCheckBox c : listCustomiseJCB) {
pGroup.addComponent(c);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.