I'm working on a JDialog
subclass (let's call it PendenciesDialog
) which shows an arbitrary number of equally sized, vertically stacked JPanel
s to the user (minimally one) and a close button below them. No interaction involved besides building the JDialog
and packing it, displaying it to the user and letting them close the dialog.
These JPanel
s show some info on invididual monthly payment pendencies, such as account name, the pendency status and a clickable link to the payment management system. I don't think we need to care about how they are constructed.
Now for my question. I want to keep the width of the JDialog
fixed at half of the screen width and limit its maximum height to half of the screen height, or shorter if the number of pendencies is small or just one. If I'm unable to achieve that by adjusting the JDialog
(e.g. by overriding the JDialog
's getXXXSize()
methods), I'm okay with adjusting its subcomponents. I'm not sure what I need to do though.
To construct the JDialog
I have set its layout manager to BoxLayout
with PAGE_AXIS
alignment, then added a JScrollPane
to it backed by a JPanel
(let's call this JPanel
's reference variable pendenciesPanel
) and then added a close button. That JPanel
also has a BoxLayout
manager with PAGE_AXIS
alignment and will contain the individual pendency JPanel
s (actually due to the equal sizes requirement I think it should actually be a GridLayout
).
I intend the JScrollPane
to show a view port of pendenciesPanel
and to provide scrolling if the number of pendencies is too large to fit the JDialog
's (or the JScrollPane
's for that matter) maximum height.
So based in this description, how do I achieve the JDialog
size adjustments? From what I've read about it it seems that the most appropriate approach would be to override its getXXXSize()
methods, for instance:
private final Dimension SCREEN_DIMENSION = Toolkit.getDefaultToolkit().getScreenSize();
@Override
public Dimension getMaximumSize() {
return new Dimension(SCREEN_DIMENSION.width / 2, SCREEN_DIMENSION.height / 2);
}
but my attempts to override getMinimumSize()
, getPreferredSize()
, getSize()
etc. don't seem to work, or perhaps I'm implementing them wrong. Will I need to adjust its internal subcomponents? Or just adjusting the JDialog
will be enough? If so, how?