Re: Simple BorderLayout problem
On 2/16/2010 12:40 PM, Fencer wrote:
Thanks for that link, I will be sure to bookmark it! Below is my updated
program: It has a "group panel" in the center. The group panel holds two
buttons on the same line with space around them. At the bottom of
frame's content pane I have label acting as a status bar.
I know the code is overly verbose, it's for my learning purposes. I
can't say I have mastered GridBagLayout at all yet, but I can say I will
be using it from now on as much as I can so I do learn it.
One question about my program code, if I want to make the buttons have
the same width, how should I do that? Basically I want the button with
longest text to have its preferred width then increase the width of any
thinner buttons. In my case the button labeled "Open BioModel" should
have its width increased to match that of the button labeled "Load Saved
Session".
GBL can make them the same width really easily if they are vertically
aligned with the fill element. Horizontally like that is a little more
difficult. You have to resort to tricks. Once you know the size of the
bigger one which you only will after the container is sized then you can
set the smaller one to the same size.
I have recreated your code to give you a simpler way of presenting your
application. One note, only one GridBagConstraints object is usually
necessary. Just adjust the fields and add your component with a
reference to the GBC and change the fields as necessary. You will see
in the example below I use only one but for two different components.
Also, you don't need to get at the ContentPane any more before adding
components to a JFrame. The add method has been overridden to add the
components to the ContentPane of the JFrame.
Back to the trick. Use a ComponentListener to check for a component
resized event. A resize event is triggered when the component is first
sized for display. Then set the preferred size of the other component
and call revalidate() which causes the component to be redrawn and
triggers another resized event.
The only other thing I might mention is that you have too many fixed
sizes for panels and frames. Usually one creates the content and then
calls pack() on the frame to size your application. That's not always
possible however but given wide range of display resolutions these days,
it is sometimes better to have fixed sizes. My example below uses your
sizes however.
Oh, and one other thing. Swing (JFrame, JWindow etc) GUI creation must
occur on the Event Dispatch Thread (search Google for EDT). That is
accomplished many ways but the simplest is to use the
EventQueue.invokeLater() method to wrap all GUI creation code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class CG extends JFrame {
public CG() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JPanel p = new JPanel(new GridBagLayout());
p.setPreferredSize(new Dimension(480,100));
p.setBorder(new TitledBorder("Panel Border"));
c.gridx = 0;
c.weightx = 1.0;
final JButton b1 = new JButton("Press Me");
p.add(b1,c);
c.gridx = 1;
final JButton b2 = new JButton("Please Don't Press Me");
p.add(b2,c);
b1.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent ce) {
b1.setPreferredSize(b2.getPreferredSize());
b1.revalidate();
System.out.println(b1.getSize());
System.out.println(b2.getSize());
}
});
c.gridx = c.gridy = 0;
c.weighty = 1.0; // c.weightx is still 1.0
c.anchor = GridBagConstraints.CENTER;
add(p,c);
c.gridy = 1;
c.weighty = 0.1; // the panel above gets most of the weighty
c.fill = GridBagConstraints.HORIZONTAL; // c.weightx is still 1.0
c.anchor = GridBagConstraints.SOUTH;
JLabel l = new JLabel(
"Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah
Blah");
l.setBorder(BorderFactory.createLineBorder(Color.BLUE,10));
add(l,c);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
CG cg = new CG();
cg.setSize(640,480);
cg.setLocationRelativeTo(null);
cg.setVisible(true);
}
});
}
}
--
Knute Johnson
email s/nospam/knute2010/