-2

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

}
O M
  • 55
  • 6
  • for better help sooner post an SSCCE/MCVE, short, runnable, compilable, just about a.m. issue – mKorbel Nov 26 '14 at 09:39
  • 1
    The use of the null layout would be my first concern – MadProgrammer Nov 26 '14 at 09:50
  • You're not actually making new components, you are just re-adding the existing components, essentially or you're doing is moving the existing components – MadProgrammer Nov 26 '14 at 09:52
  • @MadProgrammer How do I go about doing so, without erasing the existing/original components and adding the new ones? I have to use hard code xy locations. – O M Nov 26 '14 at 10:07
  • Make new instances of the components you want to add – MadProgrammer Nov 26 '14 at 10:25
  • You might use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556) (if I understand your question correctly, which I'm not sure I do). – Andrew Thompson Nov 26 '14 at 10:44

1 Answers1

0

So I run your code and I change a little bit the code structure and I put comment on important points :

public class Main {

    private JFrame jf;
    private Panel p;
    private JButton button;
    private Object options[];
    private String[] tools = {"value1","value2"}; // here put the different values you need in your combobox
    //ActionListener Variables
    private int string1 = 170;
    private int string2 = 170;
    private int yJtf1 = 80;
    private int yJtf2 = 105;
    private int cb1y = 83;
    private int cb2y = 108;
    private int count = 0;
    private List<JComboBox<String>> cbs = new ArrayList<JComboBox<String>>(); //Keep References to your comboBoxs
    private List<JTextField> tfs = new ArrayList<JTextField>(); //Keep References to your TextFields

    public Main() {
        initView();
    }

    //Method to create the view
    public void initView(){
        jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(700, 700);
        p = new Panel();
        JTextField thingsTextField = new JTextField("", 20);
        thingsTextField.setBounds(75, 30, 135, 25);
        JTextField priceTextField = new JTextField("", 20);
        priceTextField.setBounds(75, 60, 135, 25);
        p.add(thingsTextField);
        p.add(priceTextField);
        addSection();
        button = new JButton("+");
        button.setBounds(350, 153, 41, 25);
        button.addActionListener(new ButtonClicked());
        p.add(button);
        jf.add(p);
        jf.setVisible(true);

        //You have to call those methods to redraw the panel
        p.revalidate();
        p.repaint();
    }
    public static void main(String[] args) {
        new Main();
    }

    //This method is called to add a section with the "Plus" Button
    public void addSection(){
        if (count < 3) {
            string1 += 60;
            string2 += 60;
            yJtf1 += 60;
            yJtf2 += 60;
            cb1y += 60;
            cb2y += 59;
            JTextField jtf1 = new JTextField("", 20);
            jtf1.setBounds(40, yJtf1, 40, 25);

            JTextField jtf2 = new JTextField("", 20);
            jtf2.setBounds(40, yJtf2, 40, 25);
            tfs.add(jtf1);
            tfs.add(jtf2);
            p.add(jtf1);
            p.add(jtf2);

            JComboBox<String> cb1 = new JComboBox<>(tools);
            cb1.setBounds(80, cb1y, 80, 20);
            JComboBox<String> cb2 = new JComboBox<>(tools);
            cb2.setBounds(80, cb2y, 80, 20);
            p.add(cb1);
            p.add(cb2);
            cbs.add(cb1);
            cbs.add(cb2);
            p.revalidate();
            p.repaint();
            //Font font = new Font("TimesRoman", Font.BOLD, 18);
            //g.setFont(font);


        }
        count++;
    }
    class ButtonClicked implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            addSection();
        }


    }

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

        }
    }

}
Pintouch
  • 2,630
  • 1
  • 15
  • 22
  • I do actually would like to add additional components on top of the existing/original components. How do I go about doing so, without erasing the existing/original components and adding the new ones? – O M Nov 26 '14 at 10:06
  • You have to create new objects. If you want to add an infinite number of elements each time you click the button, I recommend you to create a list of elements in your class. I will edit my answer with this solution – Pintouch Nov 26 '14 at 10:25
  • I edited the question on my iPhone so I'm not sure it's compile, but the idea is here. :) – Pintouch Nov 26 '14 at 11:01
  • Thank you but I am getting an error for the line JComboBox jtf1 = new JComboBox(tools);. What could be wrong? The error is: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.DefaultComboBoxModel.(Unknown Source) at javax.swing.JComboBox.(Unknown Source) – O M Nov 26 '14 at 20:35
  • Please help if you can – O M Nov 26 '14 at 22:25
  • What is "tools" variable, is it declared and instanciated in your code? I think that variable is null. – Pintouch Nov 26 '14 at 22:35
  • Yes, sorry forgot to put it in. It is private Object tools[]; . Is that a problem? – O M Nov 27 '14 at 07:51
  • I update the code, it should do what you want, please read the comments, and try reading books or tutorials to understand what you're doing! ;) – Pintouch Nov 27 '14 at 09:11
  • Thank you! The adding section works now but there is one more thing I would like to implement. This time around I would like to delete the section that was just added. I used the same logic but for some reason, it deletes the very first section added rather than the section that was added just now. What may be the problem? I showed the code. Thank you again! – O M Nov 27 '14 at 21:40
  • ^^ It actually doesn't delete anything any section at all. I can't seem to figure out why .remove isn't working – O M Nov 27 '14 at 22:55
  • When you remove, you re creating 2 new objects, you don't want that! You have a reference in all your elements with the 2 lists that I created in the class, so just remove objects from those lists to remove a section. ;) – Pintouch Nov 28 '14 at 06:46
  • So just removing the object from the ArrayList removes it off the JPanel? – O M Dec 02 '14 at 00:23
  • I attempted just by .remove() of the most recent index from the ArrayList but it does not remove it from the JPanel. – O M Dec 02 '14 at 01:43
  • You have to remove the elements that you want to remove from their container( panel p)! You should mark this answer as solved, and create a new question, because it's out of topic. – Pintouch Dec 02 '14 at 06:54
  • Thank you so much!!! They appear perfectly fine now. But when I now try to get text out of the JTextFields and options from JComboBoxes that were added from ActionListener, JTextFields get empty strings and JComboBoxes throw nullpointexception. What may be the problem for this? Attempted with .getText(); for the text JTextField and .getSelectedItem(); for the JComboBoxes. – O M Dec 06 '14 at 23:37
  • What do you think? Spent several hours but still don't seem to find the error. Please help – O M Dec 07 '14 at 09:42