Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Last active April 15, 2018 04:42
Show Gist options
  • Save resarahadian/5014883 to your computer and use it in GitHub Desktop.
Save resarahadian/5014883 to your computer and use it in GitHub Desktop.
Program AutoComplete TextField Java Swing
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import java.sql.PreparedStatement;
import javax.swing.ImageIcon;
import java.awt.Font;
@SuppressWarnings("serial")
public class FrameTextField extends JFrame
{
private JPanel contentPane;
private JTextField txtNama;
private JLabel lblAlamat;
private JTextArea textAlamat;
private JTextField txtTelp;
private JLabel lblBg;
private JLabel lblAuto;
/**
* Create the frame.
*/
public FrameTextField()
{
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 585, 318);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNama = new JLabel("Nama : ");
lblNama.setBounds(12, 17, 70, 15);
contentPane.add(lblNama);
txtNama = new JTextField();
txtNama.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent act)
{
try
{
Connection konek = Koneksi.getKoneksi();
Statement state = konek.createStatement();
String query = "SELECT Alamat,Telp FROM Data WHERE Nama = '"+txtNama.getText()+"'";
ResultSet rs = state.executeQuery(query);
while(rs.next())
{
textAlamat.setText(rs.getString(1));
txtTelp.setText(rs.getString(2));
}
rs.close();
state.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
});
txtNama.setBounds(91, 10, 245, 29);
contentPane.add(txtNama);
txtNama.setColumns(10);
setLocationRelativeTo(null);
txtNama.addKeyListener(new keyTextField(txtNama));
lblAlamat = new JLabel("Alamat : ");
lblAlamat.setBounds(12, 51, 70, 15);
contentPane.add(lblAlamat);
textAlamat = new JTextArea();
textAlamat.setBounds(91, 51, 275, 103);
contentPane.add(textAlamat);
JButton btnSimpan = new JButton("Simpan");
btnSimpan.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/Simpan.png"));
btnSimpan.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent act)
{
try
{
Connection konek = Koneksi.getKoneksi();
String query = "INSERT INTO Data VALUES(?,?,?)";
PreparedStatement prepare = konek.prepareStatement(query);
prepare.setString(1,txtNama.getText());
prepare.setString(2,textAlamat.getText());
prepare.setString(3,txtTelp.getText());
prepare.executeUpdate();
JOptionPane.showMessageDialog(null,"Data berhasil disimpan","Pesan",JOptionPane.INFORMATION_MESSAGE);
prepare.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Data gagal disimpan","Pesan",JOptionPane.INFORMATION_MESSAGE);
System.out.println(ex);
}
finally
{
txtNama.setText("");
textAlamat.setText("");
txtTelp.setText("");
txtNama.requestFocus();
}
}
});
btnSimpan.setBounds(91, 220, 130, 44);
contentPane.add(btnSimpan);
JButton btnKeluar = new JButton("Keluar");
btnKeluar.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent act)
{
JOptionPane.showMessageDialog(null, "Terima kasih","Pesan",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
});
btnKeluar.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/hapus.png"));
btnKeluar.setBounds(266, 220, 130, 44);
contentPane.add(btnKeluar);
JLabel lblNotelp = new JLabel("No.Telp : ");
lblNotelp.setBounds(12, 176, 80, 15);
contentPane.add(lblNotelp);
txtTelp = new JTextField();
txtTelp.setBounds(91, 166, 233, 27);
contentPane.add(txtTelp);
txtTelp.setColumns(10);
lblAuto = new JLabel("* AutoComplete Text");
lblAuto.setFont(new Font("FreeSerif", Font.PLAIN, 15));
lblAuto.setBounds(348, 17, 179, 15);
contentPane.add(lblAuto);
JLabel lblIcon = new JLabel("");
lblIcon.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/mobil.png"));
lblIcon.setBounds(398, 70, 159, 135);
contentPane.add(lblIcon);
lblBg = new JLabel("");
lblBg.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/pinkBg.jpg"));
lblBg.setBounds(0, 0, 589, 296);
contentPane.add(lblBg);
}
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
FrameTextField frame = new FrameTextField();
frame.setVisible(true);
}
catch (UnsupportedLookAndFeelException e) {
}
catch (ClassNotFoundException e){
}
catch (InstantiationException e) {
}
catch (IllegalAccessException e) {
}
}
});
}
}
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTextField;
import java.sql.*;
public class keyTextField extends KeyAdapter
{
private JTextField txtField;
@SuppressWarnings("rawtypes")
private List daftar;
@SuppressWarnings("rawtypes")
public keyTextField(JTextField txtFieldParam)
{
txtField = txtFieldParam;
daftar = new ArrayList();
databaseNama();
}
public void keyPressed(KeyEvent key)
{
switch(key.getKeyCode())
{
case KeyEvent.VK_BACK_SPACE :
break;
case KeyEvent.VK_ENTER :
txtField.setText(txtField.getText());
break;
default :
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
String kt = txtField.getText();
autoComplete(kt);
}
});
}
}
public void autoComplete(String kt)
{
String complete = "";
int start = kt.length();
int last = kt.length();
int a;
for(a=0;a<daftar.size();a++)
{
if(daftar.get(a).toString().startsWith(kt))
{
complete = daftar.get(a).toString();
last = complete.length();
break;
}
}
if(last>start)
{
txtField.setText(complete);
txtField.setCaretPosition(last);
txtField.moveCaretPosition(start);
}
}
@SuppressWarnings("unchecked")
public void databaseNama()
{
try
{
Connection konek = Koneksi.getKoneksi();
Statement state = konek.createStatement();
String query = "SELECT Nama FROM Data";
ResultSet rs = state.executeQuery(query);
while(rs.next())
{
daftar.add(rs.getString(1));
}
rs.close();
state.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
import java.sql.DriverManager;
import java.sql.Connection;
public class Koneksi
{
private static Connection koneksi;
public static Connection getKoneksi()
{
if(koneksi == null)
{
try
{
String url = "jdbc:mysql://localhost/Identitas";
String username = "root";
String password = "root";
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
koneksi = DriverManager.getConnection(url,username,password);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
return koneksi;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment