When adding components to a JPanel, is it possible to add a new compoent above the rest of the components, instead of below?

I am trying to create a JPanel that contains a stack of many JPanels.   Each JPanel in this stack is the same size, has some information and a few buttons.  If the display was meant to be static, all I would have to do is use BoxLayout and go one after the other -that would be fine.  However, the problem is that any individual JPanel needs to be able to be deleted from the stack of JPanels, and new JPanels need to be added to the stack of JPanels on top of the the stack, instead of on the bottom.  Therein lies my problem: the layout managers use add() which seems to add to the bottom or right of the other components on the JPanel, instead of on top or to the left.  Furthermore, more complex layouts, such as gridbaglayout, also work from 0,0 outward...and there is no "maximum" possible to the number of JPanels, so I can't just set a max value and go backwards with the gridbaglayout.

Is there anyway around this problem, without actually clearing the JPanel and re-adding all of the components?  (if so, I can just use BoxLayout()).

Thank you for all your help!
ctangentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

InNoCenT_Ch1ldCommented:
to add:
getContentPane().add(panel, index);
validate();

to remove:
getContentPane().remove(index);
repaint();
validate();

* index = 0, means u add the panel at the first place.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InNoCenT_Ch1ldCommented:
try to run the following code (a bit messy). pay attention to add/remove actionlisteners.
to add, the text entered in the textfield will become the name on the button for indication purpose.
to remove, enter index of which button to be removed..




import javax.swing.JButton;

public class NewJFrame2 extends javax.swing.JFrame {
    public int index = 0;
   
    /** Creates new form NewJFrame2 */
    public NewJFrame2() {
        initComponents();
    }
   

    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        contentPane = new javax.swing.JPanel();
        buttonPanel = new javax.swing.JPanel();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        indexField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        contentPane.setLayout(new javax.swing.BoxLayout(contentPane, javax.swing.BoxLayout.X_AXIS));

        contentPane.setBorder(new javax.swing.border.EtchedBorder());
        getContentPane().add(contentPane, java.awt.BorderLayout.CENTER);

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        buttonPanel.add(addButton);

        removeButton.setText("Remove");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        buttonPanel.add(removeButton);

        indexField.setColumns(10);
        buttonPanel.add(indexField);

        getContentPane().add(buttonPanel, java.awt.BorderLayout.SOUTH);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);
    }
    // </editor-fold>

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
        int v = Integer.parseInt(indexField.getText());
        if(contentPane.getComponentCount() <= v){
            System.out.println(contentPane.getComponentCount() + "");
            return;
        }
        else{
            System.out.println(contentPane.getComponentCount() + "");
            contentPane.remove(v);
        }
        repaint();
        validate();
    }

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
        contentPane.add(new JButton(indexField.getText()), 0);

        validate();
        index++;
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame2().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify
    private javax.swing.JButton addButton;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel contentPane;
    private javax.swing.JTextField indexField;
    private javax.swing.JButton removeButton;
    // End of variables declaration
   
}
InNoCenT_Ch1ldCommented:
hope this helps ;)
ctangentAuthor Commented:
Thank you so much, the example was excellent, and everything works and makes sense!
InNoCenT_Ch1ldCommented:
tks ;)

glad i could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.