
how to crate dynamic texfields by clicking on button

Java Swing Dynamic TextFields
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
class DynamicTextFields
{
public static void main(String[] args)
{
JFrame frame=new JFrame();
JButton b=new JButton("Add");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from data where id=1");
String name="",address="";
if(rs.next()){
name=rs.getString("name");
address=rs.getString("address");
}
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab1=new JLabel("Name");
JTextField text1=new JTextField(20);
JLabel lab2=new JLabel("Address");
JTextField text2=new JTextField(20);
lab1.setBounds(10,10,100,20);
text1.setBounds(120,10,100,20);
lab2.setBounds(10,40,100,20);
text2.setBounds(120,40,100,20);
text1.setText(name);
text2.setText(address);
f.add(lab1);
f.add(text1);
f.add(lab2);
f.add(text2);
f.setSize(300,100);
f.setVisible(true);
}
catch(Exception ex){}
}
});
frame.add(b);
frame.pack();
frame.setVisible(true);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.