In this tutorial you will learn
- What is
JTextField
in Java Swing? - How to set
JTextField
Properties? - How to set Event Listener in
JTextField
? - How to Save and Retrieve JTextField text to Database Table?
JTextField
JTextField
is like a TextBox control that allows editing of a single line of text. This component is mostly used for asking and collecting small information from the user like their name, mobile number, city, occupation etc. Text Field controls enable you to edit string or text in a form. This text might be a single line of text or multiple lines of text, a password etc.
- Creating JTextField in Java Swing
- Add Event Listener in JTextField
- Save and Retrieve JTextField text to Database Table
Creating JTextField
in Java Swing
It is a simple example of creating JTextField
component in swing form. In this example, I have created a simple swing form with JTextField and set some basic properties of JTextField.
Programming Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package Test.MainJava.com; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class JTextField_Example { JFrame frame; JTextField txtname, txtcity; JLabel lblname, lblcity; JButton btnsave; public static void main(String[] args) { JTextField_Example j=new JTextField_Example(); j.Draw(); } public void Draw() { //JLabel lblname=new JLabel("Enter Your Name"); //JTextField txtname=new JTextField(); //Initialize txtname.setText("Enter Your Name"); //Setting Text txtname.setColumns(20); //Setting Size txtname.setToolTipText("Your Name"); //Setting ToolTip txtname.selectAll(); //Selecting All Text btnsave=new JButton("Display Text"); btnsave.addActionListener(actions); //Adding Component to Frame frame = new JFrame("Simple JTextField Example"); frame.setSize(300, 200); frame.setLayout(new FlowLayout()); frame.add(lblname); frame.add(txtname); frame.add(btnsave); frame.setVisible(true); } private ActionListener actions = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == btnsave) { String txt1; txt1=txtname.getText(); JOptionPane.showMessageDialog(null, "Hello "+txt1+". How are you"); } } }; } |
Output
JTextField With Event Listener
Most of the time, you need to run a block of code on key pressed event in JTextField
. JTextField with Event Listener allows you to write code on various JTextField Events like Key press, Key Typed, Key Released etc.
Programming Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | package Test.MainJava.com; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.*; public class JTextField_Example { JFrame frame; JTextField txtname; JLabel lblname, lblmsg; JButton btnsave; public static void main(String[] args) { JTextField_Example j=new JTextField_Example(); j.Draw(); } public void Draw() { //JLabel lblname=new JLabel("Enter Your Name"); lblmsg=new JLabel("Status"); lblmsg.setForeground(Color.DARK_GRAY); //JTextField txtname=new JTextField(); //Initialize txtname.setText("Enter Your Name"); //Setting Text txtname.setColumns(20); //Setting Size txtname.setToolTipText("Your Name"); //Setting ToolTip txtname.selectAll(); //Selecting All Text btnsave=new JButton("Display Text"); btnsave.addActionListener(actions); //Adding Component to Frame frame = new JFrame("Simple JTextField Example"); frame.setSize(300, 200); frame.setLayout(new FlowLayout()); frame.add(lblname); frame.add(txtname); frame.add(btnsave); frame.add(lblmsg); frame.setVisible(true); // This Event works when Enter is pressed txtname.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String txt1; txt1=txtname.getText(); JOptionPane.showMessageDialog(null, "Hello "+txt1+". How Just Pressed Enter Button"); } }); //JTextField KeyListener Event txtname.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub lblmsg.setText("You Just Typed "+e.getKeyChar()); } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub lblmsg.setText("You Just Released "+e.getKeyChar()); } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub lblmsg.setText("You Just Pressed "+e.getKeyChar()); } }); } private ActionListener actions = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == btnsave) { String txt1; txt1=txtname.getText(); JOptionPane.showMessageDialog(null, "Hello "+txt1+". How are you"); } } }; } |
Output
Save JTextField Text to Database
Till Now, you learned how to create and add an event in JTextField in Java. Now, you will learn how to connect JTextField to a database table and save & Retrieve data from the table.
Programming Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | package Test.MainJava.com; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import java.sql.*; public class JTextField_Example { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String dburl = "jdbc:mysql://localhost/swing_db"; static final String dbuser = "root"; static final String dbpass = "root"; Connection con=null; Statement stmt=null; JFrame frame; JTextField txtname, txtcity; JLabel lblname, lblmsg; JButton btnsave, btnget; public static void main(String[] args) { JTextField_Example j=new JTextField_Example(); j.Draw(); } public void Draw() { //JLabel lblname=new JLabel("Enter Your Name"); lblmsg=new JLabel("Status"); lblmsg.setForeground(Color.DARK_GRAY); //JTextField txtname=new JTextField(); //Initialize txtname.setText("Enter Your Name"); //Setting Text txtname.setColumns(20); //Setting Size txtname.setToolTipText("Your Name"); //Setting ToolTip txtname.selectAll(); //Selecting All Text btnsave=new JButton("Insert Record"); btnsave.addActionListener(actions); btnget=new JButton("Get Records"); btnget.addActionListener(actions); //Adding Component to Frame frame = new JFrame("Simple JTextField Example"); frame.setSize(300, 200); frame.setLayout(new FlowLayout()); frame.add(lblname); frame.add(txtname); frame.add(btnsave); frame.add(btnget); frame.add(lblmsg); frame.setVisible(true); } private ActionListener actions = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == btnsave) { SaveData(); } else if(e.getSource()== btnget) { GetData(); } } }; public void SaveData() { try { String txt1; txt1=txtname.getText(); con = DriverManager.getConnection(dburl, dbuser, dbpass); //Step 2 : Initialize Statement stmt=con.createStatement(); String query="INSERT INTO control(Table_JTextField) VALUES('"+txt1+"')"; stmt.executeUpdate(query); JOptionPane.showMessageDialog(null, txt1+" Saved Successfully to Database"); } catch (SQLException ex) { System.err.println("Cannot connect ! "); ex.printStackTrace(); } finally { System.out.println("Closing the connection."); if (con != null) try { con.close(); } catch (SQLException ignore) {} } } public void GetData() { Connection con = null; Statement stmt = null; try { //Step 1 : Connecting to server and database con = DriverManager.getConnection(dburl, dbuser, dbpass); //Step 2 : Initialize Statement stmt=con.createStatement(); //Step 3 : SQL Query String query="SELECT * FROM control"; //Step 4 : Run Query In ResultSet ResultSet rset = stmt.executeQuery(query); String str=""; while(rset.next()) { str=str+rset.getString(2)+" | "; lblmsg.setText(str); } } catch (SQLException ex) { System.err.println("Cannot connect ! "); ex.printStackTrace(); } finally { System.out.println("Closing the connection."); if (con != null) try { con.close(); } catch (SQLException ignore) {} } } } |
Summary
In this JTextField
Tutorial, you learned how to create JTextField
in Java Swing. You also learned to save and Retrieve JTextField text to Database Table. In the next chapter, you will learn JTextArea in Java Swing.