Setting the Maximum Size of a Jdialog

Setting the maximum size of a JDialog?

Make the panel imlement Scrollable is the way to go. See also (in addition to the tutorial link Trashgod already provided) Rob's blog entry

http://tips4java.wordpress.com/2009/12/20/scrollable-panel/

then:

1) implement getPreferredScrollableViewportSize() in terms reasonable for the typical content (for a JList f.i. that's the preferred number of rows to show, aka: visibleRowCount)

2) implement setters/getters for those "reasonable terms"

That additional layer (coordiates "reasonable terms") allows all collaborating components to do their best to come up with robust size hints without unfriendly interference as setXXSize (which is a no-no-never-ever, simply forget those methods exist ;)

How to limit the dimensions of a JDialog

I created a simple GUI to illustrate my comment. Here's the JFrame.

JDialog Test GUI 1

Here's the JDialog.

JDialog Test GUI 2

Here's the complete example code. You'll have to modify the createPendenciesPanel method to be more realistic.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class JDialogTest implements Runnable {

public static void main(String[] args) {
SwingUtilities.invokeLater(new JDialogTest());
}

private JFrame frame;

@Override
public void run() {
frame = new JFrame("JDialog Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(createMainPanel());

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

private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(
150, 100, 150, 100));
panel.setPreferredSize(new Dimension(400, 600));

JButton button = new JButton("Open JDialog");
button.addActionListener(new ButtonListener());
panel.add(button);

return panel;
}

public class ButtonListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
new PendenciesDialog(frame, "Pendencies Dialog");
}

}

public class PendenciesDialog extends JDialog {

private static final long serialVersionUID = 1L;

public PendenciesDialog(JFrame frame, String title) {
super(frame, true);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle(title);

add(createMainPanel(frame));
pack();
setLocationRelativeTo(frame);
setVisible(true);
}

private JPanel createMainPanel(JFrame frame) {
JPanel panel = new JPanel(new BorderLayout());

JPanel displayPanel = createDisplayPanel();
JScrollPane scrollPane = new JScrollPane(displayPanel);
panel.add(scrollPane, BorderLayout.CENTER);

Dimension d = frame.getSize();
Dimension p = displayPanel.getPreferredSize();
panel.setPreferredSize(new Dimension(p.width + 50, d.height / 2));

return panel;
}

private JPanel createDisplayPanel() {
JPanel panel = new JPanel(new GridLayout(0, 1, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

for (int i = 0; i < 6; i++) {
panel.add(createPendenciesPanel());
}

return panel;
}

private JPanel createPendenciesPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(Color.LIGHT_GRAY);
panel.setPreferredSize(new Dimension(100, 200));

return panel;
}

}

}

How can I set the MaximumSize on a JDialog?

Try this:

addComponentListener(new java.awt.event.ComponentAdapter()
{
public void componentResized(ComponentEvent event)
{
setSize(100,100);
}
});

Swing: set a fixed window size for JDialog

Don't forget to call pack() on the dialog before setVisible(true).

Also, many here will recommend that rather than setting the dialog's preferredSize, overriding it or its content pane's getPreferredSize() method as a cleaner and safer way of having it size itself correctly.


Edit
Holger posted:

Overriding getPreferredSize() is not better if it is just used to return the same constant size.

I think that we all agree that having getPreferredSize() return a dumb constant size is usually to be avoided (although it is stable). Most of the time I avoid setting size/preferredSize or overriding getPreferredSize, but rather try to have my components and their own preferred sizes as well as the layout managers all size themselves, which again is initiated by the pack() method. Occasionally I will need to take a more active roll in trying to set the size of a component such as if I need to size a component to a background image, and then I'll override getPreferredSize() and use the image dimensions for the dimension returned, or will use some type of program logic to help figure out the best size. But again we all can agree that the less you futz with this, the better.


Edit 2
Holger posted:

... but the question is about a JDialog and dialogs never adapt themselves to their preferred size automatically.

Again, they and all top-level windows do when you call pack() on them. Which was why I recommend this, and why this was my primary answer to his question.

Setting the maximum size of a JDialog?

Make the panel imlement Scrollable is the way to go. See also (in addition to the tutorial link Trashgod already provided) Rob's blog entry

http://tips4java.wordpress.com/2009/12/20/scrollable-panel/

then:

1) implement getPreferredScrollableViewportSize() in terms reasonable for the typical content (for a JList f.i. that's the preferred number of rows to show, aka: visibleRowCount)

2) implement setters/getters for those "reasonable terms"

That additional layer (coordiates "reasonable terms") allows all collaborating components to do their best to come up with robust size hints without unfriendly interference as setXXSize (which is a no-no-never-ever, simply forget those methods exist ;)

