Results 1 to 2 of 2
  1. #1
    ssarvjeet is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default move multiple columns in jtable

    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.convertColumnIndexToVie w(modelIndex), viewIndex);
    jxtable.moveColumn(jxtable.convertColumnIndexToVie w(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);
    }
    }));

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: move multiple columns in jtable

    Although you have provided a lot of code, it doesn't appear to compile. Will you provide a SSCCE?

    Short, Self Contained, Correct Example

    -- Kaydell
    Java Coaching: Home

Similar Threads

  1. Fill jtable columns
    By victoryo in forum AWT / Swing
    Replies: 2
    Last Post: 01-11-2012, 06:28 PM
  2. Redisplay New JTable and Columns
    By jcman01 in forum AWT / Swing
    Replies: 2
    Last Post: 08-19-2011, 07:40 PM
  3. How to sort a JTable for multiple columns?
    By BLR in forum Advanced Java
    Replies: 2
    Last Post: 03-16-2009, 11:41 AM
  4. Right Align columns in JTable
    By Laura Warren in forum New To Java
    Replies: 2
    Last Post: 12-18-2008, 10:01 PM
  5. sort columns in jtable
    By Alan in forum AWT / Swing
    Replies: 2
    Last Post: 05-14-2007, 06:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •