JTable and optimal column width.

Discussion in 'Java' started by Christian-Josef Schrattenthaler, Jul 23, 2006.

  1. Hi!

    I have a JTable which has to columns filled over an 'String[][]'. Is it
    possible, to set the cell width to the longest value in an column?

    I found a solution with String.lengt() over Google, but this doesn't work
    correctly because the value of String.length() doesn't fit exactly. I think
    this is because the proportional fonts.

    Greetings,
    Christian.
     
    Christian-Josef Schrattenthaler, Jul 23, 2006
    #1
    1. Advertisements

  2. Christian-Josef Schrattenthaler wrote:
    >
    > I have a JTable which has to columns filled over an 'String[][]'. Is it
    > possible, to set the cell width to the longest value in an column?


    Briefly having a look at some of my old code, I've used
    JTable.prepareRenderer on TableColumn.getCellRenderer. Then it's a
    matter of setting the column preferred width to the maximum of the
    preferred widths given by the renderer component. You probably want to
    consider the header as well.

    > I found a solution with String.lengt() over Google, but this doesn't work
    > correctly because the value of String.length() doesn't fit exactly. I think
    > this is because the proportional fonts.


    String length will give you the number of characters in the text. If you
    set the width to one pixel per character, it's probably going to be a
    tad short.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jul 23, 2006
    #2
    1. Advertisements

  3. Christian-Josef Schrattenthaler

    IchBin Guest

    Christian-Josef Schrattenthaler wrote:
    > Hi!
    >
    > I have a JTable which has to columns filled over an 'String[][]'. Is it
    > possible, to set the cell width to the longest value in an column?
    >
    > I found a solution with String.lengt() over Google, but this doesn't work
    > correctly because the value of String.length() doesn't fit exactly. I think
    > this is because the proportional fonts.
    >
    > Greetings,
    > Christian.

    If I understand your problem. Here is a method that will expand the
    columns to the largest cell\column..

    public void calcColumnWidths(JTable table)
    {
    JTableHeader header = table.getTableHeader();
    TableCellRenderer defaultHeaderRenderer = null;

    if (header != null)
    defaultHeaderRenderer = header.getDefaultRenderer();

    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();
    int margin = columns.getColumnMargin(); // only JDK1.3
    int rowCount = data.getRowCount();
    int totalWidth = 0;

    for (int i = columns.getColumnCount() - 1; i >= 0; --i)
    {
    TableColumn column = columns.getColumn(i);
    int columnIndex = column.getModelIndex();
    int width = -1;

    TableCellRenderer h = column.getHeaderRenderer();

    if (h == null)
    h = defaultHeaderRenderer;

    if (h != null) // Not explicitly impossible
    {
    Component c = h.getTableCellRendererComponent(table, column
    .getHeaderValue(), false, false, -1, i);
    width = c.getPreferredSize().width;
    }

    for (int row = rowCount - 1; row >= 0; --row)
    {
    TableCellRenderer r = table.getCellRenderer(row, i);
    Component c = r.getTableCellRendererComponent(table, data
    .getValueAt(row, columnIndex), false, false,
    row, i);
    width = Math.max(width, c.getPreferredSize().width);
    }

    if (width >= 0)
    column.setPreferredWidth(width + margin); // <1.3:
    without margin
    else
    totalWidth += column.getPreferredWidth();
    }
    }

    Thanks in Advance...
    IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
    __________________________________________________________________________

    'If there is one, Knowledge is the "Fountain of Youth"'
    -William E. Taylor, Regular Guy (1952-)
     
    IchBin, Jul 23, 2006
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AndrewF
    Replies:
    1
    Views:
    1,054
    Bruce Barker
    Oct 10, 2005
  2. Rene Ruppert

    Set column width in JTable?

    Rene Ruppert, Oct 17, 2003, in forum: Java
    Replies:
    0
    Views:
    10,480
    Rene Ruppert
    Oct 17, 2003
  3. gajo
    Replies:
    2
    Views:
    27,789
    Pacific Guy
    Jun 25, 2004
  4. hilz
    Replies:
    0
    Views:
    3,670
  5. Karl Burrows

    Optimal Page Width?

    Karl Burrows, Sep 21, 2004, in forum: HTML
    Replies:
    51
    Views:
    3,047
    Daniel R. Tobias
    Sep 27, 2004
  6. ssk
    Replies:
    5
    Views:
    6,437
    Jukka K. Korpela
    Oct 30, 2006
  7. sonaljain
    Replies:
    0
    Views:
    1,249
    sonaljain
    Mar 16, 2011
  8. Carl Howarth

    DG Column Width vs Table Column Width

    Carl Howarth, Oct 22, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    469
    Eliyahu Goldin
    Oct 24, 2004
Loading...