How to get component located in a specific gridx, gridy from a JPanel with GridBagLayout layout?

前端 未结 1 1677
太阳男子
太阳男子 2021-01-21 20:19

I have a jpanel with gridbaglayout layout, in it i have several jtextfields, several jlabels, several jbuttons which get added dynamically. Therefore I cannot know their specifi

相关标签:
1条回答
  • 2021-01-21 20:52

    The simplest solution might be to use GridBagLayout#getConstraints(Component) and simply loop through all the components until you find one that matches the required grid position...

    Component match = null;
    GridBagLayout layout = ...
    for (Component comp : getComponents()) {
        GridBagConstraints gbc = layout.getConstraints(comp);
        if (gbc.gridx = x && gbc.gridy = y) {
            match = comp;
            break;
        }
    }
    
    0 讨论(0)
提交回复
热议问题