-
Notifications
You must be signed in to change notification settings - Fork 0
Swing component not load in GUI
minsusun edited this page Feb 20, 2024
·
1 revision
Related with #14.
When a Swing component has a parent, it cannot have a new parent.
For example, if you create a new JPanel with the new keyword and then insert the original component into it, the component will not be visible on the screen.
void addListIntoJPanel(JPanel jPanel){
for(JTextPane elem : list){
jPanel.add(elem);
}
}
void showTab(){
JPanel jPanel = new JPanel();
addListIntoJPanel(jPanel);
openJPanel(); // show this jPanel into new window
}
public static void main(String[] args) {
showTab(); // in first tab, every components in list are shown
showTab(); // in second tab, any components in list are **not** shown.
}
This is problem 1 what I suffered.
Therefore, when reusing swing components, you must check if the parent exists and, if so, delete it.
void addListIntoJPanel(JPanel jPanel){
for(JTextPane elem : list){
if(elem.getParent() != null){
elem.getParent().remove(elem);
}
jPanel.add(elem);
}
}