Results 1 to 15 of 15
Thread: Dynamic textfield creation
- 04-09-2012, 11:29 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
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
- 04-09-2012, 11:48 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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( ... ));
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-09-2012, 12:16 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
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
- 04-09-2012, 12:19 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: Dynamic textfield creation
Read the API documentation for the List interface, e.g.
... retrieves the String content of JTextField #5 in the list.Java Code:String content= fields.get(5).getText();
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-09-2012, 12:30 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
Re: Dynamic textfield creation
Thank you very much Jos
I will try it and reply you.
- 04-10-2012, 03:50 PM #6
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
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());
}
}
- 04-10-2012, 04:12 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: Dynamic textfield creation
Do you call the jButton1ActionPerformed( ... ) method somewhere because nothing else will call it ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-10-2012, 08:55 PM #8
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
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
- 04-10-2012, 09:41 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: Dynamic textfield creation
Have you read this tutorial?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-10-2012, 09:52 PM #10
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
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 }
- 04-11-2012, 02:50 PM #11
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
Re: Dynamic textfield creation
any idea?????
- 04-11-2012, 03:12 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Re: Dynamic textfield creation
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-11-2012, 04:31 PM #13
Re: Dynamic textfield creation
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-11-2012, 05:21 PM #14
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
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 .
- 04-11-2012, 05:39 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Dynamic check box creation + Ajax
By Kanna in forum JavaServer Pages (JSP) and JSTLReplies: 10Last Post: 11-10-2010, 09:46 AM -
Dynamic creation of custom filter based on the columns visible in the tableColumnHead
By neha_sinha in forum AWT / SwingReplies: 5Last Post: 07-09-2010, 06:14 AM -
dynamic tree creation in jsp
By rummy in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 01-11-2010, 12:25 PM -
dynamic creation of jpanel
By masa in forum AWT / SwingReplies: 1Last Post: 12-18-2008, 11:36 AM -
TextField Example
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks