Feb 232010
 

bagi pengguna java saat menggunakan JTable mungkin anda ingin memasukkan combo ke dalam jtable anda.

Kali ini saya akan menunjukkan bagaimana cara memasukkan sebuah Object JComboBox Kedalam JTabel siap !!!!!.

Yang pertama buat sebuah class dengan nama mComboEditor :


package demoCombo;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
/**
 *
 * @author mbahsomo
 */
public class mComboEditor extends  DefaultCellEditor{
 public mComboEditor(ComboBoxModel item) {
 super(new JComboBox(item));
 }
}

Kemudian buat sebuah JFrame dengan sebuah JTable Di dalamnya :

package demoCombo;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author mbahsomo
 */
public class LatihanTabel extends javax.swing.JFrame {

 /** Creates new form LatihanTabel */
 public LatihanTabel() {
 initComponents();
 jTable1.getColumnModel().getColumn(1).setCellEditor(new mComboEditor(getIsiCombo()));
 }

 private ComboBoxModel getIsiCombo(){
 DefaultComboBoxModel model = new DefaultComboBoxModel();
 for(int a = 1 ; a <= 10 ; a++){
 model.addElement("Isi Combo Ke " + a );
 }
 return model ;
 }
 /** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
 @SuppressWarnings("unchecked")
 // <editor-fold defaultstate="collapsed" desc="Generated Code">
 private void initComponents() {

 jScrollPane1 = new javax.swing.JScrollPane();
 jTable1 = new javax.swing.JTable();

 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 setTitle("Latihan Combo");

 jTable1.setModel(new javax.swing.table.DefaultTableModel(
 new Object [][] {
 {null, null},
 {null, null},
 {null, null},
 {null, null}
 },
 new String [] {
 "Text Biasa", "Combo"
 }
 ));
 jScrollPane1.setViewportView(jTable1);

 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
 getContentPane().setLayout(layout);
 layout.setHorizontalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
 .addContainerGap(15, Short.MAX_VALUE)
 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 398, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addContainerGap())
 );
 layout.setVerticalGroup(
 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 .addGroup(layout.createSequentialGroup()
 .addContainerGap()
 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)
 .addContainerGap(14, Short.MAX_VALUE))
 );

 java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
 setBounds((screenSize.width-431)/2, (screenSize.height-308)/2, 431, 308);
 }// </editor-fold>

 /**
 * @param args the command line arguments
 */
 public static void main(String args[]) {
 java.awt.EventQueue.invokeLater(new Runnable() {
 public void run() {
 new LatihanTabel().setVisible(true);
 }
 });
 }

 // Variables declaration - do not modify
 private javax.swing.JScrollPane jScrollPane1;
 private javax.swing.JTable jTable1;
 // End of variables declaration

}

Ok Sekarang tinggal jalankan program dan silakan di explorasi sendiri 😀

Facebook Comments

  4 Responses to “Insert JComboBox To JTable java”

  1. nice info..patut d coba..

  2. Silakan di coba mas

  3. sekalian nanya dong mas, kalo comboboxnya di column header gimana tuh? buat filtering data nih, nyari2 ga dapet2…

  4. sugik.do-event.com is amazing, bookmarked!

    unlock blackberry

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

(required)

(required)