1

I am working on an Invoice System and I want to create new fields each time the add new button is clicked.

It needs to add the fields in the code below each time.

The fields need to appear under its respective columns.

    JPanel panel_2 = new JPanel();
    panel_2.setBounds(10, 256, 990, 303);
    panel.add(panel_2);
    panel_2.setLayout(null);

    code = new JTextField();
    code.setBounds(10, 11, 86, 20);
    panel_2.add(code);
    code.setColumns(10);
    code.setEditable(false);

    desc = new JTextField();
    desc.setBounds(106, 11, 345, 20);
    panel_2.add(desc);
    desc.setColumns(10);
    desc.setEditable(false);

    quantity = new JTextField("0");
    quantity.setBounds(461, 11, 86, 20);
    panel_2.add(quantity);
    quantity.setColumns(10);
    quantity.setEditable(false);

    price = new JTextField("0");
    price.setBounds(557, 11, 106, 20);
    panel_2.add(price);
    price.setColumns(10);
    price.setEditable(false);

    individualTotal = new JTextField();
    individualTotal.setBounds(673, 11, 106, 20);
    panel_2.add(individualTotal);
    individualTotal.setColumns(10);
    individualTotal.setEditable(false);

Below is my buttons that I have set up:

    JButton newEntry = new JButton("+");
    newEntry.setBackground(Color.PINK);
    newEntry.setForeground(Color.BLUE);
    newEntry.setFont(new Font("Tahoma", Font.BOLD, 15));
    newEntry.setBounds(10, 204, 57, 20);
    panel.add(newEntry);
    newEntry.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            code.setEditable(true);
            desc.setEditable(true);
            quantity.setEditable(true);
            price.setEditable(true);
            individualTotal.setEditable(true);
          } 
    });
    newEntry.setEnabled(false);

    JButton minusEntry = new JButton("-");
    minusEntry.setBackground(Color.PINK);
    minusEntry.setForeground(Color.RED);
    minusEntry.setFont(new Font("Wide Latin", Font.BOLD, 16));
    minusEntry.setBounds(77, 205, 57, 20);
    panel.add(minusEntry);
    minusEntry.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            code.setEditable(false);
            desc.setEditable(false);
            quantity.setEditable(false);
            price.setEditable(false);
            individualTotal.setEditable(false);
        } 
    });
    minusEntry.setEnabled(false);

I know there must be an answer somewhere on this site but I cannot seem to find it.

Please also note that I am new at Java development

  • Possible duplicate of [Creating JButtons dynamically](https://stackoverflow.com/questions/18612791/creating-jbuttons-dynamically) – Basil Battikhi Mar 29 '18 at 16:59
  • Possible duplicate of [How to add JTextField on a JButton just by clicking?](https://stackoverflow.com/questions/23311314/how-to-add-jtextfield-on-a-jbutton-just-by-clicking) – Vikram Palakurthi Mar 29 '18 at 17:00

1 Answers1

0

Create your own subclass of a JPanel which represents one "datasheet" or tablecell or what ever

public DataPanel extends JPanel{


   private JTextField field1 = new JTextField();
   private JTextField field2 = new JTextField();
   // ..... and so on

   public DataPanel(YourDataObject data){
     field1.setText(data.getValue1());
     field2.setText(data.getValue2());
     // ... and so on
     // then add all of your text fields to the panel
     add(field1);
     add(field2);
     // .... and so on
   }
}

Then on button click you add the panel to the component you want to show it on

onClick(SomeEvent event){
  yourComponent.add(new DataPanel(yourDataObject));
}
Basti
  • 1,117
  • 12
  • 32