import java.awt.Container; Container container = new Container(); Object lock = container.getTreeLock(); synchronized(lock) { // Code to modify container hierarchy }
import javax.swing.JPanel; JPanel panel = new JPanel(); Object lock = panel.getTreeLock(); synchronized(lock) { // Code to modify panel hierarchy }In this example, we create a JPanel object and get the tree lock from it. We then synchronize the access to the panel hierarchy using the obtained lock. Package Library: The getTreeLock method is included in the java.awt package/library.