Java Swing Tutorial - Java HierarchyEvent PARENT_CHANGED








Syntax

HierarchyEvent.PARENT_CHANGED has the following syntax.

public static final int PARENT_CHANGED

Example

In the following code shows how to use HierarchyEvent.PARENT_CHANGED field.

//  w w  w.  j av a  2s  .co  m
import java.awt.BorderLayout;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Main {
  public static void main(String[] a) {
    JFrame horizontalFrame = new JFrame();
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComponent topButton = new JButton("Left");
    JComponent bottomButton = new JButton("Right");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

    HierarchyListener hierarchyListener = new HierarchyListener() {
      public void hierarchyChanged(HierarchyEvent e) {
        long flags = e.getChangeFlags();      
        if ((flags & HierarchyEvent.PARENT_CHANGED) ==
             HierarchyEvent.PARENT_CHANGED) {
          System.out.println("HierarchyEvent.PARENT_CHANGED");
        }
      }
    };
    splitPane.addHierarchyListener(hierarchyListener);

    splitPane.setTopComponent(topButton);
    splitPane.setBottomComponent(bottomButton);

    
    horizontalFrame.add(splitPane, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);
    horizontalFrame.setVisible(true);

  }
}