简体   繁体   中英

Better way to dismiss subwindow than series of casts?

I have a subwindow that performs a search on a DB. On clicking the "Select" button on the subwindow, I want to return the results of the search to the calling window AND dismiss the subwindow. My code is doing this fine, BUT, I was just wondering if there was a better way to dismiss the subwindow than this:

((JDialog)((JPanel)(JButton)evt.getSource()).getParent()).getTopLevelAncestor()).dispose();

The intermediate step of getting the JPanel parent is not necessary since JComponent defines getTopLevelAncestor() :

((JDialog)((JButton)event.getSource()).getTopLevelAncestor()).dispose();

For better readability you can also write:

SwingUtilities.getWindowAncestor((JButton)event.getSource()).dispose();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM