1

I have a JFrame containing a JPanel.The JPanel contains a JComboBox,JTextField,add button and a remove button.If i click the add button i need to add a row contaning the above components(that is,a JComboBox,JTextField,add button and a delete button) and disable the previous add button.If i click the add button in the newly formed row the same needs to happen.I have done the these.Now if i click any of the remove button i need to remove the swing components from that row in the JPanel and similarly for other remove buttons.How to do that?Please help me. Below is my code

public class SaveIt extends JFrame {

    JPanel panel;
    JButton btnAdd;
    JButton btnRemove;
    JTextField txtAmount;
    JComboBox cmbAmount;
    private int f = 0;
    private int h = 0;

    public SaveIt() {
        super("Add component on JFrame at runtime");
        setLayout(new BorderLayout());
        panAmount = new JPanel();
        panAmount.setLayout(new FlowLayout());
        add(panAmount, BorderLayout.CENTER);
        cmbAmount = new JComboBox();
        add(cmbAmount, BorderLayout);
        txtAmount = new JTextField();
        add(txtAmount, BorderLayout);
        btnAdd = new JButton("Add");
        add(btnAdd, BorderLayout.SOUTH);
        btnAdd.addActionListener(this);
        btnRemove = new JButton("Remove");
        add(btnRemove, BorderLayout.SOUTH);
        btnRemove.addActionListener(this);
        cps = new ArrayList<JComponent>();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500, 500);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent evt) {
        getPanComponents();
    }

    public void getPanComponents() {
        btnAdd.setEnabled(false);
        btnRemove.setVisible(true);

        cmbAmount = new JComboBox();
        cmbAmount.setBounds(80, 50 + f, 115, 28);
        txtAmount = new JTextField();
        txtAmount.setBounds(310, 50 + f, 135, 28);

        btnAdd = new JButton("Add");
        btnAdd.setBounds(463, 50 + f, 41, 29);
        btnRemove = new JButton("Remove");
        btnRemove.setBounds(510, 50 + f, 41, 29);
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {

                getPanComponents();
            }
        });
        btnRemove.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
            }
        });
        cps.add(cmbAmount);
        cps.add(txtAmount);
        cps.add(btnAdd);
        cps.add(btnRemove);
        for (JComponent widget : cps) {
        panAmount.add(widget);
        }
        panAmount.revalidate();
        h = h + 40;
        panAmount.repaint();
        panAmount.setPreferredSize(new Dimension(611, 89 + h));
        f = f + 35;
    }

    public static void main(String[] args) {
        SaveIt acojfar = new SaveIt();
    }
}

1 Answers1

3
  1. there no reason to use NullLayout, use GridLayout with one column

  2. use JPanel as container for JComponents

  3. add JPanel contains another JPanels to the JScrollPane

  4. don't to set for PreferredSize, nor for revalidate() and repaint() is called

  5. SaveIt acojfar = new SaveIt(); should be wrapped into invokeLater, more to read in Oracle tutorial Initial Thread

  6. for example

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Actually i have a root panel and inside that i add a JScrollPane to it and i placed the panAmount panel in that JScrollPane.But i didn't specified these in my codes above for simplicity –  Apr 29 '14 at 10:27
  • @user3231725 Why are you adding the scroll pane to the root pane ? – MadProgrammer Apr 29 '14 at 10:42
  • Actually i dont want to add a new panel to the panel like in the suggested example.I need to add a new row of components below the existing row of components in the JPanel.Already i have done that successfully.Now i neeed to make the remove button functioning.How to do that? –  Apr 29 '14 at 10:43
  • @MadProgrammer Not to the root pane,its a panel(that is a parent panel and that contains other components also).Don't need to bother abount the parent panel here. –  Apr 29 '14 at 10:45
  • @user3231725 (Actually i dont want to add a new panel to the panel like in the suggested example) then how do you to remove JComponents form 2nd. row, without crazy algoritm, harcoded logics, don't to complicate simple things, every row (a few JComponents) will has only one parent - JPanel, looping inside containers - JPanels is direct and easiest – mKorbel Apr 29 '14 at 11:02
  • each row contains the remove button to remove that row. –  Apr 29 '14 at 11:12
  • e.getParent() or (more than) safer two-three methods from SwingUtilities can to remove whole row (if is used JPanel), because moving with ZOrder (indexing in LayoutManager) is again easier for JPanels – mKorbel Apr 29 '14 at 11:14
  • can be better by using JTable, I'd have search for – mKorbel Apr 29 '14 at 11:18
  • for JTable I can, but I'm never (too lazy) bothered with ZOrder – mKorbel Apr 29 '14 at 11:19
  • Sorry,its difficult me to change the entire code that i have done so far and including a jtable in it.... –  Apr 29 '14 at 11:23
  • Thank you..But its not suitable in my case.I think it is possible by using java component array or something like that –  Apr 29 '14 at 11:28
  • again (for bunch of JComponents, without using JPanel as container for every rows) you lost in this array, because is/must be two dimensional, two steps back – mKorbel Apr 29 '14 at 11:31
  • for your original idea to use set/getClientProperty – mKorbel Apr 29 '14 at 11:32
  • i have made some changes in my code code by includind an arraylist.Please go through that –  Apr 29 '14 at 12:11
  • Now it may easy to remove row of components from the panel on remove button click.Can anybody can help? –  Apr 29 '14 at 12:15