Reusing a panel to store values

I need to reuse the textfield in the panel to store values.

Refer to the long but very simple code.

Basically what I am doing is

  1. Creating a panel containing the textfield
  2. Creating an array of object of class Holder that has variable name that is going to store values from the textfield

class Holder
{
    String name;
}


public class Yummy12 extends Holder {
    int t;

    public static void main(String args[])
    {
        new Yummy12();
    }

    Holder[] obj=new Holder[5];

    JButton button1=new JButton("add another one");
    JButton button2=new JButton("exit");
    JPanel panel=new JPanel();
    JTextField textfield=new JTextField("Enter some text here");
    JLabel label1=new JLabel();
    JFrame frame=new JFrame();

    Yummy12()
    {
        for(int i=0;i<5;i++)
        {
            obj[i]=new Holder();
        }

        //set component bounds (only needed by Absolute Positioning)
        label1.setBounds (165, 75, 100, 25);
        textfield.setBounds (350, 75, 100, 25);
        button1.setBounds (170, 230, 100, 25);
        button2.setBounds (360, 230, 100, 25);

        panel.add(label1);
        panel.add(textfield);
        panel.add(button1);
        panel.add(button2);

        frame.add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

        ListenForButton1 lrbutton1=new ListenForButton1();
        ListenForButton1 lrbutton2=new ListenForButton1();

        button1.addActionListener((ActionListener)lrbutton1);
        button2.addActionListener((ActionListener)lrbutton2);
    }        

    private class ListenForButton1 implements ActionListener
    {   
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==button1)
            {
                obj[t].name=textfield.getText();
                t++;

                //what code should come here so that i reuse the same panel again??? 

                if(e.getSource()==button2)
                {
                    System.exit(0);
                }
            }
        }
    }

Answers


Your if block needs to be modified like this in order to make it work:

        if(e.getSource()==button1) {
            obj[t].name=textfield.getText();
            t++;

            //what code should come here so that i reuse the same panel again??? 
            textfield.setText(""); // clears the textfield for re-use

        } else if(e.getSource()==button2){
                System.exit(0);
        }

Hope this helps


Need Your Help

How to Change Stroke Color in SVG Image on Hover Event?

javascript svg hover

I've been searching through the site for an answer, and it seems no one has really received a definitive answer for this specific question.

Square brackets next to an object - What's the notation called?

python python-3.x syntax

What's the name for the square brackets syntax in this snippet?