0

Here is my code:

Object[][] refreshCartonCodesToTable = dbutils.checker.CartonCodesToTable();
String[] colnames = new String[6];
colnames[0] = selectCodes.invoiceTable.getColumnName(0).toString();
colnames[1] = selectCodes.invoiceTable.getColumnName(1).toString();
colnames[2] = selectCodes.invoiceTable.getColumnName(2).toString();
colnames[3] = selectCodes.invoiceTable.getColumnName(3).toString();
colnames[4] = selectCodes.invoiceTable.getColumnName(4).toString();
colnames[5] = selectCodes.invoiceTable.getColumnName(5).toString();

MyTableModel mod = new MyTableModel(refreshCartonCodesToTable, colnames);
selectCodes.invoiceTable = new JTable(mod);
selectCodes.invoiceTable.setVisible(true);

Custom model as shown below:

class MyTableModel extends DefaultTableModel {

    public MyTableModel(Object data[][], Object columnames[]) {
        super(data, columnames);
    }

    public Class getColumnClass(int col) {
        if (col == 5) {
            return Boolean.class;
        } else {
            return String.class;
        }
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        if (col == 0) //first column will be uneditable  
        {
            return false;
        } else {
            return true;
        }
    }
}

The table displays the columnames but the data is not diplayed. The array has data and the sample output is as shown below:

250VV  250VV0575W20140819  false  B1  19 August 2014  
250VV  250VV0561W20140819  false  B1  19 August 2014  
250VV  250VV0560W20140819  false  B1  19 August 2014  
250VV  250VV0559W20140819  false  B1  19 August 2014  
250VV  250VV0558W20140819  false  B1  19 August 2014

There are six columns. The sixth column I want to place a checkbox in the cells.

Can somebody help me please.

Here is the source code for CartonCodesToTable();

public static Object[][] CartonCodesToTable() {
        Object[][] array = null;
        try {
            dbutils.checker.connect_to_db_again_again();

            sqlcommand = "SELECT Product_ID, carton_code, scanned, batchno,date FROM carton_codes where scanned ='false' order by bno asc";
            rset = stmts.executeQuery(sqlcommand);
            int row = 0;
            while (rset.next()) {
                rset.last();
                row = rset.getRow();
            }

            array = new String[row][6];
            rset.beforeFirst();

            int x = 0;
            while (rset.next()) {
                array[x][0] = rset.getObject("Product_ID");
                array[x][1] = rset.getObject("carton_code");
                array[x][2] = rset.getObject("scanned");
                array[x][3] = rset.getObject("batchno");
                array[x][4] = rset.getObject("date");
                array[x][5] = false;
                x += 1;
            }
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e);
        }
        return array;
    }

When i use array[x][5] = false; i get an error 'java.lang.ArrayStoreException: java.lang.Boolean' So i decided to use array[x][5] = "false";

benzinect
  • 23
  • 6
  • Why do you want to return a java class? – Reporter Aug 27 '14 at 09:07
  • I want the last column to return boolean class so that a checkbox should be shown instead of printing true/ false – benzinect Aug 27 '14 at 09:09
  • In my eyes it is a strange code and I think the datatype of value is not compatible with the datatype that the component -to create a checkbox- need. I just recommend make it simple as nessacary and write 'true' or 'false' or try to convert it into primitiv boolean. – Reporter Aug 27 '14 at 09:28
  • So, you tell the JTable that the 6-th column is a boolean and should be rendered as a boolean (checkbox), and then pass "19 August 2014" as the value of said boolean? What exactly did you expect to happen there? – Ordous Aug 27 '14 at 09:47
  • 2
    Probably best to provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that we can copy,paste,compile,run and see the problem you're facing for ourselves – Paul Samsotha Aug 27 '14 at 09:50
  • Are you sure the `refreshCartonCodesToTable` array contains `Boolean`s in each row at index `5`? – icza Aug 27 '14 at 09:50
  • Ordous "19 August 2014" is the fifth column – benzinect Aug 27 '14 at 10:43
  • @benzinect Oh. Well then what is in the 6-th column of the input data? – Ordous Aug 27 '14 at 10:48
  • 1
    `isCellEditable()` can be reduced to `return col != 0`. – trashgod Aug 27 '14 at 11:48
  • In the sixth column i wanted to assign it boolean -false, since the array is of object. When i do this i get an error of java.lang.ArrayStoreException: java.lang.Boolean – benzinect Aug 27 '14 at 12:02
  • The best you can do is as @peeskillet said provide an MCVE, I have made a [question](http://stackoverflow.com/questions/17225988/how-to-add-jbutton-after-a-jtable) before, it's not exactly the same as yours, but there I'm providing an MCVE with the functionality you want. Check my code and compare it with yours. If after checking it you still have problems then consider peeskillet's link. – Frakcool Aug 27 '14 at 14:12

1 Answers1

0

You haven't provided an MCVE like I suggested, so there's hard to tell what's going on. First thing I see though is poor use of the ResultSet you don't need to do all those things. For example your use of rs.last().

boolean last() throws SQLException - Moves the cursor to the last row in this ResultSet object.

About ResultSet from API

A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable (See the API for an example how to do this)

So assuming you haven't make the ResultSet scrollable, that would explain you getting no results, as you have moved the cursor to the end with the call to rs.last()

That being said, you don't need to get the row count. Use a dynamic data structure to create the model instead. Just use a Vector. If you use an array for the data, DefaultTableModel will convert it to a Vector (under the hood) anyway.

A common approach is to make use of the ResultSetMetaData class and get the column count and create a Vector<Vector<Object>> dynamically and construct you DefaultTableModel that way. Something like:

public DefaultTableModel getModelFromResultSet(ResultSet rs) throws Exception {

    ResultSetMetaData md = rs.getMetaData();
    int columnCount = md.getColumnCount();

    String[] cols = new String[columnCount];
    for (int i = 1; i <= columnCount; i++) {
        col[i - 1] = md.getColumnName(i);
    }

    Vector<Vector<Object>> dataVector = new Vector<Vector<Object>>();
    while(rs.next()) {
        Vector<Object> row = Vector<Object>();
        for (int i = 1; i <= columnCount; i++) {
            row.add(rs.getObject(i));
        }
        dataVector.add(row);
    }

    DefaultTableModel model = new DefaultTableModel(dataVector, cols) {
        @Override
        public Class<?> getColumnClass(int column) {
            ...
        }
    };
    return model;
}

Or something like this. Haven't tested it (for any errors), but the basic concept is there.

As far as your ArrayStoreException, look at what you're doing

Object[][] array = null;
...
array = new String[row][6];

What's the point of doing this. You are making it every object has to be a String. Which may not be desirable for rendering.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720