1

I have a problem with GroupLayout. My code is:

public chat () {

        JTextField chatbox;
        JTextField msgbox;
        JButton sendbutton;
        GroupLayout layout;

        super();
        chatbox = new JTextField();
        msgbox = new JTextField();
        sendbutton = new JButton("Send");
        layout = new GroupLayout(this);

        sendbutton.setPreferredSize(new Dimension(60, 20));

        setBackground(new Color(255, 255, 255));

        setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(layout.createParallelGroup()
            .addComponent(chatbox)
            .addGroup(layout.createSequentialGroup()
                .addComponent(msgbox)
                .addComponent(sendbutton))
        );

        layout.linkSize(SwingConstants.HORIZONTAL, sendbutton);

        layout.setVerticalGroup(layout.createSequentialGroup()
            .addComponent(chatbox)
            .addGroup(layout.createParallelGroup()
                .addComponent(msgbox)
                .addComponent(sendbutton))
        );
}

And the problem is that components in Layout are as big as they can. I cant limit this size. The question is, how to set for example constant height for an item?

Or from the other hand, which function is called when window is resized? Maby can i override that function, getSize of parent JPanel (its in JPanel), and then set size based on percentage? What do you think?

// problem solved.

in fifth line from bottom change

.addGroup(layout.createParallelGroup()

for

.addGroup(layout.createParallelGroup(BASELINE)

thank you all for help.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
marxin
  • 3,692
  • 3
  • 31
  • 44
  • don't call any of the setXXSize methods, ever - http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi – kleopatra Feb 28 '12 at 11:58
  • and: please learn java naming conventions and stick to them – kleopatra Feb 28 '12 at 12:01
  • 1
    @user1040813: You can add, and even accept, [your own answer](http://meta.stackexchange.com/q/17463/163188). You might cite [*How to Use GroupLayout*](http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html). – trashgod Feb 28 '12 at 12:17

2 Answers2

0

I'm adding this because at first glance the answer isn't quite as clear as it could be, although it did answer my own version of this question quite well :-)

Basically, when creating the vertical group in GroupLayout, if you want the components to be at their preferred height (for the most part), you can add the argument GroupLayout.Alignment.BASELINE.

I'd explain the BASELINE argument, but the Oracle documentation (http://docs.oracle.com/javase/7/docs/api/javax/swing/GroupLayout.ParallelGroup.html) does a much better job:

The baseline is calculated based on the preferred height of each of the elements that have a baseline. The baseline is calculated using the following algorithm: max(maxNonBaselineHeight, maxAscent + maxDescent), where the maxNonBaselineHeight is the maximum height of all elements that do not have a baseline, or are not aligned along the baseline.

As others have stated, setMaximumSize() shouldn't be used and I'd advise checking out the link @kleopatra mentioned for more information.

By the way, the code at the end of the question doesn't appear to be entirely correct (at least when I tried something similar). As far as I can tell, the fifth line from the bottom should be

.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)

rather than

.addGroup(layout.createParallelGroup(BASELINE)
Community
  • 1
  • 1
Drake Sobania
  • 484
  • 3
  • 15
-1

setMaximumSize() in your component objects.

Eric Hydrick
  • 3,467
  • 2
  • 28
  • 41
  • Thank you, problem solved. In fifth line from bottom should be .addGroup(layout.createParallelGroup(BASELINE) not .addGroup(layout.createParallelGroup(). – marxin Feb 27 '12 at 20:17
  • 1
    -1 for setXXSize (yeah, I strive to find them all ;-), see http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi – kleopatra Feb 28 '12 at 11:59