Java JTable Column getMultiLineColumnNames(String[] ret)

Here you can find the source of getMultiLineColumnNames(String[] ret)

Description

get Multi Line Column Names

License

Open Source License

Declaration

public static void getMultiLineColumnNames(String[] ret) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 Open Door Logistics (www.opendoorlogistics.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at http://www.gnu.org/licenses/lgpl.txt
 ******************************************************************************/

import javax.swing.table.TableModel;

public class Main {
    public static void getMultiLineColumnNames(String[] ret) {
        // count max number of lines
        int maxNbLines = 1;
        for (int i = 0; i < ret.length; i++) {
            String s = ret[i];/* w  ww  .  j a  v a2 s  . com*/

            // ensure number of spaces should equal numbers of words-1
            s = s.trim();
            s = s.replaceAll("  ", " ");
            ret[i] = s;

            // split 
            maxNbLines = Math.max(ret[i].split(" ").length, maxNbLines);
        }

        // split each one
        for (int i = 0; i < ret.length; i++) {
            String[] split = ret[i].split(" ");
            StringBuilder builder = new StringBuilder();
            builder.append("<html>");
            for (int line = 0; line < maxNbLines; line++) {
                if (line > 0) {
                    builder.append("<br>");
                }
                if (line < split.length) {
                    builder.append(split[line]);
                }
                ret[i] = builder.toString();
            }
        }

    }

    /**
     * Get column names properly formatted for multi-line.
     * See http://www.javarichclient.com/multiline-column-header/ for details
     * @param model
     * @return
     */
    public static String[] getMultiLineColumnNames(TableModel model) {
        String[] ret = new String[model.getColumnCount()];

        for (int i = 0; i < ret.length; i++) {
            ret[i] = model.getColumnName(i);
        }

        getMultiLineColumnNames(ret);

        return ret;
    }
}

Related

  1. getColumnIndex(JTable table, String columnTitle)
  2. getColumnIndexFromName(TableModel tableModel, String columnToRemove)
  3. getColumnNames(TableModel tableModel)
  4. getColumnOrder(final JTable table)
  5. getColumns(TableColumnModel columnModel)
  6. getRealColumnPos(int colPos, JTable table)
  7. getRenderedComponentAt(JTable table, int row, int column)
  8. getTableColumn(JTable table, int columnIndex)
  9. hideColumn(JTable table, int columnIndex)