A comprehensive example of jtable in swing, including inserting a row of records in a table and sorting the table columns

Source: Internet
Author: User
By implementing the replicacttablemodel interface, you can add a row of records to the table.
import java.awt.BorderLayout;   import java.awt.Point;   import java.awt.event.ActionEvent;   import java.awt.event.ActionListener;   import java.awt.event.ComponentAdapter;   import java.awt.event.ComponentEvent;   import java.util.Vector;     import javax.swing.JFrame;   import javax.swing.JScrollPane;   import javax.swing.JTable;   import javax.swing.JTextField;   import javax.swing.JViewport;   import javax.swing.SwingUtilities;   import javax.swing.table.AbstractTableModel;     public class RowAdder extends JFrame {       final SimpleModel tableData = new SimpleModel();     JTable table = new JTable(tableData);       public static void main(String[] args) {       RowAdder ra = new RowAdder();       ra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       ra.setSize(400, 300);       ra.setVisible(true);     }       public RowAdder() {       final JTextField textField  = new JTextField();       setLayout(new BorderLayout());       add(new JScrollPane(table), BorderLayout.CENTER);       textField.addActionListener(new ActionListener() {         public void actionPerformed(ActionEvent event) {           tableData.addText(textField.getText());           textField.setText("");         }       });       add(textField, BorderLayout.SOUTH);     }     }   class SimpleModel extends AbstractTableModel {       Vector textData = new Vector();       public void addText(String text) {       textData.addElement(text);       fireTableDataChanged();     }       public int getRowCount() {       return textData.size();     }       public int getColumnCount() {       return 3;     }       public Object getValueAt(int row, int column) {       return textData.elementAt(row);     }     }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.