0 Replies Latest reply: Sep 28, 2013 10:49 AM by user11472199 RSS

    move multiple columns in jtable

    user11472199

      I have multiple tab and each tab have a table with multiple columns. I move like this for example-

      col1 col2 col3 col4 col5 col6 col7 col8 col9

       

       

      after columns move

       

       

      col2 col1 col3 col5 col4 col6 col8 col7 col9

       

       

      I save table and get it, it works fine. But when i move columns like this-

       

       

      col4 col3 col5 col1 col6 col2 col7 col8 col9

       

       

      it never work as i save and get table. please tell what's wrong in below code.

       

       

      here are the codes-

       

       

      Table save code-

       

       

      try {

       

       

        Map<String, Map<String, Vector<Object>>> tableMap = new HashMap<String, Map<String, Vector<Object>>>();

       

       

        private static Map<String, Object> tables = new HashMap<String, Object>();

       

       

        Set<String> tabNames = tables.keySet();

       

       

        for (String tabName : tabNames) {

       

       

        JTable tab = (JTable) tables.get(tabName);

       

       

        Map<String, Vector<Object>> colNameValues = new HashMap<String, Vector<Object>>();

        Enumeration<TableColumn> tabCols = tab.getColumnModel()

        .getColumns();

        while (tabCols.hasMoreElements()) {

       

       

        Vector<Object> colums = new Vector<Object>();

       

       

        TableColumn col = tabCols.nextElement();

       

       

        int modelIndex = col.getModelIndex();

       

       

        int viewIndex = tab.convertColumnIndexToView(modelIndex);

        String field = Utils.toString(col.getIdentifier());

        Integer width = col.getWidth();

       

       

        if (isTableSelected) {

        boolean visible = !(viewIndex == -1 || col.getWidth() == 0);

        colums.add(visible); // pos 0 visible

        colums.add(field); // pos 1 field

        colums.add(width); // pos 2 width

        colums.add(modelIndex); // pos 3 model index

        colums.add(tab.convertColumnIndexToView(modelIndex)); // pos

        // 4

        // view

        // index

        }

        colNameValues.put(field, colums);

        }

       

       

        tableMap.put(tabName, colNameValues);

        }

       

       

        if (dataStore.isColumnsSelected()) {

        if (personalizedDataStore != null) {

        Map<String, Map<String, Vector<Object>>> mapOld = personalizedDataStore

        .getInstructionTable();

        if (mapOld != null) {

        if (mapOld.size() > 0) {

        Set<String> oldTabNames = mapOld.keySet();

        for (String oldTabName : oldTabNames) {

        if (!tableMap.containsKey(oldTabName)) {

        tableMap.put(oldTabName,

        mapOld.get(oldTabName));

        }

        }

        }

        }

        }

       

       

        dataStore.setInstructionTable(tableMap);

       

       

       

       

      code for get Table-

       

       

      private void applyPesonalizeSettings(String tabName, JTable tab) {

       

       

        if (!" ".equals(tabName)) {

       

       

        if (personalizedDataStore != null) {

       

       

        Map<String, Map<String, Vector<Object>>> oldTables = personalizedDataStore

        .getInstructionTable();

        for (int viewIndex = 0, count = tab.getRowCount(); viewIndex < count; viewIndex++) {

       

       

        if (oldTables != null) {

        Map<String, Vector<Object>> oldTabCols = oldTables

        .get(tabName);

        Enumeration<TableColumn> tableColumns = tab

        .getColumnModel().getColumns();

       

       

        while (tableColumns.hasMoreElements()) {

        TableColumn col = tableColumns.nextElement();

       

       

        String field = Utils.toString(col.getIdentifier());

       

       

        if (oldTabCols != null) {

        if (oldTabCols.containsKey(field)) {

       

       

        boolean vis = (Boolean) oldTabCols.get(field)

        .get(0);

       

       

        int prefWidth = (Integer) oldTabCols.get(field)

        .get(2);

       

       

        int modelInd = (Integer) oldTabCols.get(field)

        .get(3);

        int viewInd = (Integer) oldTabCols.get(field)

        .get(4);

        // setting visible

        if (!vis) {

        tab.getColumn(field).setMinWidth(0);

        tab.getColumn(field).setMaxWidth(0);

        tab.getColumn(field).setPreferredWidth(0);

       

       

        } else {

        // setting column width

        tab.getColumn(field).setPreferredWidth(

        prefWidth);

        }

        // setting index

       

       

        if (modelInd != viewInd) {

        tab.moveColumn(

        tab.convertColumnIndexToView(modelInd),

        viewInd);

       

       

        }

       

       

        }

        }

        }

        }

        }

        }

       

       

        }

        }

       

       

       

      here is the code for set table change property with popup-

      @Override

        protected void doOK() {

       

        Map<String, Vector<Object>> labelAndRow = new HashMap<String, Vector<Object>>();  //req 22

       

        for (int viewIndex = 0, count = super.getRowCount(); viewIndex < count; viewIndex++) {

        Vector<Object> row = super.getRow(viewIndex);

       

        boolean visible = (Boolean) row.get(0);

        String label = (String) row.get(1);

        String field = (String) row.get(2);

        labelAndRow.put(field,row); //req 22

       

        Integer modelIndex = (Integer) row.get(3);

        Integer width = (Integer) row.get(4);                              

        TableColumn col = jxtable.getColumn(field);

       

       

       

       

        if (visible) {

        if (width <= 0) {

        width = 75;

        }

       

        if (width != col.getWidth()) {

        col.setMinWidth(15);

        col.setMaxWidth(1000);

        col.setPreferredWidth(width);

        }

        } else {

        col.setMinWidth(0);

        col.setMaxWidth(0);

        col.setWidth(0);

        col.setPreferredWidth(0);

        }

       

       

        col.setHeaderValue(label);

       

        jxtable.moveColumn(jxtable.convertColumnIndexToView(modelIndex), viewIndex);

        jxtable.moveColumn(jxtable.convertColumnIndexToView(modelIndex), viewIndex);

        }

       

        super.dispose();

        }

        };

       

       

        for (TableColumn col : (List<TableColumn>) jxtable.getColumns()) {

        int modelIndex = col.getModelIndex();

        int viewIndex = jxtable.convertColumnIndexToView(modelIndex);

       

       

        String label = Utils.toString(col.getHeaderValue());

        String field = Utils.toString(col.getIdentifier());

        Integer width = col.getWidth();                       

        boolean visible = !(viewIndex == -1 || col.getWidth() == 0);

       

       

        dialog.getTableModel().addRow(new Object[]{visible, label, field, modelIndex, width});

        }

       

       

        dialog.setVisible(true);

        }

        }));