Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


how to adjust jtable columns to fit the longest content in column cells using -'java,swing,jtable,jgoodies,tablecolumn'


how to adjust jtable columns to fit the longest content in column cells  using -'java,swing,jtable,jgoodies,tablecolumn'

I'm using answer http://stackoverflow.com/a/5820366 and http://tips4java.wordpress.com/2008/11/10/table-column-adjuster/ and it works, but frequently columns' sizes are too wide or too narrow.

No matter filling my table with HTML or text.

Using standard TableModel from oracle documentation.

Resize mode = JTable.AUTO_RESIZE_OFF

Container of my tabel is jGoodies:

FormLayout currentEventLayout = new FormLayout(
            "fill:p",
            "pref, pref");
PanelBuilder currentEventBuilder = new PanelBuilder(currentEventLayout);
currentEventBuilder.add(mainQuotesTable.getTableHeader(), constraints.xy(1, 1));
currentEventBuilder.add(mainQuotesTable, constraints.xy(1, 2));


HTML example:

"<html><pre><font size=+1 face='Arial'>" + firstValue + "\n" + secondValue + "</font></pre></html>"


simple row:

firstValue + " - " + secondValue


Here is the example:

public class TableAdjustExample {
private static JTable mainTable;
private static Random random = new Random();

private static List<Data> data;

private static class Data {
    String name;
    String surname;

    private Data(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }
}

public static void main(String[] args) {
    data = stubProvider();

    final JFrame frame = new JFrame("table adjust example");
    frame.add(createUI());
    frame.pack();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setSize(350, 400);
    frame.setVisible(true);

    update();
    java.util.Timer timer = new java.util.Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            update();
        }
    }, 3000, 3000);
}

private static JPanel createUI() {
    JPanel jPanel = new JPanel();
    mainTable = new JTable(2, 3);
    mainTable.setModel(new AbstractTableModel() {
        @Override
        public int getRowCount() {
            return data.size();
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Data dataItem = data.get(rowIndex);
            if (columnIndex == 0) {
                return dataItem.name;
            }
            if (columnIndex == 1) {
                return dataItem.surname;
            }
            throw new IllegalStateException();
        }
    });
    mainTable.setGridColor(Color.black);
    mainTable.setShowHorizontalLines(false);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    final TableCellRenderer defaultRenderer = mainTable.getTableHeader().getDefaultRenderer();
    mainTable.getTableHeader().setDefaultRenderer(new TableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable jTable, Object o, boolean b, boolean b1, int row, int column) {
            JLabel parent = (JLabel) defaultRenderer.getTableCellRendererComponent(jTable, o, b, b1, row, column);
            if (column == 0) {
                parent.setText("name");
            } else {
                parent.setText("surname");
            }
            return parent;
        }
    });

    jPanel.add(mainTable.getTableHeader());
    jPanel.add(mainTable);

    return jPanel;
}

private static void update() {
    System.out.println("updating");
    data = stubProvider();

    adjustJTableRowSizes(mainTable);
    for (int i = 0; i < mainTable.getColumnCount(); i++) {
        adjustColumnSizes(mainTable, i, 2);
    }
}

private static void adjustJTableRowSizes(JTable jTable) {
    for (int row = 0; row < jTable.getRowCount(); row++) {
        int maxHeight = 0;
        for (int column = 0; column < jTable.getColumnCount(); column++) {
            TableCellRenderer cellRenderer = jTable.getCellRenderer(row, column);
            Object valueAt = jTable.getValueAt(row, column);
            Component tableCellRendererComponent = cellRenderer.getTableCellRendererComponent(jTable, valueAt, false, false, row, column);
            int heightPreferable = tableCellRendererComponent.getPreferredSize().height;
            maxHeight = Math.max(heightPreferable, maxHeight);
        }
        jTable.setRowHeight(row, maxHeight);
    }

}

public static void adjustColumnSizes(JTable table, int column, int margin) {
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(column);
    int width;

    TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }
    JLabel comp = (JLabel) renderer.getTableCellRendererComponent(
            table, col.getHeaderValue(), false, false, 0, 0);
    width = comp.getPreferredSize().width;

    for (int r = 0; r < table.getRowCount(); r++) {
        renderer = table.getCellRenderer(r, column);
        comp = (JLabel) renderer.getTableCellRendererComponent(
                table, table.getValueAt(r, column), false, false, r, column);
        int currentWidth = comp.getPreferredSize().width;
        width = Math.max(width, currentWidth);
    }

    width += 2 * margin;

    col.setPreferredWidth(width);
}

private static List<Data> stubProvider() {
    List<Data> data = new ArrayList<Data>();
    for (int i = 0; i < 4; i++) {
        data.add(new Data(
                "<html>" +
                        "<div style='font-size: 15px'>Jason</div>" +
                        "<div style='font-size: 15px'>" + random.nextInt() + "</div>" +
                        "</html>",
                "Statham " + random.nextInt()));
    }
    return data;
}
}




I have such problem with row height adjustment. Using of <pre>\n</pre> instead of <br> fixed row adjustment.
    

asked Sep 7, 2015 by rajesh
0 votes
12 views



Related Hot Questions



Government Jobs Opening


...