JDialog does not respect child components sizes

I have a setup that usually works, but I find it a little painful at the moment.

I have JDialog

(previously a JFrame

, but recently I changed the UI flow to remove redundant JFrames

) set to BorderLayout, which contains three main ones JPanels

, header, content and footer.

The header JPanel

has a background image that loads fine. However, I call all sorts of setMinimumSize

/ setPreferredSize

/ etc (I even tried setSize

and setBounds

out of desperation) and JPanel

it is incorrectly evaluated. The layout is for this component BoxLayout

, and the kids sit comfortably inside it, but that shouldn't affect what I'm trying to do with the title bar itself.

The only thing that works is to set the size of the parent JDialog

. But I don't want to do this / from what I read, it seems like bad design. I would also have to mathematically determine the potential width / height of all children, add margins, etc.

Council. I'm not looking for "use another LayoutManager

."

I want to understand if there is a reason why child components are not being honored.

I can provide code, but isolating a small, runnable segment is tricky given the number of interconnected systems that I juggle. Here is the relevant snippet to set the size JPanel

. Image dimensions are 480 * 96.

public JPanelThemed(BufferedImage image) {
    super();
    this.backgroundImage = image;
    setAllSimilarConstraints(new Dimension(image.getWidth(), image.getHeight()));
    setOpaque(false);
}

// for use with BoxLayout as it requires explicit bounds to be set
public void setAllSimilarConstraints(Dimension dim) {
    setPreferredSize(dim);
    setMinimumSize(dim);
    setMaximumSize(dim);
}

      

-2


source to share


2 answers


If I do not specify dimensions, how can I display the entire background that I am drawing?

You can use JLabel to display ImageIcon. The size of the mark will be the size of the image. Then you install the label layout manager to add components to it.

Edit:

Do you know why it is generally recommended to subclass JPanel when drawing background in Java



When you use a JPanel, the size of the panel depends on the components added to the panel and the layout manager you are using. Then your own drawing code should draw the image the way you want. That is, you can draw the image with (0, 0), or you can center the image on the panel, you can scale the image to fit the area, but the image has no control over the size of the panel, unless you override the getPreferredSize()

panel method to use native code to size larger components or images. Thus, you are in complete control.

When you use the JLabel approach suggested here, the label size is always the same as the image size. The components will then be positioned based on the layout manager, but may be truncated if the image is less than the space required by the components.

So, based on your requirement that you display the entire image, I suggested the JLabel approach, since you don't need to write any custom code. I find this simple approach when using non-resizing popup dialogs to display a background image and a few components and buttons to close the dialog.

You can also check out Background Bar which provides these common drawing functionality in a reusable class.

+2


source


I would implement this a little differently - to ensure that the preferred / min / max / Size cannot be changed from elsewhere:



public JPanelThemed(BufferedImage image) {
    this.backgroundImage = image;
    setOpaque(false);
}

public Dimension getPreferredSize() {
    return new Dimension(this.backgroundImage.getWidth(), this.backgroundImage.getHeight());
}

      

+5


source







All Articles