Created
February 18, 2013 18:36
-
-
Save resarahadian/4979566 to your computer and use it in GitHub Desktop.
Program Table dengan ComboBox
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
import java.awt.EventQueue; | |
import javax.swing.DefaultCellEditor; | |
import javax.swing.JComboBox; | |
import javax.swing.JFrame; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.UIManager; | |
import javax.swing.UnsupportedLookAndFeelException; | |
import javax.swing.border.EmptyBorder; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTable; | |
import javax.swing.table.DefaultTableModel; | |
import javax.swing.JButton; | |
import java.awt.event.ActionListener; | |
import java.awt.event.ActionEvent; | |
import javax.swing.ImageIcon; | |
import javax.swing.JLabel; | |
@SuppressWarnings("serial") | |
public class ComboBoxTable extends JFrame | |
{ | |
private JPanel contentPane; | |
private JTable tabel; | |
DefaultTableModel tabelModel; | |
String[] field = {"ID","Nama","Status Pegawai"}; | |
String[][] data = {{"P01","Resa",null},{"P02","Candra",null},{"P03","Ayu",null},{"P04","Dian",null},{"P05","Dita",null}}; | |
@SuppressWarnings("rawtypes") | |
private JComboBox cbPegawai; | |
private JButton btnTampilData; | |
private JLabel lblBackground; | |
/** | |
* Create the frame. | |
*/ | |
@SuppressWarnings({ "rawtypes", "unchecked" }) | |
public ComboBoxTable() | |
{ | |
setResizable(false); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setBounds(100, 100, 640, 326); | |
contentPane = new JPanel(); | |
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | |
setContentPane(contentPane); | |
contentPane.setLayout(null); | |
cbPegawai = new JComboBox(); | |
cbPegawai.addItem("Tetap"); | |
cbPegawai.addItem("Kontrak"); | |
JScrollPane scrollPane = new JScrollPane(); | |
scrollPane.setBounds(54, 12, 531, 209); | |
contentPane.add(scrollPane); | |
setLocationRelativeTo(null); | |
tabelModel = new DefaultTableModel(data,field); | |
tabel = new JTable(); | |
tabel.setModel(tabelModel); | |
tabel.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(cbPegawai)); | |
scrollPane.setViewportView(tabel); | |
btnTampilData = new JButton("Tampilkan Data Tabel"); | |
btnTampilData.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/tampilData.png")); | |
btnTampilData.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
int pilih = tabel.getSelectedRow(); | |
if(pilih<0) | |
{ | |
return; | |
} | |
String id = (String) tabelModel.getValueAt(pilih, 0); | |
String nama = (String) tabelModel.getValueAt(pilih, 1); | |
String pegawai = (String) tabelModel.getValueAt(pilih, 2); | |
JOptionPane.showMessageDialog(null,"No.ID : " + id + " \n " +"Nama : " + nama + " \n " + "Pegawai : " + pegawai); | |
} | |
}); | |
btnTampilData.setBounds(193, 233, 239, 51); | |
contentPane.add(btnTampilData); | |
lblBackground = new JLabel(""); | |
lblBackground.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/whiteGradient.jpg")); | |
lblBackground.setBounds(0, 0, 638, 296); | |
contentPane.add(lblBackground); | |
} | |
/** | |
* 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"); | |
ComboBoxTable frame = new ComboBoxTable(); | |
frame.setVisible(true); | |
} | |
catch (UnsupportedLookAndFeelException e) { | |
} | |
catch (ClassNotFoundException e){ | |
} | |
catch (InstantiationException e) { | |
} | |
catch (IllegalAccessException e) { | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment