When the user presses an info button on an element in my game, a new JFrame pops up with params about that element. For some reason, the JFrame appears blank until I resize it. I don’t what’s the deal with this but it didn’t used to happen and is annoying. Here’s how I create the frame:
Won’t pack() also resize the window, though? It’s really not useful if you’ve got scroll panes, either, because it uses the preferredSize of its children and so does the scroll pane when it needs to know how to scroll. Anyway I’ll try both out and see how it works. I might just pack() it then call setSize() afterwards.
Both methods worked, although using setSize() after pack() makes it not work again. Thanks for the help.
Well, I never use setSize(w, h), I let LayoutManagers do all the dirty work for me, even if in this case it means I’d simply call setPreferredSize(dim) instead.
Once your GUIs get really complex, you need to conform to the API to keep things predictable and managable, with the least amount of workarounds.
public ParameterWindow(JFrame parent)
{
JTabbedPane pane = new JTabbedPane();
//Perspective panel.
JPanel perspectivePanel = new JPanel();
perspectivePanel.setLayout(new BorderLayout());
JPanel contentPanel1 = new JPanel();
contentPanel1.setLayout(new GridLayout(4,2));
contentPanel1.add(new JLabel("Near Distance")); contentPanel1.add(nearField);
contentPanel1.add(new JLabel("Far Distance")); contentPanel1.add(farField);
contentPanel1.add(new JLabel("")); contentPanel1.add(new JLabel(""));
contentPanel1.add(new JLabel("Near Grid Size")); contentPanel1.add(sizeField);
perspectivePanel.add(contentPanel1,BorderLayout.NORTH);
perspectivePanel.add(new JLabel(""),BorderLayout.CENTER);
//Spawn panel.
JPanel spawnPanel = new JPanel();
spawnPanel.setLayout(new BorderLayout());
JPanel contentPanel2 = new JPanel();
contentPanel2.setLayout(new GridLayout(9,2));
contentPanel2.add(new JLabel("Minimum Spawn Frequency")); contentPanel2.add(minFreqField);
contentPanel2.add(new JLabel("Optimal Spawn Frequency")); contentPanel2.add(optFreqField);
contentPanel2.add(new JLabel("Maximum Spawn Frequency")); contentPanel2.add(maxFreqField);
contentPanel2.add(new JLabel("")); contentPanel2.add(new JLabel(""));
contentPanel2.add(new JLabel("Minimum Spawn Delay")); contentPanel2.add(minDelayField);
contentPanel2.add(new JLabel("Maximum Spawn Delay")); contentPanel2.add(maxDelayField);
contentPanel2.add(new JLabel("")); contentPanel2.add(new JLabel(""));
contentPanel2.add(new JLabel("Prestige Spawn Chance")); contentPanel2.add(prestigeField);
contentPanel2.add(new JLabel("VIP Spawn Chance")); contentPanel2.add(vipField);
spawnPanel.add(contentPanel2,BorderLayout.NORTH);
spawnPanel.add(new JLabel(""),BorderLayout.CENTER);
//Money panel.
JPanel moneyPanel = new JPanel();
moneyPanel.setLayout(new BorderLayout());
JPanel contentPanel3 = new JPanel();
contentPanel3.setLayout(new GridLayout(6,2));
contentPanel3.add(new JLabel("Rent Per Month")); contentPanel3.add(rentField);
contentPanel3.add(new JLabel("Parking Fee")); contentPanel3.add(optimalFeeField);
contentPanel3.add(new JLabel("")); contentPanel3.add(new JLabel(""));
contentPanel3.add(new JLabel("Minimum Tip")); contentPanel3.add(tipRField);
contentPanel3.add(new JLabel("Middle Tip")); contentPanel3.add(tipYField);
contentPanel3.add(new JLabel("Maximum Tip")); contentPanel3.add(tipGField);
moneyPanel.add(contentPanel3,BorderLayout.NORTH);
moneyPanel.add(new JLabel(""),BorderLayout.CENTER);
//Rank panel.
JPanel rankPanel = new JPanel();
rankPanel.setLayout(new BorderLayout());
JPanel contentPanel4 = new JPanel();
contentPanel4.setLayout(new GridLayout(4,2));
contentPanel4.add(new JLabel("A Rank Requirement")); contentPanel4.add(ownerAField);
contentPanel4.add(new JLabel("B Rank Requirement")); contentPanel4.add(ownerBField);
contentPanel4.add(new JLabel("C Rank Requirement")); contentPanel4.add(ownerCField);
contentPanel4.add(new JLabel("D Rank Requirement")); contentPanel4.add(ownerDField);
rankPanel.add(contentPanel4,BorderLayout.NORTH);
rankPanel.add(new JLabel(""),BorderLayout.CENTER);
//Car panel.
JPanel carPanel = new JPanel();
{
carPanel.setLayout(new GridLayout(1,2));
JPanel carLeftPanel = new JPanel();
{
carLeftPanel.setLayout(new BorderLayout());
JPanel carTypesPanel = new JPanel();
{
carTypesPanel.setLayout(new GridLayout(typeButtons.length+1,1));
carTypesPanel.add(new JLabel("Car Types Allowed"));
for (int i = 0; i < typeButtons.length; i++)
{
carTypesPanel.add(typeButtons[i]);
}
}
carLeftPanel.add(carTypesPanel, BorderLayout.NORTH);
carLeftPanel.add(new JLabel(""),BorderLayout.CENTER);
}
JPanel carRightPanel = new JPanel();
{
carRightPanel.setLayout(new BorderLayout());
JPanel soundPanel = new JPanel();
{
soundPanel.setLayout(new GridLayout(2,1));
JPanel honkPanel = new JPanel();
{
honkPanel.setLayout(new GridLayout(honkButtons.length+1,1));
honkPanel.add(new JLabel("Honk Sound"));
for (int i = 0; i < honkButtons.length; i++)
{
honkPanel.add(honkButtons[i]);
}
}
JPanel zoomPanel = new JPanel();
{
zoomPanel.setLayout(new GridLayout(zoomButtons.length+1,1));
zoomPanel.add(new JLabel("Zoom Sound"));
for (int i = 0; i < zoomButtons.length; i++)
{
zoomPanel.add(zoomButtons[i]);
}
}
soundPanel.add(new JScrollPane(honkPanel));
soundPanel.add(new JScrollPane(zoomPanel));
}
JPanel sizePanel = new JPanel();
{
sizePanel.setLayout(new BorderLayout());
JPanel carSizePanel = new JPanel();
{
carSizePanel.setLayout(new GridLayout(1,2));
carSizePanel.add(new JLabel("Size"));
carSizePanel.add(sizeLabel);
}
sizePanel.add(carSizePanel, BorderLayout.NORTH);
sizePanel.add(sizeSlider, BorderLayout.CENTER);
}
carRightPanel.add(soundPanel, BorderLayout.CENTER);
carRightPanel.add(sizePanel, BorderLayout.SOUTH);
}
carPanel.add(new JScrollPane(carLeftPanel));
carPanel.add(carRightPanel);
}
pane.addTab("Perspective", perspectivePanel);
pane.addTab("Spawning", spawnPanel);
pane.addTab("Money", moneyPanel);
pane.addTab("Ranking", rankPanel);
pane.addTab("Cars", carPanel);
getContentPane().add(pane);
//Add an assload of listeners.
nearField.addCaretListener(this);
farField.addCaretListener(this);
sizeField.addCaretListener(this);
minFreqField.addCaretListener(this);
optFreqField.addCaretListener(this);
maxFreqField.addCaretListener(this);
minDelayField.addCaretListener(this);
maxDelayField.addCaretListener(this);
prestigeField.addCaretListener(this);
vipField.addCaretListener(this);
rentField.addCaretListener(this);
optimalFeeField.addCaretListener(this);
tipGField.addCaretListener(this);
tipYField.addCaretListener(this);
tipRField.addCaretListener(this);
ownerAField.addCaretListener(this);
ownerBField.addCaretListener(this);
ownerCField.addCaretListener(this);
ownerDField.addCaretListener(this);
//Add in some more listeners.
for (int i = 0; i < typeButtons.length; i++)
{
typeButtons[i].addItemListener(this);
}
for (int i = 0; i < honkButtons.length; i++)
{
honkButtons[i].addItemListener(this);
}
for (int i = 0; i < zoomButtons.length; i++)
{
zoomButtons[i].addItemListener(this);
}
sizeSlider.addChangeListener(this);
//No cars are selected.
setSelectedCar(-1);
//Finally create and size the window.
setSize(500,375);
setLocation(new Point( (int)parent.getLocation().getX() + parent.getWidth()/2 - getWidth()/2,
(int)parent.getLocation().getY() + parent.getHeight()/2 - getHeight()/2));
setVisible(true);
((JComponent) getContentPane()).revalidate();
repaint();
}
Just to note, that’s a single window that pops up when you press Control-I. There are plenty of other ones, plus there’s the main interface that has all the menus and panels and whatnot to call everything else. And I don’t use pack() at all, nor have I ever really used it before. I prefer getting a lot more control over what I do (like for example I used to use GridBagLayout a lot).
Anyway, I suppose it’s a debate anyone can take. I just prefer to have direct control over what my GUI will look like, plus it seems faster than doing stuff like increasing the preferred size of everything.