问题描述
有没有办法迭代组件列表并将它们添加到Swing GroupLayout中的ParallelGroup?
Is there a way to iterate over a List of Components and add them to a ParallelGroup in Swing GroupLayout?
这似乎很难,因为没有方法来获取ParallelGroup。
It seems difficult because there is no method to get hold of the ParallelGroup.
这是代码生成组件列表(在本例中为JCheckBoxes)。
Here is the code generating a List of Components (in this case, 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));
}
List正在运行,但我如何迭代List以插入每个JCheckbox进入GroupLayout的ParallelGroup?例如,下面的代码将无法编译。
The List is working, but how can I iterate over the List to insert each JCheckbox into a GroupLayout's 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);
或者,有没有人知道如何获取ParallelGroup以便我可以迭代地添加组件在一个独立的for循环中的那个组?
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?
推荐答案
我可以看到你正在尝试做什么以及你的困惑。您只能对new运算符使用匿名类语法。即
I can see what you're trying to do and your confusion. You can only use anonymous class syntax with the new operator. i.e
new LinkedList<String>() {
{
add("bar");
}
};
但是,只能使用工厂方法createParallelGroup(...)创建ParallelGroup实例。
However ParallelGroup instances can only be created with the factory method 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);
}
这篇关于如何迭代地将组件添加到Swing GroupLayout ParallelGroup?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!