Reputation: 727
I have the code below to add JPanel to another JPanel the ui is not displayed .
I have tried changing the User_UI to just a JButton but it is not displayed as well.
It seem quite straight forward did i make a mistake ?
private void startActionPerformed(java.awt.event.ActionEvent evt) {
userArray = new User_UI[9];
for (int x = 0; x < 9; x++) {
User_UI tmp = new User_UI(); // JPanel Object
mainPanel.add(tmp); // Adding to a JPanel with GridLayout
}
validate();
}
I have this method in my user_ui class which i used to update a JLabel. However everytime i excute the function is whole panel (mainPanel) will disapper.
public void setID(final String id) {
System.out.println("ID SET to " + id);
this.id = id;
id_no.setText(id);
}
Upvotes: 0
Views: 65
Reputation: 285403
You must call revalidate()
on the mainPanel after adding new components to it as this tells the containers layout managers to re-layout all components. Also the mainPanel must use a layout manager that is conducive towards allowing components to be added on the fly (i.e., not GroupLayout). Sometimes you must also call repaint()
after revalidate, especially if components are removed.
Upvotes: 2
Reputation: 3140
try this
private void startActionPerformed(java.awt.event.ActionEvent evt) {
userArray = new User_UI[9];
for (int x = 0; x < 9; x++) {
User_UI tmp = new User_UI(); // JPanel Object
mainPanel.add(tmp); // Adding to a JPanel with GridLayout
}
repaint();
}
Upvotes: 0