JDialog not setting preferred size correctly?

The problem is happening because the preferred size of a dialog includes the title bar of the window, as well as any other window decorations. However, the location within the window is set from the upper left corner of the content pane. Because of that difference, most window managers will have your button end up a little too far right, and a lot too far down.

The correct solution is to use actual layout managers, which will correctly position your components. Here's an example which puts the button where you want:

setLayout(new BorderLayout());
JPanel bottom = new JPanel();
bottom.setLayout(new BorderLayout());
bottom.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
bottom.add(new JPanel(), BorderLayout.CENTER);
bottom.add(cancel, BorderLayout.EAST);
add(new JPanel(), BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);

If you're insistent on using a null layout, you can change your code to use getContentPane().setPreferredSize(new Dimension(300, 200)) instead, which will let your frame add the necessary space for the window decorations. However, I would strongly advise you not to do this. Using a null layout is considered a bad practice.

Java - set a size to JDialog

In the second class there's no call to pack() method and that's why the dialog remains "small":

public void pack()

Causes this Window to be sized to fit the preferred size and layouts
of its subcomponents. The resulting width and height of the window are
automatically enlarged if either of dimensions is less than the
minimum size as specified by the previous call to the setMinimumSize
method.

If the window and/or its owner are not displayable yet, both of them
are made displayable before calculating the preferred size. The Window
is validated after its size is being calculated.

You should also take a look to this topic: Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Swing JDialog width too wide

testDialog = new TestDialog(SwingUtilities.windowForComponent(jframe));

You don't need to use the windowForComponent(...) method. You already have a reference to the parent frame:

testDialog = new TestDialog( jframe );

Don't attempt to hard code sizes. Each component will determine its own preferred size and the then layout manager will use this information to determine the overall size.

//jdialog.setPreferredSize(new Dimension(100, 0));

A JMenuItem was designed to be used on a JMenu. Instead you should be using a JButton to add to a regular panel.

I don't have a problem with the size. The width/height of the dialog is as expected.

Note I use JDK 11 on Windows 10. When I ran your original code, the dialog had no size since the setPreferredSize() statement caused the height to be 0. So I'm not sure how you get the display shown in your image.

And yes the dialog width is wider than the component added to the frame because there is a minimum width to the dialog to be able to be able to display the title bar even if no components are added to the dialog.

Cannot set maximum height of JEditorPane inside JScrollPane inside JDialog

Your dialog is going to be Modal so make sure you set the dialog to always be on top of other windows:

dialog.setAlwaysOnTop(true);

Display the dialog realative to something like a specific Window or component. If you want the dialog to display in the center of the monitor screen then make it display relative to null:

........
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);

You can get away with setting the preferred size of things at the final component level in this particular case however you will eventually find later on that this can lead to some undesirable results. Set the dialog to the size you want then let pack() take care of the rest:

dialog.setPreferredSize(new java.awt.Dimension(500, 400));

Set the dimensions to something more realistic as @camickr has already mentioned in comments. A height setting of 1500 is a little much but I'm sure this was a typo and your actual intention was 500, 500.

If you are going to have a title bar for you dialog (which most do) then it's always nice to have an actual title in it so as to denote what the display content is about:

dialog.setTitle("Would be nice to have a title here... - longHtmlString");

Set your Scroll Bar Pane's horizontal and vertical policies so that the component knows what is expected:

sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

With the above setting, either of the scroll bars will be displayed IF they are needed.

Sometimes it's just nice to be able to resize a window a little bit to to catch that wee bit of data that went beyond limits instead of working a scroll bar all the time. You might want to consider removing the dialog.setResizable(false); line of code so that the dialog can indeed be resized if desired.

So that the text material placed into the JEditorPane starts its display from the top of the document instead of the bottom of the document, you may want to consider moving the caret location to index location 0. Yes, this will work even if the Editor Pane is set to non-editable:

ep.setCaretPosition(0);

The following should work fine:

// The JDialog Window...
JDialog dialog = new JDialog();
dialog.setTitle("Would be nice to have a title here... - longHtmlString");
dialog.setAlwaysOnTop(true);
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.setPreferredSize(new java.awt.Dimension(500, 400));
dialog.setModal(true);
//dialog.setResizable(false);

// The JEditorPane...
JEditorPane ep = new JEditorPane();
ep.setContentType("text/html");
ep.setEditable(false);
ep.setText(longHtmlString);
ep.setCaretPosition(0);

// The JScrollPane...
JScrollPane sp = new JScrollPane(ep); // Declared JEditorPane added here.
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

// Add components to dialog, pack it, and display the dialog.
dialog.getContentPane().add(sp);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);


Related Topics



Leave a reply



Submit