james Verosi
james Verosi

Reputation: 21

How to add button dynamically in panel?

I want to add label dynamically in panel with horizontally and vertically. I tried this code.

GridBagConstraints labelConstraints = new GridBagConstraints();

        // Add buttons
        for(int i = 0; i < indexer; i++)
        {
             // Label constraints
            labelConstraints.gridx = 1;
            labelConstraints.gridy = i;

            // Add them to panel                
            panel.add(listOfLabels.get(i), labelConstraints);
        }

        // Align components top-to-bottom
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = indexer;
        c.weighty = 1;
        panel.add(new JLabel(), c);


        indexer++;
    }

but I need result like this image:

view image

I don't want to change size of JLabel and change label count.

Upvotes: 0

Views: 2345

Answers (2)

Jan Bodnar
Jan Bodnar

Reputation: 11647

Not sure what was meant by adding a label dynamically here.

For me, dynamic addition of a component is adding it at runtime. The following example demonstrates this with the MigLayout manager:

package com.zetcode;

import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

/*
Demonstrating dynamic addition of labels 
with MigLayout manager.

Author: Jan Bodnar
Website: zetcode.com
 */
public class MigLayoutDynamicLabelsEx extends JFrame {

    private int counter = 0;

    public MigLayoutDynamicLabelsEx() {

        initUI();
    }

    private void initUI() {

        MigLayout layout = new MigLayout("wrap 4");
        setLayout(layout);

        JButton addBtn = new JButton("Add");
        addBtn.addActionListener((ActionEvent e) -> {
            JLabel lbl = createLabel();
            add(lbl, "w 100lp, h 30lp");
            pack();
        });

        add(addBtn);
        add(createLabel(), "w 100lp, h 30lp");
        add(createLabel(), "w 100lp, h 30lp");
        add(createLabel(), "w 100lp, h 30lp");

        pack();

        setTitle("MigLayout dynamic example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private JLabel createLabel() {

        counter++;
        String text = "#table no." + counter;

        JLabel lbl = new JLabel(text);
        lbl.setBorder(BorderFactory.createEtchedBorder());

        return lbl;
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> {
            MigLayoutDynamicLabelsEx ex = new MigLayoutDynamicLabelsEx();
            ex.setVisible(true);
        });
    }
}

At the beginning, there are button and three labels. Clicking on the button adds a new label to the layout. The wrap 4 constraint creates 4 columns per row. The window is reorganized with the pack() method.

Screenshot:

Screenshot of the example

Upvotes: 2

Eric
Eric

Reputation: 193

I think that you did not set the gridx and gridy correctly, the below codes hope can solve your issue:

    int rows = 5;
    int columns = 5;
    JLabel label;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints constraints= new GridBagConstraints();

    for(int rowIndex = 0; rowIndex < rows; rowIndex++){
        for(int columnIndex = 0 ; columnIndex < columns; columnIndex++){
            label = new JLabel(String.format("table row:%d, column:%d ", rowIndex+1,columnIndex+1));
            constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.gridx = columnIndex;
            constraints.gridy = rowIndex;
            pane.add(label, constraints);
        }
    }

Upvotes: 1

Related Questions