3

I need to get an appropriate table size according to a text in a header. It contains abbreviations of czech names of days like: "Po", "Út", "St", atc. but instead of this three dots are displayed.

I have this code:

width, height - max of all minimum row/column sizes
allWidth,allHeight - should be the total width, height of the whole table

//GET DIMENSION
int width = 0;
int height = 0;
int allWidth, allHeight;
for (int col = 0; col < table.getColumnCount(); col++) {        
    TableColumn tableColumn = table.getTableHeader().getColumnModel().getColumn(col);
    TableCellRenderer renderer = tableColumn.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }
    Component component = renderer.getTableCellRendererComponent(table,
            tableColumn.getHeaderValue(), false, false, -1, col);
    width = Math.max(component.getPreferredSize().width, width);
    table.getColumnModel().getColumn(col).setPreferredWidth(width);
}
allWidth = table.getColumnCount() * width;
for (int row = 0; row < table.getRowCount(); row++) {
    TableCellRenderer renderer = table.getCellRenderer(row, 0);
    Component comp = table.prepareRenderer(renderer, row, 0);
    height = Math.max(comp.getMinimumSize().height, height);
}
allHeight = table.getRowCount() * height;

//HERE I SET WIDTHS AND HEIGHTS
table.setRowHeight(height);
table.setMinimumSize(new Dimension(allWidth, allHeight));
scrollPane.setMinimumSize(new Dimension(allWidth, allHeight));

I'm using GridBagLayout with this settings, I think this is ok:

this.setBorder(BorderFactory.createTitledBorder("Calendar"));
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 1;   //position x=0, y=0 is for JSpinners, months and years
gbc.weighty = 0;
gbc.weightx = 0;
add(scrollPane,gbc);//add table in a custom Calendar component

Also I changed the look and feel and this is causing those three dots. Otherwise the names are correctly displayed.

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
}

And this is how I initialize JTable and JScrollPane, I have a feeling like this could be ok too.

    setTable(new JTable());
    getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    getTable().setAutoscrolls(false);
    getTable().getTableHeader().setResizingAllowed(false);
    getTable().getTableHeader().setReorderingAllowed(false);
    getTable().setColumnSelectionAllowed(true);
    getTable().setRowSelectionAllowed(true);
    getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    tableModel = new CalendarTableModel();  //my class extended from AbstractTableModel
    getTable().setModel(tableModel);
    scrollPane = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setAutoscrolls(false);

Problems:
If I don't set the scrollPane with gbc to resize itself, it gets the minimum possible size unless I calculate and set the minimum sizes of table and scrollPane by hand. (the left picture)

Using this settings, I got the result in the middle pic, but also too small. (no names of days and the height is also somewhat smaller.)
I don't know of any other better approach to do this, nothing I've found helped me so far.

When I considered some padding, e.g 2 pixels and adjusted the calculation of allWidth,allHeight like this:
allHeight = (1+table.getRowCount()+pady) * height; //plus one line of header
allWidth = table.getColumnCount() * width;

I got the picture on the right.

almost no table still too small didnt helped

