下面列出了javax.swing.JComponent#addHierarchyListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initListeners(final JComponent view) {
view.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
if (view.isShowing()) {
view.removeHierarchyListener(this);
loadPackages(view);
}
}
}
});
controller.addListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
refreshState(view);
}
});
}
private void installVisibilityTracker( final ComponentProviderAdapter provider ) {
JComponent component = provider.getComponent();
component.addHierarchyListener( new HierarchyListener() {
public void hierarchyChanged( HierarchyEvent e ) {
long changeFlags = e.getChangeFlags();
if ( HierarchyEvent.SHOWING_CHANGED ==
(changeFlags & HierarchyEvent.SHOWING_CHANGED) ) {
setSelected( provider.isVisible() );
}
}
} );
}
public static void showDialog(String title, String message, int style) {
JTextArea area = new JTextArea(message);
area.setBackground(UIManager.getColor("OptionPane.background"));
area.setEditable(false);
JComponent content = area;
// If the message is too long then wrap it in
// a scrollable text area and make the font
// fixed width.
if (message.length() >= 128) {
area.setFont(new Font("monospaced", Font.PLAIN, 12));
JScrollPane jp = new JScrollPane(area);
jp.getViewport().add(area);
//area.setColumns(80);
area.setLineWrap(true);
jp.setPreferredSize(new Dimension(800,600));
content = jp;
}
// Make the dialog resizable:
// https://blogs.oracle.com/scblog/entry/tip_making_joptionpane_dialog_resizable
final JComponent pane = content;
pane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(pane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
JOptionPane.showMessageDialog(MAIN, pane, title, style);
}
public static int showOKCancelDialog(String title, String message) {
JTextArea area = new JTextArea(message);
area.setBackground(UIManager.getColor("OptionPane.background"));
area.setEditable(false);
JComponent content = area;
// If the message is too long then wrap it in
// a scrollable text area and make the font
// fixed width.
if (message.length() >= 128) {
area.setFont(new Font("monospaced", Font.PLAIN, 12));
JScrollPane jp = new JScrollPane(area);
jp.getViewport().add(area);
//area.setColumns(80);
area.setLineWrap(true);
jp.setPreferredSize(new Dimension(800,600));
content = jp;
}
// Make the dialog resizable:
// https://blogs.oracle.com/scblog/entry/tip_making_joptionpane_dialog_resizable
final JComponent pane = content;
pane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(pane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
Object[] options = {"Cancel", "OK"};
return JOptionPane.showOptionDialog(MAIN, pane, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}
/**
* This adds this listener to the argument <code>JComponent</code>.
*
* @param jc
* the component added to this hierarchy tree.
*/
public void add(JComponent jc) {
jc.addHierarchyListener(hierarchyListener);
Object value = jc.getRootPane();
if (value == null)
value = NULL_VALUE;
rootPanes.put(jc, value);
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
getFields(c).install();
c.addPropertyChangeListener(AudioPlayerComponent.SOURCE_KEY,
updateSourceListener);
c.addHierarchyListener(hierarchyListener);
}
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (propertyName.equals("demoComponent")) {
Demo demo = (Demo)e.getSource();
JComponent demoComponent = (JComponent)e.getNewValue();
if (demoComponent != null) {
demoComponent.putClientProperty("swingset3.demo", demo);
demoComponent.addHierarchyListener(new DemoVisibilityListener());
registerPopups(demoComponent);
}
}
}
public void propertyChange(PropertyChangeEvent e) {
String propertyName = e.getPropertyName();
if (propertyName.equals("demoComponent")) {
Demo demo = (Demo)e.getSource();
JComponent demoComponent = (JComponent)e.getNewValue();
if (demoComponent != null) {
demoComponent.putClientProperty("swingset3.demo", demo);
demoComponent.addHierarchyListener(new DemoVisibilityListener());
registerPopups(demoComponent);
}
}
}