Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

java - JTable will set to editable after converting ResultSet to TableModel with DbUtils. How to make it non-editable again?

Here is My Code for doing this`

public static void addSong(String[] fileDetail, JTable SongData_Table)
{
    try {
        con = DBConnection.getCon();
        stmt = con.createStatement();

        stmt.executeUpdate("insert into songs values (null,'" + fileDetail[0] + "', '" + fileDetail[1] + "',null,null)");
        ResultSet rs = stmt.executeQuery("select * from songs");

        TableModel model = DbUtils.resultSetToTableModel(rs);
        SongData_Table.setModel(model);

        if (con != null) {
            stmt.close();
            con.close();
        }
    } catch (SQLException e) {
        System.out.println("Error in Stmt " + e);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In DbUtils.resultSetToTableModel, replace

return new DefaultTableModel(rows, columnNames);

and return a TableModel that overrides isCellEditable():

return new DefaultTableModel(rows, columnNames){

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...