Java JTable Column formatStringListToTable(List columnNames, List rows)

Here you can find the source of formatStringListToTable(List columnNames, List rows)

Description

Builds an HTML table from supplied arguments.

License

Open Source License

Parameter

Parameter Description
columnNames A list of column names.
rows A list of arrays representing column data for each row.

Return

See above.

Declaration

public static String formatStringListToTable(List<String> columnNames, List<String[]> rows) 

Method Source Code

//package com.java2s;
/*/*from ww w. ja v  a2 s.c o m*/
 * org.openmicroscopy.shoola.util.ui.UIUtilities
 *
 *------------------------------------------------------------------------------
 *  Copyright (C) 2006-2015 University of Dundee. All rights reserved.
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *------------------------------------------------------------------------------
 */

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Builds an HTML table from supplied arguments.
     *
     * Accepts a list of column names and a list of <code>String</code> arrays.
     * Each array forms the column values for a specific row in the table.
     * If the number of column names and elements in a row array won't match
     * the table will be rendered correctly nevertheless.
     *
     * @param columnNames A list of column names.
     * @param rows A list of arrays representing column data for each row.
     * @return See above.
     */
    public static String formatStringListToTable(List<String> columnNames, List<String[]> rows) {
        if (columnNames == null || rows == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("<table>");
        sb.append("<tr>");
        for (String column : columnNames) {
            sb.append("<th>" + column + "</th>");
        }
        sb.append("</tr>");

        for (String[] rowColumns : rows) {
            sb.append("<tr>");
            for (int i = 0; i < rowColumns.length; ++i) {
                sb.append("<td>");
                sb.append(rowColumns[i]);
                sb.append("</td>");
            }
            sb.append("</tr>");
        }
        sb.append("</table>");
        return formatToolTipText(sb.toString());
    }

    /** 
     * Builds a tool tip in a fixed font and color.
     * You pass the tool tip text and get back an <i>HTML</i> string to be
     * passed, in turn, to the <code>setToolTipText</code> method of a 
     * {@link javax.swing.JComponent}.
     *
     * @param toolTipText The textual content of the tool tip.
     * @return The String.
     */
    public static String formatToolTipText(String toolTipText) {
        return toolTipText;
    }

    /** 
     * Builds a tool tip in a fixed font and color.
     * You pass the tool tip text and get back an <i>HTML</i> string to be
     * passed, in turn, to the <code>setToolTipText</code> method of a 
     * {@link javax.swing.JComponent}.
     *
     * @param toolTipText     The textual content of the tool tip.
     * @return An <i>HTML</i> formatted string to be passed to 
     *          <code>setToolTipText()</code>.
     */
    public static String formatToolTipText(List<String> toolTipText) {
        if (toolTipText == null)
            return "";
        StringBuffer buf = new StringBuffer();
        buf.append("<html><body>");
        buf.append("<font face=Arial size=2>");
        Iterator<String> i = toolTipText.iterator();
        buf.append("<p>");
        while (i.hasNext()) {
            buf.append(i.next());
            buf.append("<br>");
        }
        buf.append("</p>");
        buf.append("</font></body></html>");
        return buf.toString();
    }

    /** 
     * Builds a tool tip in a fixed font and color.
     * You pass the tool tip text and get back an <i>HTML</i> string to be
     * passed, in turn, to the <code>setToolTipText</code> method of a 
     * {@link javax.swing.JComponent}.
     *
     * @param toolTipText     The textual content of the tool tip.
     * @return An <i>HTML</i> formatted string to be passed to 
     *          <code>setToolTipText()</code>.
     */
    public static String formatToolTipText(String[] toolTipText) {
        if (toolTipText == null)
            return "";
        StringBuffer buf = new StringBuffer();
        buf.append("<html><body>");
        buf.append("<font face=Arial size=2>");
        buf.append("<p>");
        for (int i = 0; i < toolTipText.length; i++) {
            buf.append(toolTipText[i]);
            buf.append("<br>");
        }
        buf.append("</p>");
        buf.append("</font></body></html>");
        return buf.toString();
    }
}

Related

  1. filterTable(JTable table, DefaultTableModel dtm, String text, int[] columns, boolean forceRegEx)
  2. findColumn(TableModel m, String columnName)
  3. findColumn(TableModel model, String name)
  4. fitColumns(JTable table)
  5. FitTableColumns(JTable myTable)
  6. getColumnData(final JTable table, final int iSelectedRow)
  7. getColumnIndex(JTable table, String columnTitle)
  8. getColumnIndexFromName(TableModel tableModel, String columnToRemove)
  9. getColumnNames(TableModel tableModel)