动态JTable - 从所有行获取数据

时间:2017-03-11 11:04:17

标签: java dynamic jtable

我在点击按钮时动态地向JTable添加行。 如何保存MySQL中所有行的数据?我可以保存所选行的数据,但我需要保存用户添加的所有行的数据。  我使用以下代码从行获取值:

def new_vect(vect, n_row):
    l_vect = len(vect)
    l_new_vect = n_row / l_vect + 1
    new_vect = vect * l_new_vect
    return new_vect[:n_row]

mtcars['year'] = new_vect([1973,1974],mtcars.shape[0])

如何移动其他行?

1 个答案:

答案 0 :(得分:1)

您可以使用从DefaultTableModel扩展的自定义表模型以及您自己的数据对象来实现此目的。

请参阅以下示例。国旗' addedLater'可用于区分动态数据和初始数据。

package test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JTabeGetRowsFromTable {

    static String []columnNames = {
        "Name",
        "Age",
        "Country"
    } ;
    Vector<Student> students = new Vector<Student> () ;

    Vector<Student> buildInitialData (){
        students.add(new Student("X", 18, "US")) ;
        students.add(new Student("Y", 19, "US")) ;
        students.add(new Student("Z", 20, "US")) ;

        return students ;
    }

    Student getNewStudent (){
        return new Student("A", 21, "Canada", true) ;
    }

    protected void buildAndGetTableData (){
        JTable table = new JTable () ;

        CustomTableModel ctm = new CustomTableModel(buildInitialData()) ;
        ctm.setColumnIdentifiers(columnNames);
        table.setModel (ctm) ;


        JFrame frame = new JFrame () ;
        frame.add(new JScrollPane(table), BorderLayout.CENTER) ;

        JButton btnAddRow = new JButton ("Add Row");
        btnAddRow.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ctm.addARow(getNewStudent());
            }
        });
        frame.add(btnAddRow, BorderLayout.SOUTH);

        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    private static class Student {
        String name;
        int age;
        String country ;
        boolean addedLater = false;

        public Student (String name, int age, String country){
            this(name, age, country, false) ;
        }

        public Student (String name, int age, String country, boolean addedLater){
            this.name = name ;
            this.age = age ;
            this.country = country ;
        }
    }



    private class CustomTableModel extends DefaultTableModel{

        /**
         * 
         */
        private static final long serialVersionUID = -8119518861817992201L;

        private Vector<Student> data ;

        public CustomTableModel (Vector<Student> data){
            this.data = data ;
        }

        @Override
        public Object getValueAt(int row, int column) {
            if (data != null && row < data.size()){
                Student student = (Student)data.get(row) ;
                switch (column){
                case 0:
                    return student.name ;
                case 1:
                    return student.age ;
                case 2:
                    return student.country;
                default:
                    return null ;

                }
            }

            return null ;
        }

        @Override
        public int getRowCount() {
            if (data != null){
                return data.size();
            }
            return 0 ;
        }

        public void addARow (Student student){
            data.add(student) ;
            fireTableDataChanged();
        }
    }

    public static void main (String args[]){
        JTabeGetRowsFromTable test = new JTabeGetRowsFromTable () ;
        test.buildAndGetTableData();
    }

}