I have a jpanel that contains a jframe, when i press on a button on that jframe I want to access the jpanel
I use the method getParent()
but that doesn't work
public class StorePanel extends javax.swing.JPanel {
private TableProducts tableproducts;
......
}
code of jframe to get the parent
private void confirmActionPerformed(java.awt.event.ActionEvent evt) { 开发者_StackOverflow
StorePanel store=(StorePanel)this.getParent();
store.getitemTable();
.........
}
Thanks
JComponent.getTopLevelAncestor() presuming the same conditions as the other people who replied.
I assume that your post is in error, that the JFrame contains the JPanel. If so, and if no other containers exist between the JPanel and the JFrame, getting the parent will likely get the JFrame's contentPane. If you want to get the JFrame, then try the SwingUtilities#getWindowAncestor method on the JPanel.
Otherwise, if you are trying to get a JPanel that is held by the JFrame, then you can iterate recursively through it's components (via the getComponents method), but the easiest way is to pass a reference of the JPanel to the JFrame when one or the other are created or added into the other.
精彩评论