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();
}
}