ComboBox With dynamic UI forms
Answered
Hi All,
I need help related to dynamically changing UI when comboBox Item is selected.
I've created a new GUI form using Intellij form designer(e.g. MainForm) which has a comboBox(with 2 items ) and a panel(e.g. subPanel) below the comboBox. Also, I've created 2 more GUI forms(e.g Item1Form and Item2Form) for each element of the comboBox. I want to dynamically display Item1Form or Item2Form based on the item selected in comboBox in the subPanel of MainForm.
I tried to add Item1Form to subPanel using subPanel.add(new Item1Form().getPanel) But It is not displaying anything. I'm new to the GUI framework and IntelliJ Plugin, help is much appreciated.
Thanks.
Please sign in to leave a comment.
Could you please provide the full sources?
Hi Yann Cebron, Below is the code.
Driver_code() {
ConnectionSettingsGUIForm.createPanel()
}
//main form which has combo box and subpanel whose content I want to update dynamically based on comboItem
public class ConnectionSettingsGUIForm {
private JPanel rootPanel;
private JPanel comboPanel;
private JComboBox comboBox1;
private JPanel subPanel;
public JComponent createPanel() {
String[] profileNames = new String[profileTypes.length];
//fill profileNames array
comboBox1.setModel(new DefaultComboBoxModel(profileNames));
comboBox1.setSelectedIndex(0);
//trying to add a element to sub panel
subPanel.add((new CloudGUIForm()).getPanel());
return rootPanel;
}
}
//one of the sub form which I wanted to add to main form
public class CloudGUIForm {
private JPanel cloudPanel;
private JTextField textField2;
private JTextField textField1;
private JTextField textField3;
private JLabel port;
public JPanel getPanel() {
return cloudPanel;
}
}
I've tried to play with similar scenario:
1) subPanel should have LayoutManager that allows add() without layout conatraints. For example BorderLayout (by default it adds component with constraint CENTER).
2) if you call subPanel.add(...) dynamically you also should call subPanel.revalidate(); subPanel.repaint();
3) Ensure you use properly initialized gui forms: new ConnectionSettingsGUIForm().createPanel()
Actually for GUI Forms we use special compiler and it has to be used automatically, so first of all ensure you can see in your application of plugin very simple GUI form, ensure that it works for you.
4) Please check console output, probably something goes wrong and you cannot see UI just because of exceptions like NullPointerException.