Click here to Skip to main content
15,867,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am working on swing related project in that i am increase text Field dynamically but i am not able to give unique name to each text field .please give me suggestion how can i use each text Field .getText(); method for text Field or how we can give unique name to the each text field
my code bellow


Java
public class tt implements ActionListener {

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new tt().createAndShowGUI();
			}
		});
	}

	private JFrame frame;
	public JPanel panel = new JPanel(new GridBagLayout());
	public GridBagConstraints constraints = new GridBagConstraints();
	public GridBagConstraints constraints1 = new GridBagConstraints();

	public List fields = new ArrayList();
	public List fieldButton = new ArrayList();
	public List fieldFile = new ArrayList();
public JTextField  field;
	public JButton button1 = new JButton("Add Dept");
	public JButton button2 = new JButton("Add DataBase");
	public static int countReport = 1;
	String files = null;
	int y = 2;

	protected void createAndShowGUI() {
		String[] labels = { "" };
		for (String label : labels)
			addColumn(label);
		addRowBelow();
		constraints.gridx = 20;
		constraints.gridy = 20;
		constraints1.gridx = 30;
		constraints1.gridy = 20;
		panel.add(button1, constraints);
		panel.add(button2, constraints1);

		frame = new JFrame("Add Button Dynamically");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(new JScrollPane(panel));
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		button1.addActionListener(this);
JRootPane root = frame.getRootPane();
		root.setDefaultButton(button1);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				System.exit(0);
			}
		});
	}

	public void addColumn(String labelText) {
		constraints.gridx = fields.size();
		constraints.gridy = 1;
		panel.add(new JLabel(labelText), constraints);
		constraints.gridy = 2;
	 field = new JTextField(40);
		String str=field.getText();
		System.out.println("in column block"+str);
		// field.setEditable(false);s
		panel.add(field, constraints);
		fields.add(field);
		}

	public void addRowBelow() {
		y++;
		constraints.gridy = y;
		// System.out.println(fields.size());
		for (int x = 0; x < fields.size(); x++) {
			constraints.gridx = x;
		JTextField field = new JTextField(40);
			// field.setEditable(false);
			panel.add(field, constraints);
			constraints.gridx = x + 1;


		}
	}

	public void actionPerformed(ActionEvent ae) {
		if ("Add Dept".equals(ae.getActionCommand())) {
			
			
			addRowBelow();
			field.getText();
			System.out.println(field.getText());
			frame.pack();
			frame.setLocationRelativeTo(null);
			
		}
	}
}



my code working fine but i cant able getvalue of dynamic text Field
Posted
Updated 4-Mar-14 19:42pm
v2

1 solution

Here You Have Two TextField with name field one is Local And Another is Global And In Action Listener Which One You Are Accessing is that `field` is Global(Datamember of class) if you want to get value of text field that is in `addrowbelow` method then you must take that JTextField Object In Global Scope(i means inside class) with different name
 
Share this answer
 
Comments
baliram bhande 5-Mar-14 1:58am    
i can do that but problem is like it can gettext() of only one textField(means only first text field) but i want gettext value for each increasing text Field
Nisarg Desai 5-Mar-14 3:09am    
Then I think You Should Go For Array Of TextField

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