Wednesday, 3 March 2010

Tabel Biodata Pada Java



Tabel Biodata


kode :

import java.util.logging.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;

public class tablebiodata extends JFrame{
private JScrollPane scrollPane;
private JTable table;
private JLabel labelNama;
private JLabel labelNpm;
private JLabel labelKelas;
private JLabel labelJurusan;
private JTextField textNama;
private JTextField textNpm;
private JTextField textKelas;
private JTextField textJurusan;
private JButton buttonSubmit;
private JButton buttonReset;
private Object[] obj;
private DefaultTableModel model;
private EventListener eventTable;

public static void main(String[]damian){
SwingUtilities.invokeLater(new Runnable() {

public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(tablebiodata.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(tablebiodata.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(tablebiodata.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(tablebiodata.class.getName()).log(Level.SEVERE, null, ex);
} finally {
tablebiodata biodata = new tablebiodata();
biodata.setVisible(true);
}
}
});
}

public tablebiodata() {
setTitle("Program Aplikasi Biodata");
initTableModel();
initComponent();
setLocationRelativeTo(null);
initAction();

}

private void initComponent() {
this.getContentPane().setLayout(null);
setSize(700, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

scrollPane = new JScrollPane();
labelNama = new JLabel();
labelNpm = new JLabel();
labelKelas = new JLabel();
labelJurusan = new JLabel();
textNama = new JTextField();
textNpm = new JTextField();
textKelas = new JTextField();
textJurusan = new JTextField();
buttonSubmit = new JButton();
buttonReset = new JButton();

table = new JTable();
table.setModel(model);
scrollPane.setViewportView(table);
scrollPane.setBounds(20, 140, 655, 270);

labelNama.setText("Nama");
labelNpm.setText("N.P.M");
labelKelas.setText("Kelas");
labelJurusan.setText("Jurusan");
labelNama.setBounds(new Rectangle(20, 10, 90, 20));
labelNpm.setBounds(new Rectangle(20, 40, 90, 20));
labelKelas.setBounds(new Rectangle(20, 70, 90, 20));
labelJurusan.setBounds(new Rectangle(20, 100, 150, 20));
labelNama.setHorizontalTextPosition(SwingConstants.CENTER);
labelNpm.setHorizontalTextPosition(SwingConstants.CENTER);
labelKelas.setHorizontalTextPosition(SwingConstants.CENTER);
labelJurusan.setHorizontalTextPosition(SwingConstants.CENTER);

textNama.setBounds(new Rectangle(70, 10, 605, 20));
textNpm.setBounds(new Rectangle(70, 40, 605, 20));
textKelas.setBounds(new Rectangle(70, 70, 605, 20));
textJurusan.setBounds(new Rectangle(70, 100, 605, 20));

buttonSubmit.setText("Insert");
buttonSubmit.setBounds(575, 425, 100, 25);
buttonSubmit.setHorizontalTextPosition(SwingConstants.CENTER);

buttonReset.setText("Reset");
buttonReset.setBounds(465, 425, 100, 25);
buttonReset.setHorizontalTextPosition(SwingConstants.CENTER);

this.getContentPane().add(scrollPane);
this.getContentPane().add(labelNama);
this.getContentPane().add(labelNpm);
this.getContentPane().add(labelKelas);
this.getContentPane().add(labelJurusan);
this.getContentPane().add(textNama);
this.getContentPane().add(textNpm);
this.getContentPane().add(textKelas);
this.getContentPane().add(textJurusan);
this.getContentPane().add(buttonSubmit);
this.getContentPane().add(buttonReset);
}

private void initAction() {
buttonSubmit.addActionListener(new Button_Insert_Data());
buttonReset.addActionListener(new Button_Reset_Data());
}

class Button_Insert_Data implements ActionListener{

public void actionPerformed(ActionEvent e) {
if(textNama.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "Nama masih kosong");
textNama.requestFocusInWindow();
}else if(textNpm.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "N.P.M masih kosong");
textNpm.requestFocusInWindow();
}else if(textNpm.getText().length()>8){
JOptionPane.showMessageDialog(null, "N.P.M tidak boleh lebih dari 8 karakter");
textNpm.requestFocusInWindow();
}else if(textKelas.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "Kelas masih kosong");
textKelas.requestFocusInWindow();
}else if(textJurusan.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "Jurusan masih kosong");
textJurusan.requestFocusInWindow();
}else{
addListenerObjec(eventTable);
JOptionPane.showMessageDialog(null, "Data berhasil dimasukan");
reset();
}
}

}

class Button_Reset_Data implements ActionListener{

public void actionPerformed(ActionEvent e) {
reset();
}

}

public void addListenerObjec(EventListener listener){
obj = new Object[]{textNama.getText(), textNpm.getText(), textKelas.getText(), textJurusan.getText()};
model.addRow(obj);
}

private void initTableModel(){
model = new DefaultTableModel();
model.addColumn("Nama");
model.addColumn("N.P.M");
model.addColumn("Kelas");
model.addColumn("Jurusan");
}

private void reset(){
textJurusan.setText("");
textNama.setText("");
textNpm.setText("");
textKelas.setText("");
textNama.requestFocusInWindow();
}

}






Untuk menjalankan program ini harus mempunyai java yaitu j2sdk, atau jdk "untuk java", untuk menyimpan filenya ketik nama_file.java simpan di folder "bin" pada software javanya. Dan untuk menjalankan serta mengcompile buka "CommandPrompt", buka folder bin di software javanya, Cara Compile ketik "javac nama_file.java" tdk memakai tanda kutip dan cara run atau menjalankan ketik "java nama_file".

Penjelasan singkat pada kode tersebut :

Program ini menggunakan package awt, swing, util. Program ini akan mengeluarkan seperti dibawah ini ..

Output Program :

Posting Terkait

2 comments:

Evo D said...

Wa Bermanfaat banget bro thax infonya ya!!! ;))

Anonymous said...

izin mempelajari dan ngedit

Post a Comment

Mikail Alfith Amarullah

↑ Grab this Headline Animator

IKLAN

  © Blogger Tamplates

Back to TOP