Question

i'm having a little problem in putting a checkbox in my jtable. there are four columns in my jtable the last 2 columns have checkboxes. the data in my jtable are from my database. this is my error "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean", in my database there are two "yes/no" columns which are "present" and "overtime" how can i resolved this error?

this is my code

private void attendance(){
        DateFormat dateFormat = new SimpleDateFormat("MMMM dd hh:mm a");
        Date date = new Date();
        attendanceDate.setText(dateFormat.format(date));

        try{
            String query ="SELECT e.firstName,e.lastName,a.Present,a.Overtime FROM employees e INNER JOIN attendance a ON e.ID = a.empID";
            Object[][] result = connectToDB(query);

            dailyAttendanceTable.setModel(new javax.swing.table.DefaultTableModel(
                result, new String [] {"First Name","Last Name","Present","Overtime"}
            ) 
            {
                Class[] types = new Class [] {
                    java.lang.String.class, java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class
                };
                boolean[] canEdit = new boolean [] {
                    false, false, true, true
                };

                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
                }

                public boolean isCellEditable(int rowIndex, int columnIndex) {

                    return canEdit [columnIndex];
                }
            });
        }catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
Was it helpful?

Solution

Firstly, you need to convert the "yes" to "true" and "no" to "false". You can either change in the database or you may use an ENUM in java to do the conversion.

See if that automatically solves the problem. Else you may need to add another step where you need to convert the values manually and update the Object[][] result using something like boolean b = Boolean.parseBoolean("value");

================= Edited ====================================== Sample code

package com.pkg1;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TableWithModel extends JFrame {

    private static final long serialVersionUID = 1L;

    // constructor that will display a JTable based on elements received as arguments
    TableWithModel(Object[][] obj, String[] header) {
        super("JTable example");
        JPanel panel = new JPanel(new BorderLayout());

        JTable table = new JTable();
        table.setModel(new javax.swing.table.DefaultTableModel(
                obj, header
                ) 
                {
                    /**
                     * 
                     */
                    private static final long serialVersionUID = 1L;
                    Class[] types = new Class [] {
                        java.lang.String.class, java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class
                    };
                    boolean[] canEdit = new boolean [] {
                        false, false, true, true
                    };

                    public Class getColumnClass(int columnIndex) {
                        return types [columnIndex];
                    }

                    public boolean isCellEditable(int rowIndex, int columnIndex) {

                        return canEdit [columnIndex];
                    }
                });

        panel.add(new JScrollPane(table));
        add(panel);    // adding panel to frame
        // and display it
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
        pack();
    }

    // to run the whole thing
    public static void main(String[] args) {
        Object[][] rowAndColumn = {
                {"Dog", "Mammal",Boolean.parseBoolean("true")},
                {"Cat", "Mammal",Boolean.parseBoolean("true")},
                {"Shark", "Fish",Boolean.parseBoolean("false")},
                {"Eagle", "Bird",Boolean.parseBoolean("false")}
        };
        // defines the header
        String[] header = {"Animal", "Family","Domestic"};
        TableWithModel twm = new TableWithModel(rowAndColumn, header);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top