Ryan Ryan - 4 months ago 24
Java Question

JDialog not setting preferred size correctly?

I really don't understand what is happening here. I have a program that I would like to use a JDialog in, but I wanted a separate class for it so I extended the class for a JDialog, went to set the preferred size of it, but it doesn't appear to be setting it correctly. I make a button to "cancel" the operation the JDialog will do that is supposed to be in the bottom right of the JDialog, but it doesn't go there. It instead is outside of it, and only shows up once I resize the JDialog.

I don't have the slightest clue what's going on, or if I'm missing something stupid, but any help is appreciated.

Here is the code:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class NewPlatformDialog extends JDialog implements ActionListener {
private LevelEditor le;

private JButton cancel = new JButton("cancel");

public NewPlatformDialog(Frame owner, String title, LevelEditor l) {
super(owner, title, Dialog.ModalityType.APPLICATION_MODAL);
le = l;
setLayout(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setPreferredSize(new Dimension(300, 200));

add(cancel);
setLocation((owner.getLocationOnScreen().x + (owner.getLocationOnScreen().x + owner.getWidth()))/2 - getPreferredSize().width/2
, (owner.getLocation().y + (owner.getLocationOnScreen().y + owner.getHeight()))/2 - getPreferredSize().height/2);

cancel.setBounds(getPreferredSize().width - cancel.getPreferredSize().width - 10,
getPreferredSize().height - cancel.getPreferredSize().height - 10,
cancel.getPreferredSize().width, cancel.getPreferredSize().height);
cancel.addActionListener(this);
cancel.setFocusable(false);

pack();
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == cancel) dispose();
}
}


Sorry it is a little messy, it gets a little screwed up when I copy paste it.

UPDATED CODE WITH LAYOUTS

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class NewPlatformDialog extends JDialog implements ActionListener {
private LevelEditor le;
private String[] options = {
"Standard Platform",
"Boost Platform",
"Moving Platform",
"Death Platform"
};
private JComboBox<String> platformSelector = new JComboBox<String>(options);

private JButton cancel = new JButton("cancel");

CardLayout cl = new CardLayout();

private JPanel typeSelect = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel panelContainer = new JPanel();
private JPanel standardPlatform = new JPanel();
private JPanel boostPlatform = new JPanel();
private JPanel movingPlatform = new JPanel();
private JPanel deathPlatform = new JPanel();
private JPanel buttonPanel = new JPanel();

public NewPlatformDialog(Frame owner, String title, LevelEditor l) {
super(owner, title, Dialog.ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setResizable(false);

cl.setVgap(10);
panelContainer.setLayout(cl);
le = l;

platformSelector.addActionListener(this);
platformSelector.setFocusable(false);
platformSelector.setSize(100, 70);
platformSelector.setFont(new Font("", Font.BOLD, 12));
platformSelector.setMaximumSize(new Dimension(200, platformSelector.getPreferredSize().height));
platformSelector.setMinimumSize(new Dimension(200, platformSelector.getPreferredSize().height));

add(typeSelect);
add(panelContainer);
add(buttonPanel);

panelContainer.add(standardPlatform, "1");
panelContainer.add(boostPlatform, "2");
panelContainer.add(movingPlatform, "3");
panelContainer.add(deathPlatform, "4");
panelContainer.setMaximumSize(new Dimension(500, 500));
panelContainer.setMinimumSize(new Dimension(20, 20));

createTypeSelect();
createStandardPlatformPanel();
createButtonPanel();

cl.show(panelContainer, "1");

pack();
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == cancel) dispose();
else if(source == platformSelector) {
if(platformSelector.getSelectedIndex() == 0) {
cl.show(panelContainer, "1");
createStandardPlatformPanel();
}
else if(platformSelector.getSelectedIndex() == 1) {
cl.show(panelContainer, "2");
createBoostPlatformPanel();
}
else if(platformSelector.getSelectedIndex() == 2) {
cl.show(panelContainer, "3");
createMovingPlatformPanel();
}
else if(platformSelector.getSelectedIndex() == 3) {
cl.show(panelContainer, "4");
createDeathPlatformPanel();
}
}
}

private void createTypeSelect() {
typeSelect.add(new JLabel("Platform Type: ")).setFont(new Font("", Font.PLAIN, 14));
typeSelect.add(platformSelector);
}

private void createStandardPlatformPanel() {
panelContainer.setPreferredSize(new Dimension(200, 200));
standardPlatform.setBackground(Color.BLUE);
pack();
}

private void createBoostPlatformPanel() {
panelContainer.setPreferredSize(new Dimension(500, 500));
boostPlatform.setBackground(Color.RED);
pack();
}

private void createMovingPlatformPanel() {
panelContainer.setPreferredSize(new Dimension(50, 50));
movingPlatform.setBackground(Color.YELLOW);
pack();
}

private void createDeathPlatformPanel() {
panelContainer.setPreferredSize(new Dimension(100, 100));
deathPlatform.setBackground(Color.GRAY);
pack();
}

private void createButtonPanel() {
cancel.addActionListener(this);
cancel.setFocusable(false);
cancel.setAlignmentX(Box.RIGHT_ALIGNMENT);
buttonPanel.add(cancel);
}

/*
*
add(cancel);
setLocation((owner.getLocationOnScreen().x + (owner.getLocationOnScreen().x + owner.getWidth()))/2 - getPreferredSize().width/2
, (owner.getLocation().y + (owner.getLocationOnScreen().y + owner.getHeight()))/2 - getPreferredSize().height/2);

cancel.setBounds(getPreferredSize().width - cancel.getPreferredSize().width - 10,
getPreferredSize().height - cancel.getPreferredSize().height - 10,
cancel.getPreferredSize().width, cancel.getPreferredSize().height);
cancel.addActionListener(this);
cancel.setFocusable(false);
*/
}


Sorry it's significantly more

Answer

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.

Comments