--------------------------------------------------------EDITED------------------------------------------------------------------
The adding section works nicely now the button!
I added newly implementation with the same logic, but this time around, deleting the section that was just added. I tried it out but the problem is that it doesn't delete the section just added.
Also, I would like to use g.addString when adding section but how do I go about doing so? Thank you so much!
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
private JFrame jf;
private JTextField jtf1;
private JTextField jtf2;
private Panel p;
private JComboBox jcb1;
private JComboBox jcb2;
private JButton button;
private String tools[] = {""};
//ActionListener Variables
private int string1 = 170;
private int string2 = 170;
private int yJtf1 = 140;
private int yJtf2 = 165;
private int cb1 = 143;
private int cb2 = 168;
private int count = 0;
public Main() {
jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(700, 700);
p = new Panel();
jtf1 = new JTextField("", 20);
jtf2 = new JTextField("", 20);
jcb1 = new JComboBox(tools);
jcb2 = new JComboBox(tools);
button = new JButton("+");
p.add(jtf1);
jtf1.setBounds(75, 30, 135, 25);
p.add(jtf2);
jtf2.setBounds(75, 60, 135, 25);
p.add(button);
plusButton.setBounds(350, 153, 41, 25);
button.addActionListener(new ButtonClicked());
p.add(button2);
plusButton.setBounds(350, 153, 41, 25);
minusButton.addActionListener(new NewButtonClicked());
p.add(jcb1);
jcb1.setBounds(80, 143, 80, 20);
p.add(jcb2);
jcb2.setBounds(80, 168, 80, 20);
jf.add(p);
jf.setVisible(true);
`
}
public class Panel extends JPanel {
public Panel() {
this.setLayout(null);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Font font = new Font("Arial", Font.BOLD, 12);
g.setFont(font);
g.drawString("Things:", 27, 45);
g.drawString("Price:", 27, 75);
}
}
public static void main(String[] args) {
new Main();
}
class ButtonClicked implements ActionListener {
public void actionPerformed(ActionEvent ae) {
if (count < 3) {
string1 += 60;
string2 += 60;
jtf1 += 60;
jtf2 += 60;
cb1 += 60;
cb2 += 59;
JTextField jtf1 = new JTextField("", 20);
jtf1.setBounds(40, yJtf1, 40, 25);
p.add(jtf1);
JTextField jtf2 = new JTextField("", 20);
jtf2.setBounds(40, yJtf2, 40, 25);
p.add(jtf2);
JComboBox jcb1 = new JComboBox(tools);
jcb1.setBounds(80, cb1, 80, 20);
p.add(jcb1);
JComboBox jcb2 = new JComboBox(tools);
jcb2.setBounds(80, cb2, 80, 20);
p.add(jcb2);
------------->>>//NEW I WOULD LIKE TO IMPLEMENT (BUT HOW DO I USE 'g' TO ADD TO ANOTHER PANEL?)
Font font = new Font("TimesRoman", Font.BOLD, 12);
g.setFont(font);
g.drawString("Things:", 7, string1);
g.drawString("Price:", 165, string2);
p.revalidate();
p.repaint();
count++;
}
}
}
------------->>>//////NEWLY EDITED IMPLEMENTATION (DELETING SECTION)
class NewButtonClicked implements ActionListener {
public void actionPerformed(ActionEvent ae) {
if (count > 0 && count < 4) {
JTextField jtf1 = new JTextField("", 20);
JTextField jtf2 = new JTextField("", 20);
JComboBox jcb1 = new JComboBox(tools);
JComboBox jcb2 = new JComboBox(tools);
p.remove(jtf1);
p.remove(jtf2);
p.remove(jcb1);
p.remove(jcb2);
jtf1 -= 60;
jtf2 -= 60;
cb1 -= 60;
cb2 -= 59;
p.revalidate();
p.repaint();
count--;
}
}
}
}