January 07, 2012

how to create dynamic table in java

How to create a dynamic table in java

Description : The example show how to create a dynamic table using java swing, The code is developed using eclipse ide

/** DyanmicJTable
*
* version 1.0
*Aaron+Rodgers
*
*/
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.GridBagConstraints;
import java.util.Vector;
/**
*
*/
/**
* @author vishnu
*
*/
public class DyanmicJTable extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
private JTextField jTextField = null;
private JPanel jPanel = null;
private JScrollPane jScrollPane = null;
private JTable jTable = null;
private DefaultTableModel tableModel = null;
/**
* This is the default constructor
*/
public DyanmicJTable() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(600, 600);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJPanel(), null);
}
return jContentPane;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(218, 50, 176, 23));
jButton.setText("Add to table");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Vector rowData = new Vector();
rowData.add(jTextField.getText());
tableModel.addRow(rowData);
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(30, 49, 170, 26));
}
return jTextField;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.gridx = 0;
jPanel = new JPanel();
jPanel.setLayout(new GridBagLayout());
jPanel.setBounds(new Rectangle(22, 105, 524, 214));
jPanel.add(getJScrollPane(), gridBagConstraints);
}
return jPanel;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
tableModel = new DefaultTableModel();
tableModel.addColumn("Name");
String [] data = {"jhon duke"};
tableModel.addRow(data);
jTable = new JTable(tableModel);
}
return jTable;
}

public static void main(String[] args) {
    DyanmicJTable dynamicTable = new DyanmicJTable();
    dynamicTable.setVisible(true);
    dynamicTable.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

}

Description : Creating dynamic table (Grid) in java web application using javascript



Example

Java swing dynamic table example

create a dynamic table

Facebook comments