简体   繁体   中英

How to Insert row data from database into specific columns of a JTable?

I have written a GUI Java program that manages a MySQL database. The user selects which columns (which tables and columns will be selected from the database) he/she wants to populate the JTable with.

I hard-coded the column names for the JTable so even if the user chooses to only display the data from a subset of columns, all of the column-names will be visible.

The problem is that when the user chooses columns in a different order than my JTable is anticipating, the data gets displayed in the wrong column.. It's a bit hard to explain so here's a screenshot of the genre data being loaded into the length column:

如您所见,它在长度列中显示类型数据

tableGenerator class:

package gui;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

public class TableGenerator
{

    private ArrayList columnNames = new ArrayList();
    private ArrayList data = new ArrayList();
    private Vector columnNamesVector = new Vector();
    private Vector dataVector = new Vector();
    private int columns = 0;
    private int rows = 0;

    @SuppressWarnings("unchecked")
    public TableGenerator(ResultSet rs)
    {
        try{
            ResultSetMetaData md = rs.getMetaData();
            columns = md.getColumnCount();
            //  Get column names

                columnNames.add("Title");
                columnNames.add("Year");
                columnNames.add("Length");
                columnNames.add("Genre");
                columnNames.add("Actor");
                columnNames.add("Producer");
                columnNames.add("Director");
                columnNames.add("Writer");

            //  Get row data
            while (rs.next())
            {
                ArrayList row = new ArrayList(columnNames.size());

                for (int i = 1; i <= columns; i++)
                {
                        row.add(rs.getObject(i));
                }
                data.add( row );
                rows++;
            }
        }
        catch (SQLException e)
        {
            System.out.println( e.getMessage() );
        }

        // Create Vectors and copy over elements from ArrayLists to them
        // Vector is deprecated but I am using them in this example to keep 
        // things simple - the best practice would be to create a custom defined
        // class which inherits from the AbstractTableModel class


        for (int i = 0; i < data.size(); i++)
        {
            ArrayList subArray = (ArrayList)data.get(i);
            Vector subVector = new Vector();
            for (int j = 0; j < subArray.size(); j++)
            {
                subVector.add(subArray.get(j));
            }
            dataVector.add(subVector);
        }

        for (int i = 0; i < columnNames.size(); i++ )
            columnNamesVector.add(columnNames.get(i));
        }

    public Vector getColumns(){
        return columnNamesVector;
    }
    public Vector getData(){
        return dataVector;
    }
    public ArrayList getColumnNames(){
        return columnNames;
    }
    public int getNumberOfRows(){
        return rows;
    }

}

I'm using the DefaultTableModel with some modifications.. :

model = new DefaultTableModel(rows, columns){

    private static final long serialVersionUID = 1L;

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    @Override
    public Class<?> getColumnClass(int column) {
        if (column < classes.length) 
            return classes[column];
        return super.getColumnClass(column);

    };};

Your query should always return the data for all columns. This means the data will be stored in the same manner in the TableModel.

You can then change the view for the columns to be displayed. That is you can remove TableColumns from the TableColumnModel of the JTable and only the data the user want to view will be displayed, even though it is still available in the model. Then means the user can click on any check box at any time and you don't need to redo the database query, only add the TableColumn back to the table.

Check out Table Column Manager for an example of this approach. This class uses a popup menu to manage the columns, but you can still use your check boxes. You just need to invoke the appropriate method of the TableColumnManager to hide/show a column. That is, assuming the labels of the check boxes match the headings in the table you can just use the check box text to hide/show a column.

The other approach is to NOT hard code the column names (if you build your query to only get specific columns) but instead get the column names from the meta data of the ResultSet. The TableFromDatabaseExample.java from Table From Database shows how this can be done. The code is generic so that appropriate renderers are used for Dates, Integers etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM