1

In my JFrame, I have in each line two label and one JComboBox ordred as follow:

Label    JComboBox    Label
**Bouton**

The user can add a new line with the same entities. So, I add a bouton and when user submit a new line is created.

    Label    JComboBox    Label
    **Bouton**
    Label    JComboBox    Label

However the position of the bouton keep the same position. How can i change the position of the bouton (at the end of the page) even if a new line is created ?

My code is bellow:

public class Display extends JFrame{

    public Display(){
        super("Test");
        setTitle("Test");
        setSize(800,800);
        setResizable(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents();
    }

    private void initComponents(){
        setLayout(new GridLayout(0, 3));

        JPanel po = new JPanel();
        JLabel label = new JLabel("Resource");
        po.add(label);
        add(po);

        JPanel po2 = new JPanel();
        po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
        add(po2);


        JPanel po3 = new JPanel();
        JLabel label3 = new JLabel("Something");
        po3.add(label3);
        add(po3);


        //new 
        JPanel PanelBouton = new JPanel();
        JButton bouton = new JButton(new AddResourceAction("Add new"));
        add(bouton);
        PanelBouton.add(bouton);
        add(PanelBouton);

        PanelBouton = new JPanel();
        JLabel vide = new JLabel("");
        PanelBouton.add(vide);
        add(PanelBouton);

        PanelBouton = new JPanel();
        vide = new JLabel("");
        PanelBouton.add(vide);
        add(PanelBouton);

    }

class AddResourceAction extends AbstractAction {

        public AddResourceAction(String n){
            super(n);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            JPanel po = new JPanel();
            JLabel label = new JLabel("Resource");
            po.add(label);
            add(po);

            JPanel po2 = new JPanel();
            po2.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
            add(po2);


            JPanel po3 = new JPanel();
            JLabel label3 = new JLabel("Something");
            po3.add(label3);
            add(po3);

            revalidate();

        }
}




    public static void main(String[] args) {
        /*display panel*/
        SwingUtilities.invokeLater(new Runnable() {
            @Override public void run() {
                new Display().setVisible(true);
            }
        });

    }

}
Mehdi
  • 2,160
  • 6
  • 36
  • 53
  • 1
    I found the solution. If some one need solution in the futur of this question. You can just remove the Bouton and recreate a new one after creating the new line. Not forget validate() & repaint() to JFrame. cheers – Mehdi Aug 24 '15 at 09:23
  • That looks like tabular data to me. For that, use a `JTable`. – Andrew Thompson Aug 24 '15 at 09:42

1 Answers1

2

Create a divided panel, that has two containers. One container (A) for dynamic buttons and another container for the button "Add new". Add new components to the A container.

Find code below that illustrates this concept with your situation. use at your own risk :)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Display 
    extends JFrame
{ 
    Box upperBox   = new Box(BoxLayout.X_AXIS);
    Box dynamicBox = new Box(BoxLayout.Y_AXIS);
    Box staticBox  = new Box(BoxLayout.X_AXIS);

    public Display()
    {
        super("Test");
        setTitle("Test");
        setSize(800,800);
        setResizable(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents();
    }

    private void initComponents()
    {
        //This will be the parent panel for other panels.
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        upperBox.add(new JLabel("Resource"));
        upperBox.add(new JComboBox<>(new String[] { "option1", "option2", "option3",}));
        upperBox.add(new JLabel("Something"));

        panel.add(upperBox);

        staticBox.add(new JButton(new AddResourceAction("Add new")));

        panel.add(dynamicBox); //just add this box now, it will be filled later with components
        panel.add(staticBox);  

        add(panel); 
    }

    class AddResourceAction extends AbstractAction 
    {
        public AddResourceAction(String n)
        {
            super(n);
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            Box box = new Box(BoxLayout.X_AXIS);
            box.add(new JLabel("Resource"));
            box.add(new JComboBox<>(
                        new String[] { "option1", "option2", "option3",}));
            box.add(new JLabel("Something"));

            dynamicBox.add(box);

            revalidate(); 
        }
    }

    public static void main(String[] args) 
    {
        /*display panel*/
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override 
            public void run() 
            {
                new Display().setVisible(true);
            }
        });
    }
}