Search code examples
javaswingjpanelnull-layout-manager

JPanel cropping or (.setBounds()) not working


Sometimes the setBounds() works but only in the actionlistener. my code:
-does not work

public panel() {
    initComponents();
    setBounds(100,100,105,105);
    setMaximumSize(new Dimension(100,100));

}

-does work

private void btnBestellingItemToevActionPerformed(java.awt.event.ActionEvent evt) {                                                      
    setBounds(100,100,105,105);
    setMaximumSize(new Dimension(100,100));


} 

-layout manager:

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(btnBestellingItemToev, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtWat, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtTafelNr, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)))
            .addGap(0, 131, Short.MAX_VALUE))
    );

Can anyone help me so it works in the constructor also?


Solution

  • As I said, the problem you're having is the fact that the container the component you are trying to move/size has a layout manager on it.

    The layout manager is responsible for determining the position and size of all the components within in it.

    You have two choices. Write your own layout manager that does what you want it to do. This is reasonably complex process, but is generally a better choice or do without a layout manager.

    Personally, when doing things like this, I paint the result directly to the panel, but that's me...

    BeachBallOfDoom

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class BouncyBall {
    
        public static void main(String[] args) {
            new BouncyBall();
        }
    
        public BouncyBall() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new BallCourt());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class BallCourt extends JPanel {
    
            private JLabel beachBall;
            private int delta = 4;
    
            public BallCourt() {
                setLayout(null);
                beachBall = new JLabel();
                try {
                    beachBall.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/BeachBallOfDoom.png"))));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
    
                beachBall.setBounds(0, 100 - 16, 32, 32);
                add(beachBall);
    
                Timer timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int x = beachBall.getX() + delta;
                        if (x + beachBall.getWidth() > getWidth()) {
                            delta *= -1;
                            x = (getWidth() - beachBall.getWidth()) + delta;
                        } else if (x < 0) {
                            delta *= -1;
                            x = delta;
                        }
    
                        beachBall.setLocation(x, beachBall.getY());
                    }
                });
                timer.start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
        }
    
    }