Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.LineBorder;

public class stext { 
        static JPanel panel = new JPanel();
        static Integer indexer = 1;
        static List<jlabel> listOfLabels = new ArrayList<jlabel>();
        static List<jtextfield> listOfTextFields = new ArrayList<jtextfield>();

        public static void main(String[] args)
        {       
            
            JFrame frame = new JFrame();
            frame.setLayout(new GridBagLayout());
            frame.setPreferredSize(new Dimension(990, 990));
            frame.setTitle("My Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          
            GridBagConstraints frameConstraints = new GridBagConstraints();

            
            JButton addButton = new JButton("Add");
            addButton.addActionListener(new ButtonListener());

           
            frameConstraints.gridx = 0;
            frameConstraints.gridy = 0;
            frame.add(addButton, frameConstraints);

           
            panel.setPreferredSize(new Dimension(600, 600));
            panel.setLayout(new GridBagLayout());
            panel.setBorder(LineBorder.createBlackLineBorder());

           
            frameConstraints.gridx = 0;
            frameConstraints.gridy = 1;
            frameConstraints.weighty = 1;
            frame.add(panel, frameConstraints); 

                        frame.pack();

            
            frame.setVisible(true);
        }

        static class ButtonListener implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent arg0) 
            {       
              panel.removeAll();

               
                JTextField jTextField = new JTextField();
                jTextField.setSize(100, 200);
                listOfTextFields.add(jTextField);
                listOfLabels.add(new JLabel("Name" + indexer));

                
                GridBagConstraints textFieldConstraints = new GridBagConstraints();
                GridBagConstraints labelConstraints = new GridBagConstraints();

               
                for(int i = 0; i < indexer; i++)
                {   textFieldConstraints.gridx = 1;
                    textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
                    textFieldConstraints.weightx = 0.5;
                    textFieldConstraints.insets = new Insets(10, 10, 10, 10);
                    textFieldConstraints.gridy = i;

                    
                    labelConstraints.gridx = 0;
                    labelConstraints.gridy = i;
                    labelConstraints.insets = new Insets(10, 10, 10, 10);

                    panel.add(listOfLabels.get(i), labelConstraints);
                    panel.add(listOfTextFields.get(i), textFieldConstraints);
                }

                GridBagConstraints c = new GridBagConstraints();
                c.gridx = 0;
                c.gridy = indexer;
                c.weighty = 1;
                panel.add(new JLabel(), c);              
                indexer++;
                panel.updateUI();
            }
        }
    
}
Posted
Updated 28-Oct-14 0:37am
v2

1 solution

Where is the database connection?

First things first:
You need to do proper coding:
GET AN IDE!
I can't stress that enough. This is 21. century, not 60s.
Eclipse is free and common used. there is no need to code in some notepad.

https://www.eclipse.org/downloads/[^]

Now let's take a look at the coding:

Java
public class Stext { // classes/ objects ALWAYS starts with capital letter

// get rid of "static" (see main why possible). Instead use "private" to limit access to your class only
        private JPanel panel = new JPanel();
        private Integer indexer = 1; // Eclipse will tell you that this is rubbish false reference: can not assign a int to Integer
        private List<jlabel> listOfLabels = new ArrayList<jlabel>(); // Eclipse will tell you that this is  rubbish false reference "jlabel"
        private List<jtextfield> listOfTextFields = new ArrayList<jtextfield>(); // Eclipse will tell you that this is  rubbish false reference "jtextfield"


        public Stext() // constructor
        {
           this.init();
        { 

        public static void main(String[] args)
        {
             Stext stext = new Stext(); // break out of static context - no static needed any more
        }

        private void init()
        {
            // code from function "main" goes here
        {

        private class ButtonListener implements ActionListener // no "static" either!
        {
            // fancy coding
        }
</jtextfield></jtextfield></jlabel></jlabel>


Please read here for understanding OOP:
http://docs.oracle.com/javase/tutorial/java/javaOO/index.html[^]

Now to your problem:

You have a List<textfield></textfield> in that GUI.

The JTextField is holding the entered text. So to get & work with that String, you should consider to create a variable for each value. As given in your coding a List<string></string> could be fine. Please also consider to use a HashMap instead of List as that gives you key-values for the String values.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900