Macas
Macas

Reputation: 560

Java JPanel moves slightly on its own + extra

I have a problem. Now I'm working with 3 panels, mainPanel and 2 others ( btnPanel and iconPanel). So the problem is when I push button "reset" I delete iconPanel and add it again it moves slightly to right on its own. Maybe someone can check my code where the problem?

Also I dont want to create another question so I give 2 extra questions.

Do I delete JPanel properly? If I delete JPanel with components inside they also will be removed from memory?

P.s. Im beginner so dont judge me :)

Main.java

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main extends JFrame {
    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Made by Mac4s");
                frame.setVisible(true);
                frame.setSize(310, 654);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.setResizable(false);          




                MainScreen screenObj = new MainScreen();  
                screenObj.setPreferredSize(new Dimension(310, 650));
                frame.add(screenObj);
            }
        });
    }
}

MainScreen.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;



public class MainScreen extends JPanel {
    private JButton resetBtn;
    private JPanel btnPanel;
    private JPanel iconPanel;

    public MainScreen() {
        JPanel mainPanel = new JPanel(new BorderLayout());  
        this.setBackground(Color.BLACK);

        setBtnPanelAndComp();
        setIconPanelAndComp();

        add(mainPanel);
    }

    private void setBtnPanelAndComp() {
        btnPanel = new JPanel(new BorderLayout());
        btnPanel.setBackground(Color.GREEN);
        btnPanel.setPreferredSize(new Dimension(295, 30));

        setButtons();

        this.add(btnPanel, BorderLayout.NORTH);
    }

    private void setButtons() {
        resetBtn = new JButton("Reset");
        resetBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                resetIconLabel();

            }               
        });

        btnPanel.add(resetBtn, BorderLayout.WEST);
    }

    private void resetIconLabel() {
        this.remove(iconPanel);
        this.repaint();
        this.revalidate();
        setIconPanelAndComp();

    }

    private void setIconPanelAndComp() {
        iconPanel = new JPanel(new BorderLayout());
        iconPanel.setBackground(Color.YELLOW);
        iconPanel.setPreferredSize(new Dimension(295, 580)); 

        this.add(iconPanel, BorderLayout.SOUTH);
    }
}

Upvotes: 1

Views: 147

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209062

"the problem is when I push button "reset" I delete iconPanel and add it again it moves slightly to right on its own."

The reason this happens is because a JPanel has a FlowLayout by default. You're trying add to a BorderLayout position that is non-existent.

this.add(iconPanel, BorderLayout.SOUTH);

The FlowLayout has default gaps on the edges, so when you set the size of the frame, those gaps aren't respected. To over come this, it is also preferable to pack() the frame, instead of setSize()

The reason BorderLayout works (doesn't shift) is because preferred sizes aren't respected.

If you set the layout in the constructor to this.setLayout(new BorderLayout()); You won't have the shift.

public MainScreen() {
    JPanel mainPanel = new JPanel(new BorderLayout());
    this.setLayout(new BorderLayout());                  <----
    setBtnPanelAndComp();
    setIconPanelAndComp();
    add(mainPanel);
}

Notes

  • You should setVisible() after adding components. That's why your frame jumps when you first open it. You are setting the frame visible, then moving it around with the location, and adding components.

Upvotes: 3

Related Questions