Results 1 to 15 of 15
  1. #1
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Dynamic textfield creation

    Hello

    I woold like to ask how can i create dynamicly textfields, and how to handle the content of them after the creation????

    i have a variable which says how many textfields i will have.

    int counttextfields =10; // i need 10 textfields

    for(int i=0; i<counttextfields; i++){

    //what i have to put here??????


    }


    Thank you in advanced

    ki_ha1984

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default Re: Dynamic textfield creation

    I assume that you want to keep track of those JTextFields, so a List<JTextField> can do fine; it stores those JTextFields for you so you can do what you want with them:

    Java Code:
    List<JTextField> fields= new ArrayList<JTextField>(); // or a LinkeList<JTextField> ...
    
    for (int i= 0; i < counttedxtfields; i++) // create the text fields ...
       fields.add(new JTextField( ... ));
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: Dynamic textfield creation

    this wil create me 10 textfields.
    When i wont to read the content of the fifth of sixth thextfield how i will callit????
    or if i wont to store all the content of the textfields in an array how i will doit??

    thanl you Jos

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default Re: Dynamic textfield creation

    Read the API documentation for the List interface, e.g.

    Java Code:
    String content= fields.get(5).getText();
    ... retrieves the String content of JTextField #5 in the list.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: Dynamic textfield creation

    Thank you very much Jos

    I will try it and reply you.

  6. #6
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: Dynamic textfield creation

    i tried this one in a JFrane1 created with GUI but it give nothing.
    I want to add them in a JPanell one by clicking in the button JButton1.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    int countme = 10;
    List<JTextField> fields= new ArrayList<JTextField>(); // or a LinkeList<JTextField> ...

    for (int i= 0; i < countme; i++){ // create the text fields ...
    fields.add(new JTextField());
    }
    }

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default Re: Dynamic textfield creation

    Do you call the jButton1ActionPerformed( ... ) method somewhere because nothing else will call it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: Dynamic textfield creation

    why and how to call it .
    I want to create the JTextfields by pressing the Jbutton.
    i mean when i press the Jbutton to load the value of the counttedxtfields from another class and to create the jTextFields with the for() loop, by pressing the jjbutton to appear all the Jtextfields in a jform or better in a jpannel. The JForm an the Jpanel are created by the Netbeans GUI.

    ki_ha1984

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default Re: Dynamic textfield creation

    Quote Originally Posted by ki_ha1984 View Post
    why and how to call it .
    Have you read this tutorial?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: Dynamic textfield creation

    yes my friend i have read it .
    My problem is that i am not in runtime.
    and i want to add the dynamic text fields in the jPanel1 by pressing the jButton1..


    XML Code:
    package javaappdemos;
    
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JTextField;
    
    
    public class Tetstagain extends javax.swing.JFrame {
    
        public Tetstagain() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
            jPanel2 = new javax.swing.JPanel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("jButton1");
            jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    jButton1MouseClicked(evt);
                }
            });
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 188, Short.MAX_VALUE)
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 112, Short.MAX_VALUE)
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(285, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(42, 42, 42))
                .addGroup(layout.createSequentialGroup()
                    .addGap(61, 61, 61)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(26, 26, 26)
                    .addComponent(jButton1)
                    .addGap(59, 59, 59)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(80, Short.MAX_VALUE))
            );
    
            pack();
        }
    
                                             
    
       [SIZE=3] private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
            
                   int countme = 10;               
            List<JTextField> fields= new ArrayList<JTextField>(); // or a LinkeList<JTextField> ...
     
                for (int i= 0; i < countme; i++){ // create the text fields ...
                    fields.add(new JTextField());
                }
        }       [/SIZE]                              
    
        public static void main(String args[]) {
            
       
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(Tetstagain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Tetstagain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Tetstagain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Tetstagain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new Tetstagain().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JPanel jPanel2;
        // End of variables declaration
    }

  11. #11
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: Dynamic textfield creation

    any idea?????

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default Re: Dynamic textfield creation

    Quote Originally Posted by ki_ha1984 View Post
    any idea?????
    Yes, I have: put a few System.out.println( ... ) calls in your event handling method and see if it is actuall called when you press your button; note that the situation you are in now has nothing to do with your original question.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Dynamic textfield creation

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: Dynamic textfield creation

    Why is different my problem still the same i need to create many textfields by pressing a button.

    Sorry about the crosspost .

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default Re: Dynamic textfield creation

    Quote Originally Posted by ki_ha1984 View Post
    Why is different my problem still the same i need to create many textfields by pressing a button.
    Sure, but now it's about a button you can't get to work. Read my previous reply again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Dynamic check box creation + Ajax
    By Kanna in forum JavaServer Pages (JSP) and JSTL
    Replies: 10
    Last Post: 11-10-2010, 09:46 AM
  2. Replies: 5
    Last Post: 07-09-2010, 06:14 AM
  3. dynamic tree creation in jsp
    By rummy in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 01-11-2010, 12:25 PM
  4. dynamic creation of jpanel
    By masa in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 11:36 AM
  5. TextField Example
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •