This site requires JavaScript, please enable it in your browser!
Greenfoot back
Yehuda
Yehuda wrote ...

2017/4/28

anchor

Yehuda Yehuda

2017/4/28

#
I want my Component to be in the top-left corner of the panel but it doesn't budge. Here is the code I have for adding in the backButton:
        GridBagConstraints gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 0;
        gridConstraints.anchor = GridBagConstraints.NORTHWEST;
        worldPanel.add(backButton, gridConstraints);
Between the gridx, gridy, and anchor I think it should be in the top-left corner but it stays in the place that it is when I remove line 4.
Yehuda Yehuda

2017/4/30

#
Can somebody please help?
davmac davmac

2017/5/1

#
Can you produce an example we can try without having to fill in the missing code? (also, asking about Swing particulars on a Greenfoot forum might not get you anywhere - perhaps try stackoverflow or another forum).
Yehuda Yehuda

2017/5/1

#
Here's an example:
import greenfoot.World;
import greenfoot.core.WorldHandler;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Example extends World {

    public Example() {
        super(600, 400, 1);
        JPanel worldPanel = WorldHandler.getInstance().getWorldCanvas();
        worldPanel.removeAll();
        try {
            // Set System L&F
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            // handle exception
        }
        worldPanel.setLayout(new GridBagLayout());
        SwingUtilities.updateComponentTreeUI(worldPanel);
        JButton backButton = new JButton("Back");
        GridBagConstraints gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 0;
        gridConstraints.anchor = GridBagConstraints.FIRST_LINE_START; // same as NORTHWEST
        worldPanel.add(backButton, gridConstraints);
        SwingUtilities.updateComponentTreeUI(backButton);
        JPanel emptyPanel = new JPanel();
        emptyPanel.setPreferredSize(new Dimension(100, 150));
        emptyPanel.setBorder(BorderFactory.createTitledBorder("Panel"));
        gridConstraints = new GridBagConstraints();
        gridConstraints.gridx = 1;
        gridConstraints.gridy = 1;
        gridConstraints.anchor = GridBagConstraints.CENTER;
        worldPanel.add(emptyPanel, gridConstraints);
        SwingUtilities.updateComponentTreeUI(emptyPanel);
    }
}
In the above World the anchor doesn't seem to have any effect at all.
davmac davmac

2017/5/3

#
Well, you're using undocumented Greenfoot internal APIs, and you're doing Swing stuff on the wrong thread, neither of which are a particularly good idea. Greenfoot may be doing things to the worldPanel that are interfering with your own code. You should create a new window instead of trying to manipulate the Greenfoot UI. That said, the problem is that your grid is smaller than the component (worldPanel) and you are not specifying how extra space is to be allocated. See the description of weightx and weighty members of GridBagConstraints: Specifies how to distribute extra horizontal space. The grid bag layout manager calculates the weight of a column to be the maximum weightx of all the components in a column. If the resulting layout is smaller horizontally than the area it needs to fill, the extra space is distributed to each column in proportion to its weight. A column that has a weight of zero receives no extra space. If all the weights are zero, all the extra space appears between the grids of the cell and the left and right edges. So you need to set gridConstraints.weightx = 1.0 (for example) and similary for weighty. Also, FWIW, despite the apparent flexibility of GridBagLayout, it's awful to use and in practice most people avoid it. You can often achieve similar effect with less effort using embedded BoxLayouts.
Yehuda Yehuda

2017/5/3

#
Thank you. I should have figured that out after I read "How to Use GridBagLayout" which did speak about having all the components clumped in the middle or setting them to not be, but I got led away when they spoke about the reason for the components being clumped in the middle is because the user might have enlarged the frame, so I wasn't thinking of a JPanel being originally too big.
You need to login to post a reply.