Here is the Sample Code.
String[] columnNames = {"A","B","C","D","E", "F", "G"};
Object[][] data = new Object[5][7];
JTable jTable = new JTable();
jTable.setVisible(true);
jTable.setAutoCreateRowSorter(true);
jTable.setModel(new DefaultTableModel(data, columnNames));
add(jTable);
JScrollPane scrollPane = new JScrollPane(jTable);
jTable.setFillsViewportHeight(true);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(jTable.getTableHeader(), BorderLayout.PAGE_START);
this.getContentPane().add(jTable, BorderLayout.CENTER);
---- Complete code --------------------------------------
import java.awt.BorderLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewJFrame.java
*
* Created on Feb 24, 2010, 10:37:20 AM
*/
/**
*
* @author samith
*/
public class NewJFrame extends javax.swing.JFrame {
private JScrollPane jScrollPane1;
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
String[] columnNames = {"A","B","C","D","E", "F", "G"};
Object[][] data = new Object[5][7];
JTable jTable = new JTable();
jTable.setVisible(true);
jTable.setAutoCreateRowSorter(true);
jTable.setModel(new DefaultTableModel(data, columnNames));
add(jTable);
JScrollPane scrollPane = new JScrollPane(jTable);
jTable.setFillsViewportHeight(true);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(jTable.getTableHeader(), BorderLayout.PAGE_START);
this.getContentPane().add(jTable, BorderLayout.CENTER);
}
/** 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() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addContainerGap(576, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(467, Short.MAX_VALUE)
.addComponent(jButton1))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
--------------------------------------------------------------------
For more visit :-
http://java.sun.com/docs/books/tutorial/uiswing/components/table.htmlBest Regards.