Get height of JDialog title bar?

2019-07-29 08:49发布

问题:

How can I get the height of JDialog title bar?

I've tried with getInsets().top, but it returns 0.

回答1:

I don't know if, if you're using OS's own window manager, this may not be possible. The reason is that, the title bar is drawn outside Java. If you really need this information you will probably need to use JNI.

However, if you are using internal frames, you can do the following

JInternalFrame mydialog = new JInternalFrame();
((BasicInternalFrameUI)mydialog.getUI()).getNorthPane().getHeight();

But a more important question is, why do you want to know? The idea of a windowing system is so that the programmer can abstract the content of his application from the window environment. This is so that window appearances can be customised dynamically by the user, look homogeneous across all apps, and not interfere with the application's normal running.

Such an interface between OS and app would require a whole message-passing API to inform when window decorations change etc.



回答2:

Give this a try:

Container c = this.getContentPane();
Point pt = c.getLocation();
pt = SwingUtilities.convertPoint(c, pt, this);

The pt variable now holds the location of the content pane, relative to the origin of the JDialog. Therefore, pt.x is the distance from the left edge and pt.y is the distance from the top.

Caveats:

  • This assumes no JMenuBar. If you have one, use the location of the JMenuBar instead.
  • This will include any borders added to the contentPane, or the layerdPane. You'll need to subtract those out.


回答3:

kofucii, you are on the right path - use the dialog getInsets().top as you did in your example. I guess you got 0 probably because the JDialog object was invisible. Otherwise the 'top' value should be ~36 ...

Here is an isolated case:

package dejan.various;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;

/**
 *
 * @author dejan
 */
public class TestDialog extends JDialog implements ActionListener {
    private javax.swing.JButton testButton;

    public TestDialog() {
        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);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TestDialog().setVisible(true);
            }
        });
    }

} // TestDialog class

On the STDOUT I get:

36
9


回答4:

To get insets you need to make sure your JDialog is visible. if you try to get it before showing on screen you got 0.