我有一个每行有多个组合框的面板,我需要从中获取组合框值。
这是我的代码:
gridPanel = new JPanel();
grid = new GridLayout(0,1);
gridPanel.setLayout(grid);
gridPanel.add(createChildPanel());
和createChildPanel方法:
JComboBox columnACB = new JComboBox();
columnACB.addItemListener(this);
感谢您的解决方案:这就是我们使用的
Component[] comps = gridPanel.getComponents();
for (Component comp : comps) {
if (comp instanceof JPanel) {
JPanel panel = (JPanel) comp;
Component[] comps1 = panel.getComponents();
for (Component comp1 : comps1) {
if (comp1 instanceof JComboBox)
{
JComboBox combp = (JComboBox) comp1;
String colA = combp.getSelectedItem().toString();
System.out.println("colA"+colA);
}
else if (comp1 instanceof JTextField)
{
JTextField combp = (JTextField) comp1;
String colA = combp.getText();
System.out.println("colA"+colA);
}
}
}
}
答案 0 :(得分:1)
您可以使用getComponents()迭代面板中的组件。
Component[] comps = gridPanel.getComponents();
for (Compoent comp : comps) {
if (comp instanceof JComboBox) {
JComboBox combo = (JComboBox) comp;
Object selected = combo.getSelectedItem();
}
}