Example usage for javax.swing JTable moveColumn

List of usage examples for javax.swing JTable moveColumn

Introduction

In this page you can find the example usage for javax.swing JTable moveColumn.

Prototype

public void moveColumn(int column, int targetColumn) 

Source Link

Document

Moves the column column to the position currently occupied by the column targetColumn in the view.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int rows = 10;
    int cols = 5;
    JTable table = new JTable(rows, cols);

    table.moveColumn(table.getColumnCount() - 1, 0);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);

    table.getTableHeader().setReorderingAllowed(false);

    table.moveColumn(table.getColumnCount() - 1, 0);

}

From source file:com.sshtools.common.ui.PreferencesStore.java

/**
 *
 *
 * @param table/*from  w ww. j  a  va 2 s.  co m*/
 * @param pref
 * @param defaultWidths
 *
 * @throws IllegalArgumentException
 */
public static void restoreTableMetrics(JTable table, String pref, int[] defaultWidths) {
    //  Check the table columns may be resized correctly
    if (table.getAutoResizeMode() != JTable.AUTO_RESIZE_OFF) {
        throw new IllegalArgumentException("Table AutoResizeMode must be JTable.AUTO_RESIZE_OFF");
    }

    //  Restore the table column widths and positions
    for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
        try {
            table.moveColumn(table.convertColumnIndexToView(getInt(pref + ".column." + i + ".position", i)), i);
            table.getColumnModel().getColumn(i)
                    .setPreferredWidth(getInt(pref + ".column." + i + ".width",
                            (defaultWidths == null) ? table.getColumnModel().getColumn(i).getPreferredWidth()
                                    : defaultWidths[i]));
        } catch (NumberFormatException nfe) {
        }
    }
}

From source file:org.yccheok.jstock.gui.JTableUtilities.java

public static void setJTableOptions(JTable jTable, GUIOptions.JTableOptions jTableOptions) {
    if (jTableOptions.getColumnSize() <= 0) {
        // A bug introduced in version 1.0.5p. Due to incorrect
        // implementation of Utils.toXML, we may get a table option with 0
        // column turned on. To avoid all columns being turned off, we will
        // return early.
        return;/*from   w  ww . ja v a2 s  .  c  o m*/
    }

    final Locale locale = jTableOptions.getLocale();
    // HACKING!
    boolean first_column_hacking_required = false;

    // Remove unwanted column. MUST BE DONE FIRST!
    for (int i = 0; i < jTable.getColumnCount(); i++) {
        final String name = jTable.getColumnName(i);

        // In to avoid the following situation.
        // 1. GUIOptions is being saved when we are in Chinese locale.
        // 2. Application is restarted in English locale.
        // 3. We are trying to compare English wording with Chinese wording.
        //
        // Before performing comparison, we shall convert name, to the locale
        // of table options.
        final java.util.List<String> keys = getKeys(name, Locale.getDefault());

        // Ensure correct resource file is being loaded.
        // When ResourceBundle.getBundle(..., locale) is being called, the
        // system will try to search in the following sequence.
        // 1. gui_<locale>.properties.
        // 2. gui_<default_locale>.properties.
        // 3. gui.properties.
        final Locale oldLocale = Locale.getDefault();
        Locale.setDefault(locale);
        try {
            final ResourceBundle bundle = ResourceBundle.getBundle("org.yccheok.jstock.data.gui", locale);
            Locale.setDefault(oldLocale);

            // Try all the keys.
            boolean found = false;
            for (String key : keys) {
                final String translated_name = bundle.getString(key);
                if (jTableOptions.contains(translated_name)) {
                    found = true;
                    break;
                }
            }

            /* Remove any unwanted columns. */
            if (found == false) {
                // HACKING!
                // Some customers complain their first column of Watchlist,
                // or Portfolio are being hidden. I'm not sure why that
                // happen. YES! I have really no idea why that happen!
                // This is a hacking way "if (i > 0)" to prevent such 
                // problem. Shh...
                if (i > 0) {
                    removeTableColumn(jTable, name);
                    i--;
                } else {
                    first_column_hacking_required = true;
                }
            }
        } finally {
            Locale.setDefault(oldLocale);
        }
    }

    final int optionsCount = jTableOptions.getColumnSize();
    final int tableCount = jTable.getColumnCount();

    // HACKING!
    // jTableOptions doesn't have first column information if first_column_hacking_required
    // is true. When perform column moving, we will start from 2nd column if
    // first_column_hacking_required is true. Assume first column will always
    // stay in first column.
    int target = first_column_hacking_required ? 1 : 0;

    /* Sort the columns according to user preference. */
    for (int i = 0; i < optionsCount; i++) {
        final String name = jTableOptions.getColumnName(i);
        final java.util.List<String> keys = getKeys(name, locale);
        assert (keys != null);

        int index = -1;
        for (int j = 0; j < tableCount; j++) {
            // Try all the keys.
            boolean found = false;
            String translated_name = null;
            for (String key : keys) {
                translated_name = GUIBundle.getString(key);
                if (jTable.getColumnName(j).equals(translated_name)) {
                    found = true;
                    break;
                }
            }

            if (found) {
                /* Restore width. */
                jTable.getColumn(translated_name).setPreferredWidth(jTableOptions.getColumnWidth(i));

                index = j;
                break;
            }
        }

        if (index >= 0) {
            jTable.moveColumn(index, target++);
        }
    }
}

From source file:org.yccheok.jstock.gui.JTableUtilities.java

public static void insertTableColumnFromModel(JTable jTable, Object value, int clickedColumnIndex) {
    boolean isVisible = true;

    try {/* w ww .j a va2 s.  c  o  m*/

        TableColumn tableColumn = jTable.getColumn(value);

    }

    catch (java.lang.IllegalArgumentException exp) {

        isVisible = false;

    }

    if (isVisible)
        return;

    TableModel tableModel = jTable.getModel();

    final int modelIndex = getModelColumnIndex(jTable, value);

    Class c = tableModel.getColumnClass(modelIndex);

    TableColumn tableColumn = new javax.swing.table.TableColumn(modelIndex, 0, jTable.getDefaultRenderer(c),
            jTable.getDefaultEditor(c));

    jTable.addColumn(tableColumn);

    makeTableColumnWidthFit(jTable, jTable.getColumnCount() - 1, 5);

    // If we right clicked on the 3rd column, and select a new column, we
    // would like the new column to be inserted into 4th column. Note that,
    // clickedColumnIndex will be < 0, if we right clicked on empty area.
    if (clickedColumnIndex < 0) {
        // Have it in the last column when we right clicked on empty area.
        jTable.moveColumn(jTable.getColumnCount() - 1, jTable.getColumnCount() - 1);
    } else {
        // +1, as we want our newly inserted column to be at the right of
        // clicked column.
        jTable.moveColumn(jTable.getColumnCount() - 1,
                Math.min(jTable.getColumnCount() - 1, clickedColumnIndex + 1));
    }
}