Java Swing How to - Get height of JDialog title bar








Question

We would like to know how to get height of JDialog title bar.

Answer

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*from w  w  w. j  ava2  s .  co m*/
import javax.swing.JButton;
import javax.swing.JDialog;

public class Main extends JDialog implements ActionListener {
  private javax.swing.JButton testButton;

  public Main() {
    setPreferredSize(new Dimension(640, 480));
    getContentPane().setLayout(new BorderLayout());
    testButton = new JButton("Click me");
    testButton.addActionListener(this);
    getContentPane().add(testButton, BorderLayout.SOUTH);
    pack();
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    Insets insets = this.getInsets();
    System.out.println(insets.top);
    System.out.println(insets.left);
  }

  public static void main(String args[]) {
    new Main().setVisible(true);
  }

}