amaidment
amaidment

Reputation: 7268

How can I get the height of the title bar of a JInternalFrame?

How can I find out the height of the title bar of a JInternalFrame? It would be preferable to do this in an OS and L&F independent manner.

I have seen this SO question, which seems to be asking a similar question but for JDialog. However, the answers seem to suggest using the Container method getInsets(), for which the javadoc states:

Determines the insets of this container, which indicate the size of the container's border.

A Frame object, for example, has a top inset that corresponds to the height of the frame's title bar.

Unfortunately, JInternalFrame is not a Frame, but a JComponent. As such, this only provides the size of the border.

In the previous SO question, it was asked what the use case is... so let me explain mine. I want to create JInternalFrames, popped up at the point of the mouse click, and sized to some maximum/minimum criteria, including that it is no larger than currently visible space to the bottom-right of the mouse click within the parent frame.

Consider the following SSCCE:

/**
 * @author amaidment
 */
public class JInternalFrameToy {

  public static void main(String[] args) {
    // create top-level frame
    final JFrame frame = new JFrame("JInternalFrame Toy");
    frame.setPreferredSize(new Dimension(500, 500));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // create desktop pane
    final JDesktopPane pane = new JDesktopPane();

    // put button on the base of the desktop pane
    JButton button = new JButton("Create Internal Frame");
    button.setSize(new Dimension(100,100));
    button.setVisible(true);
    button.setLocation(0, 0);
    button.setSelected(true);
    pane.add(button);
    frame.setContentPane(pane);

    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        // create internal frame
        JInternalFrame internal = new JInternalFrame("Internal Frame", 
          true, true, false, false);
        JLabel label = new JLabel("Hello world!");

        // set size of internal frame
        Dimension parentSize = pane.getParent().getSize();
        Insets insets = internal.getInsets();
        Point point = pane.getMousePosition();
        int width = parentSize.width - (insets.left+insets.right) - point.x;
        int height = parentSize.height - (insets.top+insets.bottom) -  point.y;
        label.setPreferredSize(new Dimension(width, height));

        internal.setContentPane(label);
        internal.setLocation(point);
        internal.pack();
        internal.setVisible(true);
        pane.add(internal);
        internal.toFront();
      }
    });

    frame.pack();
    frame.setVisible(true);
  }
}

As you will see if you run this, the internal popups are sized to fit the width, but not the height - i.e. the height exceeds the frame size by... the height of the JInternalFrame's title bar. As such, I want to find out this height, so that I can set the size of the JInternalFrame appropriately.

Upvotes: 0

Views: 3574

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347184

You could try something like

( (javax.swing.plaf.basic.BasicInternalFrameUI) getUI()).getNorthPane().getPreferredSize();

Upvotes: 1

amaidment
amaidment

Reputation: 7268

The best I have come up with so far...

Dimension parentSize = pane.getParent().getSize();
// get the initial preferred size - this has to be done before setContentPane()
// the height should, I think, be the border + height of the title bar
// the width is based on the title name, icon, etc. so is not useful
Dimension initSize = internal.getPreferredSize(); 

// then get the insets, which can be used for the horizontal border
Insets insets = internal.getInsets();

Point point = pane.getMousePosition();
int width = parentSize.width - (insets.left+insets.right) - point.x;
int height = parentSize.height - initSize.height -  point.y;
label.setPreferredSize(new Dimension(width, height));

Not sure if this is the best way of doing this, so would still welcome alternative answers or suggested improvements...

Upvotes: 1

Sean
Sean

Reputation: 789

can you call getHeight() of that particular JComponent? see documentation

Upvotes: -1

Related Questions