Here is a simple complete compilable class that demonstrates the problem:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class Main {
    public static void main(String[] args) {
        new Main().start();
    }

    private void start() {
        JFrame f = new JFrame();
        f.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        initTable();

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        }

        JPanel calendar = new JPanel();
        calendar.setLayout(new GridBagLayout());
        calendar.setBorder(BorderFactory.createTitledBorder("Calendar"));
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weighty = 0;
        gbc.weightx = 0;
        calendar.add(scrollPane, gbc);

        f.add(calendar,gbc);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    JTable table;
    JScrollPane scrollPane;
    CalendarTableModel tableModel;

    private void initTable() {
        setTable(new JTable());
        getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        getTable().setAutoscrolls(false);
        getTable().getTableHeader().setResizingAllowed(false);
        getTable().getTableHeader().setReorderingAllowed(false);
        getTable().setColumnSelectionAllowed(true);
        getTable().setRowSelectionAllowed(true);
        getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        tableModel = new CalendarTableModel();  //my class extended from AbstractTableModel
        getTable().setModel(tableModel);
        scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setAutoscrolls(false);

        //GET DIMENSION
        int width = 0;
        int height = 0;
        int allWidth, allHeight;
        int padx = 2,pady = 2;
        for (int col = 0; col < table.getColumnCount(); col++) {
            TableColumn tableColumn = table.getTableHeader().getColumnModel().getColumn(col);
            TableCellRenderer renderer = tableColumn.getHeaderRenderer();
            if (renderer == null) {
                renderer = table.getTableHeader().getDefaultRenderer();
            }
            Component component = renderer.getTableCellRendererComponent(table,
                    tableColumn.getHeaderValue(), false, false, -1, col);
            width = Math.max(component.getPreferredSize().width, width);
            table.getColumnModel().getColumn(col).setPreferredWidth(width);
        }
        allWidth = (table.getColumnCount()+padx) * width;
        for (int row = 0; row < table.getRowCount(); row++) {
            TableCellRenderer renderer = table.getCellRenderer(row, 0);
            Component comp = table.prepareRenderer(renderer, row, 0);
            height = Math.max(comp.getMinimumSize().height, height);
        }
        allHeight = (1+table.getRowCount()+pady) * height;

//HERE I SET WIDTHS AND HEIGHTS
        table.setRowHeight(height);
        table.setMinimumSize(new Dimension(allWidth, allHeight));
        scrollPane.setMinimumSize(new Dimension(allWidth, allHeight));
    }

    private void setTable(JTable jTable) {
        this.table = jTable;
    }

    private JTable getTable() {
        return this.table;
    }

    private class CalendarTableModel extends AbstractTableModel {
        private String[] daysData = {"Po", "Út", "St", "Čt", "Pá", "So", "Ne"};
        private int[][] values;

        public CalendarTableModel() {
            values = new int[7][6];
            for (int i = 0; i < 6; i++) {
                for (int j = 0; j < 7; j++) {
                    values[j][i] = 7;
                }
            }
        }

        @Override
        public int getRowCount() {
            return 6;
        }

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

        @Override
        public String getColumnName(int column) {
            return daysData[column];
        }

        @Override
        public Object getValueAt(int row, int column) {
            return this.values[column][row];
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    }
}
Daniel Katz
  • 2,271
  • 3
  • 25
  • 27
  • Table columns also have padding take take into consideration of the grid lines. Try adding something like 2 or 4 to your width calculations... – MadProgrammer Aug 22 '13 at 11:10
  • ok, i'll try it.. I thought it is something that I could get from some method. – Daniel Katz Aug 22 '13 at 11:12
  • Perhaps I should edit the question to add another picture. – Daniel Katz Aug 22 '13 at 11:18
  • Instead of using setMin/pref size, take a look at getPreferredScrollableViewportSize – MadProgrammer Aug 22 '13 at 11:18
  • 2
    table.setPreferredScrollableViewportSize(table.getPreferredSize()); – mKorbel Aug 22 '13 at 11:22
  • 1
    Perhaps I should edit the question to add another picture., for better help sooner post an SSCCE, short, runable, compilable, just about JFrame with JTable in JScrollPane, and hardcoded value for XxxTableModel demonstrated a.m. issue – mKorbel Aug 22 '13 at 11:25
  • I forgot to mention that I changed look and feel. I already have the compilable code in one simple class. – Daniel Katz Aug 22 '13 at 12:08
  • @mKorbel Where should I post it? – Daniel Katz Aug 22 '13 at 12:16
  • edit your question with runnable, compilable code contains main class – mKorbel Aug 22 '13 at 12:42
  • @mKorbel I've done it. I have a half solution and anybody can use it to answer me. But I don't understand why it is so, that I need to change the look and feel before I create any instance of frame. You can easily try to putthe changing LAF as the first command. This still doesn't help me to set the appropriate size because using padding like 2 pixels or 1 only either crops the table or creates too big 'some' border. – Daniel Katz Aug 22 '13 at 12:53
  • *"I've done it."* Done what? Where is the SSCCE? – Andrew Thompson Aug 22 '13 at 13:23
  • 1
    the main problem is that you confuse every collaborator with setting the LAF _after_ instantiating/configuring the components. – kleopatra Aug 22 '13 at 13:44
  • I am sorry, in my project there was a similar problem that I didn't realize, so I had to leave it on the wrong place so that the problem remains. If I didn't, half of the problem would be solved. When I posted the SSCCE I pointed out this problem. – Daniel Katz Aug 22 '13 at 16:17

3 Answers3

7

you can to get this one

enter image description here

by

  • f.pack() before f.setVisible(true);

  • desired widht should be at 22 / 23 pixelx but component.getPreferredSize() or SwingUtilities#computeStringWidth(FontMetrics fm, String str) returns only 17 / 18 and plus table.getIntercellSpacing().width only one pixel for Po - Ne

  • note real workaround could be based on TableColumnAdjuster by @camickr

from code

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.JTextComponent;

public class Main {

    public static void main(String[] args) {
        new Main().start();
    }

    private void start() {
        JFrame f = new JFrame();
        f.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        initTable();

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        }

        JPanel calendar = new JPanel();
        calendar.setLayout(new GridBagLayout());
        calendar.setBorder(BorderFactory.createTitledBorder("Calendar"));
        gbc.fill = GridBagConstraints.NONE;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weighty = 0;
        gbc.weightx = 0;
        calendar.add(scrollPane, gbc);
        f.add(calendar, gbc);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
    JTable table;
    JScrollPane scrollPane;
    CalendarTableModel tableModel;

    private void initTable() {
        setTable(new JTable());
        getTable().setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        getTable().setAutoscrolls(false);
        getTable().getTableHeader().setResizingAllowed(false);
        getTable().getTableHeader().setReorderingAllowed(false);
        getTable().setColumnSelectionAllowed(true);
        getTable().setRowSelectionAllowed(true);
        getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        tableModel = new CalendarTableModel();  //my class extended from AbstractTableModel
        getTable().setModel(tableModel);
        scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setAutoscrolls(false);

        TableColumnModel columnModel = table.getColumnModel();
        for (int col = 0; col < table.getColumnCount(); col++) {
            int maxWidth = 0;
            for (int row = 0; row < table.getRowCount(); row++) {
                TableCellRenderer rend = table.getCellRenderer(row, col);
                Object value = table.getValueAt(row, col);
                Component comp = rend.getTableCellRendererComponent(table, value, false, false, row, col);
                maxWidth = Math.max(comp.getPreferredSize().width, maxWidth);
            }
            TableColumn column = columnModel.getColumn(col);
            TableCellRenderer headerRenderer = column.getHeaderRenderer();
            if (headerRenderer == null) {
                headerRenderer = table.getTableHeader().getDefaultRenderer();
            }
            Object headerValue = column.getHeaderValue();
            Component headerComp = headerRenderer.getTableCellRendererComponent(table, headerValue, false, false, 0, col);
            maxWidth = Math.max(maxWidth, headerComp.getPreferredSize().width);
            // note some extra padding
            column.setPreferredWidth(maxWidth + 6);//IntercellSpacing * 2 + 2 * 2 pixel instead of taking this value from Borders
        }
        DefaultTableCellRenderer stringRenderer = (DefaultTableCellRenderer) table.getDefaultRenderer(String.class);
        stringRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
    }

    private void setTable(JTable jTable) {
        this.table = jTable;
    }

    private JTable getTable() {
        return this.table;
    }

    private class CalendarTableModel extends AbstractTableModel {

        private String[] daysData = {"Pondelok", "Út", "St", "Čt", "Pá", "Sobota", "Ne"};
        private int[][] values;

        public CalendarTableModel() {
            values = new int[7][6];
            for (int i = 0; i < 6; i++) {
                for (int j = 0; j < 7; j++) {
                    values[j][i] = 30;
                }
            }
        }

        @Override
        public int getRowCount() {
            return 6;
        }

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

        @Override
        public String getColumnName(int column) {
            return daysData[column];
        }

        @Override
        public Object getValueAt(int row, int column) {
            return this.values[column][row];
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

This is fantastic code to auto-fit your header and cell content in the JTable:

jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF );

for (int column = 0; column < jTable1.getColumnCount(); column++){
    TableColumn tableColumn = jTable1.getColumnModel().getColumn(column);
    int preferredWidth = tableColumn.getMinWidth();
    int maxWidth = 0;
    TableCellRenderer rend = jTable1.getTableHeader().getDefaultRenderer();
    TableCellRenderer rendCol = tableColumn.getHeaderRenderer();
    if (rendCol == null) rendCol = rend;
    Component header = rendCol.getTableCellRendererComponent(jTable1, tableColumn.getHeaderValue(), false, false, 0, column);
    maxWidth = header.getPreferredSize().width;
    System.out.println("maxWidth :"+maxWidth);

    for (int row = 0; row < jTable1.getRowCount(); row++){
        TableCellRenderer cellRenderer = jTable1.getCellRenderer(row, column);
        Component c = jTable1.prepareRenderer(cellRenderer, row, column);
        int width = c.getPreferredSize().width + jTable1.getIntercellSpacing().width;
        preferredWidth = Math.max(preferredWidth, width);
        System.out.println("preferredWidth :"+preferredWidth);
        System.out.println("Width :"+width);

        //  We've exceeded the maximum width, no need to check other rows

        if (preferredWidth <= maxWidth){
            preferredWidth = maxWidth;
            break;
        }
    }
    tableColumn.setPreferredWidth(preferredWidth);
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

I came to solution :-)

I had to change the look and feel before I calculated appropriate column widths. When I did it the other way around, the calculation was out of date.

So I only needed to set the correct widths like this:

table.getColumnModel().getColumn(col).setPreferredWidth(width);

And then change the size of the viewPort like this, because the table's prefered size is now correct.

table.setPreferredScrollableViewportSize(table.getPreferredSize());

Thanks! ;)

Daniel Katz
  • 2,271
  • 3
  • 25
  • 27
  • In my application the problem remained even though I changed the LAF before the calculation. I had to put the LAF out of a constructor of the JFrame, before it. – Daniel Katz Aug 22 '13 at 14:15
  • _had to put the LAF out of a constructor of the JFrame_ sounds like you are extending JFrame - that's **wrong** most of the time. Instead, _use_ a frame, as you do in your SSCCE – kleopatra Aug 22 '13 at 15:08