jeudi 17 novembre 2016

Java Swing JTable - get right rowindex after sort

I've been stuck with a problem in Java. Image showing the Problem

The problem is: I have a JTable with 2 Columns: one is a Checkbox and the second one is a Integer (or String in my example). Whenever the User checks a Checkbox the Position Column will count a Number up, so the first checked Checkbox will get the Position '1', the second checked Checkbox will get the Position '2' and so on. When the User checks out one Checkbox with lets say the Position 5 out of 10 items, the rows with the Position 6 and up are going to get count one down.

This works but the Problem i got is that when the User checks multiple Checkboxes, then sorts the Table and tries to check Checkboxes, which will result in a screweing up the columns as seen in the Picture. In my Example Picture you can see additionally the 2 more Columns: the Column Row, which is given at the start, where the Table isnt Sorted and the Column RowIndexToModel, which shows the Position of the Rows but converted to the row inside the Vector, well i thought that but doesnt seem so.

Here is the tableChanged Methode:

            @Override
        public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();
            row = tbl.convertRowIndexToModel(row);
            if (e.getColumn() == 0) {
                TableModel model = (TableModel) e.getSource();
                Boolean checked = (Boolean) model.getValueAt(row, 0);//getting if boolean got checked or not
                if (checked) {
                    int maxCount = 0;
                    for (int i = 0; i < model.getRowCount(); i++) {
                        String curValue = (String)model.getValueAt(i, 1);
                        if(curValue != null && !curValue.equals("") && Integer.valueOf(curValue) > maxCount)
                            maxCount = Integer.valueOf(curValue); //get highest number in tbl
                    }
                    maxCount++;
                    model.setValueAt(String.valueOf(maxCount),row, 1); //set highest number + 1
                } else {
                    String columnString = (String)model.getValueAt(row, 1); //getting position
                    int tmpPos = Integer.valueOf(columnString);

                    for (int index = 0; index < model.getRowCount(); index++) {
                        int indexSorted = tbl.convertRowIndexToModel(index);
                        String curValue = (String)model.getValueAt(indexSorted, 1);
                        //get if current number is higher than unchecked value
                        if (curValue != null && !curValue.equals("") && Integer.valueOf(curValue) > tmpPos) {
                            int curValueToChange = Integer.valueOf(curValue) - 1;
                            model.setValueAt(String.valueOf(curValueToChange),indexSorted, 1); 
                        }
                    }
                    model.setValueAt("",row, 1); 
                }
            }
        }

PS:The code is written for this Example, so please dont wonder that it doesnt follow general Coding Standards. Sorry if my Code makes your Eyes bleed. ;)

Thanks for any help!




Aucun commentaire:

Enregistrer un commentaire