Created
January 29, 2013 06:47
-
-
Save nugraha16/4662320 to your computer and use it in GitHub Desktop.
Membuat Autonumber Huruf dan Angka di Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package newbieilmuDemo; | |
import java.awt.*; | |
import javax.swing.*; | |
import javax.swing.table.DefaultTableModel; | |
import javax.swing.table.JTableHeader; | |
import javax.swing.table.TableCellRenderer; | |
import java.awt.event.*; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
//import com.jtattoo.plaf.aero.*; | |
//import com.christalign.emc.swing.*; | |
public class autonumberDemo extends JFrame implements ActionListener,MouseListener{ | |
private JLabel lblgambar1 = new JLabel(new ImageIcon("src/image/lap2.png")); | |
private JLabel lblJudul = new JLabel("::[[DATA BANGSAL]]::"); | |
private JLabel lblKdBangsal = new JLabel("Kode Bangsal :"); | |
private JTextField txtKdBangsal = new JTextField(20); | |
private JLabel lblNmBangsal = new JLabel("Nama Bangsal :"); | |
private JTextField txtNmBangsal = new JTextField(20); | |
private JLabel lblKelas = new JLabel("Kelas :"); | |
private JTextField txtKelas = new JTextField(20); | |
private String[] JdlTabel = {"KdBangsal","NmBangsal","Kelas"}; | |
DefaultTableModel tabModel; | |
JTable tabel = new JTable(); | |
JTableHeader header = tabel.getTableHeader(); | |
JScrollPane scrTabel = new JScrollPane(); | |
private JLabel lblKeyWord = new JLabel("Key Word :"); | |
private JTextField txtKeyWord = new JTextField(20); | |
private JButton btnCentang = new JButton("",new ImageIcon("src/image/dialog-ok-2.png")); | |
private JButton btnCari = new JButton("",new ImageIcon("src/image/find.png")); | |
private JLabel lblRecord = new JLabel("Record : 3"); | |
private JButton btnSimpan = new JButton("Simpan",new ImageIcon("src/imagebaru.jpeg")); | |
private JButton btnBatal = new JButton("Batal",new ImageIcon("src/imageedit.jpeg")); | |
private JButton btnHapus = new JButton("Hapus",new ImageIcon("src/imagecetak.png")); | |
private JButton btnEdit = new JButton("Edit",new ImageIcon("src/imageclose.jpeg")); | |
private JButton btnPrint = new JButton("Print",new ImageIcon("src/imagecetak.png")); | |
private JButton btnKeluar = new JButton("Keluar",new ImageIcon("src/imageclose.jpeg")); | |
private JLabel lblnotes = new JLabel(":Semua Field Wajib Isi: "); | |
private JLabel lblgambar = new JLabel(new ImageIcon("src/image/pink.png")); | |
//private SimpleTable simpleTable; | |
public autonumberDemo() { | |
// TODO Auto-generated constructor stub | |
super(".::Data Bangsal::."); | |
setIconImage(getToolkit().getImage("src/image/edit-user.png")); | |
setSize(530,520); | |
setVisible(true); | |
getContentPane().setLayout(null); | |
setLocationRelativeTo(null); | |
setDefaultCloseOperation(DISPOSE_ON_CLOSE); | |
//simpleTable = new SimpleTable(JdlTabel); | |
//simpleTable.setBackgroundImage(new ImageIcon("src/image/pink.png")); | |
tabModel = new DefaultTableModel(null,JdlTabel); | |
header.setBackground(Color.pink); | |
tabel.setModel(tabModel); | |
tabel.setBackground(Color.white); | |
scrTabel.getViewport().add(tabel); | |
tabel.setEnabled(true); | |
scrTabel.setBounds(5, 110, 515, 250); | |
getContentPane().add(scrTabel); | |
lblgambar1.setBounds(5, 5, 50, 30); | |
getContentPane().add(lblgambar1); | |
lblJudul.setFont(new Font("",Font.BOLD,14)); | |
lblJudul.setBounds(200, 5, 150, 30); | |
lblJudul.setForeground(Color.white); | |
getContentPane().add(lblJudul); | |
lblKdBangsal.setBounds(10, 50, 120, 22); | |
getContentPane().add(lblKdBangsal); | |
lblKdBangsal.setForeground(Color.white); | |
txtKdBangsal.setBounds(120, 50, 120, 22); | |
getContentPane().add(txtKdBangsal); | |
txtKdBangsal.setEnabled(false); | |
lblNmBangsal.setBounds(10, 80, 120, 22); | |
getContentPane().add(lblNmBangsal); | |
lblNmBangsal.setForeground(Color.white); | |
txtNmBangsal.setBounds(120, 80, 400, 22); | |
getContentPane().add(txtNmBangsal); | |
lblKelas.setBounds(350, 50, 120, 22); | |
getContentPane().add(lblKelas); | |
lblKelas.setForeground(Color.white); | |
txtKelas.setBounds(400, 50, 120, 22); | |
getContentPane().add(txtKelas); | |
lblKeyWord.setBounds(10, 370, 120, 22); | |
getContentPane().add(lblKeyWord); | |
lblKeyWord.setForeground(Color.white); | |
txtKeyWord.setBounds(120, 370, 220, 22); | |
getContentPane().add(txtKeyWord); | |
btnCentang.setBounds(340, 370, 30, 21); | |
getContentPane().add(btnCentang); | |
btnCari.setBounds(373, 370, 30, 21); | |
getContentPane().add(btnCari); | |
lblRecord.setBounds(420, 370, 120, 21); | |
getContentPane().add(lblRecord); | |
lblRecord.setForeground(Color.white); | |
btnSimpan.setBounds(10, 410, 80, 25); | |
getContentPane().add(btnSimpan); | |
btnSimpan.setForeground(Color.pink); | |
btnBatal.setBounds(90, 410, 80, 25); | |
getContentPane().add(btnBatal); | |
btnBatal.setForeground(Color.pink); | |
btnHapus.setBounds(170, 410, 80, 25); | |
getContentPane().add(btnHapus); | |
btnHapus.setForeground(Color.pink); | |
btnEdit.setBounds(250, 410, 80, 25); | |
getContentPane().add(btnEdit); | |
btnEdit.setForeground(Color.pink); | |
btnPrint.setBounds(330, 410, 80, 25); | |
getContentPane().add(btnPrint); | |
btnPrint.setForeground(Color.pink); | |
btnKeluar.setBounds(410, 410, 80, 25); | |
getContentPane().add(btnKeluar); | |
btnKeluar.setForeground(Color.pink); | |
lblnotes.setBounds(200, 450, 120, 25); | |
getContentPane().add(lblnotes); | |
lblnotes.setForeground(Color.white); | |
lblgambar.setBounds(0, 0, getWidth(), getHeight()); | |
getContentPane().add(lblgambar); | |
autonumber(); | |
TampilkeTabel(); | |
btnSimpan.addActionListener(this); | |
} | |
void autonumber(){ | |
try{ | |
Class.forName("com.mysql.jdbc.Driver").newInstance(); | |
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root"); | |
Statement state = con.createStatement(); | |
String sql = "select max(right(KdBangsal,2)) as no_terakhir from DtBangsal"; | |
ResultSet rs = state.executeQuery(sql); | |
if(rs.first()==false) | |
{ | |
txtKdBangsal.setText("BGS0001"); | |
} | |
else{ | |
rs.last(); | |
int no=rs.getInt(1)+1; | |
String cno=String.valueOf(no); | |
int pjg_cno=cno.length(); | |
for(int i=0;i<2 - pjg_cno; i++); | |
{ | |
cno="000"+cno; | |
} | |
txtKdBangsal.setText("BGS"+cno); | |
} | |
rs.close(); | |
con.close(); | |
} | |
catch(Exception ex){ | |
} | |
} | |
void getDataTabel(){ | |
txtKdBangsal.setText(""+tabModel.getValueAt(tabel.getSelectedRow(),0)); | |
txtNmBangsal.setText(""+tabModel.getValueAt(tabel.getSelectedRow(),1)); | |
txtKelas.setText(""+tabModel.getValueAt(tabel.getSelectedRow(),2)); | |
} | |
void TampilkeTabel(){ | |
try{ | |
hapusTabel(); | |
Class.forName("com.mysql.jdbc.Driver").newInstance(); | |
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root"); | |
Statement state = con.createStatement(); | |
String sql = "select * from DtBangsal"; | |
ResultSet rs = state.executeQuery(sql); | |
while(rs.next()) | |
{ | |
String kdbangsal = rs.getString(1); | |
String nmbangsal = rs.getString(2); | |
String kelas = rs.getString(3); | |
String[] data = {kdbangsal,nmbangsal,kelas}; | |
tabModel.addRow(data); | |
autonumber(); | |
//Kosong(); | |
} | |
rs.close(); | |
con.close(); | |
} | |
catch(Exception ex){ | |
} | |
} | |
void hapusTabel(){ | |
int brs = tabModel.getRowCount(); | |
for(int i=0;i<brs;i++){ | |
tabModel.removeRow(0); | |
} | |
} | |
void Tambah(){ | |
try{ | |
Class.forName("com.mysql.jdbc.Driver").newInstance(); | |
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","root","root"); | |
Statement st = con.createStatement(); | |
String sql = "insert into DtBangsal values('"+txtKdBangsal.getText()+ | |
"','"+txtNmBangsal.getText()+ | |
"','"+txtKelas.getText()+ | |
"')"; | |
int hasil = st.executeUpdate(sql); | |
if(hasil>0) | |
JOptionPane.showMessageDialog(null,"Data berhasil di tambah","Pesan",JOptionPane.INFORMATION_MESSAGE); | |
con.close(); | |
TampilkeTabel(); | |
kosong(); | |
} | |
catch(Exception ex){ | |
JOptionPane.showConfirmDialog(null, "Data gagal di tambah"); | |
} | |
} | |
void kosong(){ | |
txtNmBangsal.setText(""); | |
txtKelas.setText(""); | |
} | |
/** | |
* @param args | |
* @throws UnsupportedLookAndFeelException | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
//UIManager.setLookAndFeel(new AeroLookAndFeel()); | |
new autonumberDemo(); | |
} | |
@Override | |
public void mouseClicked(MouseEvent arg0) { | |
// TODO Auto-generated method stub | |
getDataTabel(); | |
} | |
@Override | |
public void mouseEntered(MouseEvent arg0) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void mouseExited(MouseEvent arg0) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void mousePressed(MouseEvent arg0) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void mouseReleased(MouseEvent arg0) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void actionPerformed(ActionEvent arg0) { | |
// TODO Auto-generated method stub | |
if(arg0.getSource()==btnSimpan){ | |
Tambah(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment