CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Hybrid View

  1. #1
    Guest

    How to set Font in JTable

    Hi,
    I need to set font for a particular column. I did the foll:

    TableColumn col0 = colmodel.getColumn(0);
    DefaultTableCellRenderer cellrenderer = new DefaultTableCellRenderer();
    cellrenderer.setFont(new Font("Arial", Font.BOLD, 12));
    col0.setCellRenderer(cellrenderer);
    cellrenderer.repaint();

    But this didn't work. The column font doesnt change at all at run time.

    Any ideas??? Someone must have done this already.




  2. #2
    Guest

    Re: How to set Font in JTable

    Hi there,

    I have the same problem you are facing.
    You try to change the font of the cell in column.
    I try to change the font of the cell in header.
    I was told to call the fireTableChanged from the TableModel of the JTable.
    I am still working on it.

    I wish I could be more helpful ...

    Good luck.



  3. #3
    Join Date
    Aug 2008
    Posts
    1

    Re: How to set Font in JTable

    I realize this thread is real old, but maybe this will help someone else.
    calling cellrenderer.setFont(...) doesn't work. Don't know why.
    If you want to set the font for a particular column you can subclass DefaultTableCellRenderer and override its getTableCellRendererComponent(...) method, then set your column renderer to use your subclass.
    Something like this:
    <code>
    DefaultTableCellRenderer rdr = new DefaultTableCellRenderer() {
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column) {
    if (value == null)
    return super.getTableCellRendererComponent(table,
    value, isSelected, hasFocus, row, column);
    JLabel label = new JLabel(value.toString());
    label.setFont(new Font("Helvetica Bold", Font.ITALIC,12));
    return label;
    }
    };

    tableColumn.setCellRenderer(rdr);
    </code>

  4. #4
    Join Date
    Apr 2007
    Posts
    442

    Re: How to set Font in JTable

    tnjed111 is on te right track. However a few additions.

    DefaultTableCellRenderer is in its self a JLabel, so you have no need to create another one in the method. All you can do to JLabel you can reference through this in a DefaulTableCellRenderer.

    Also, in most cases, especially if your primary interest is only on changing a font. You do want to call super method. Have it handle to focused, selected etc, and you then override the changes where appropriate. This way the table cell will be different where wanted, else consistent with the look of other cells.

    To set font etc. to the columns header, you find the right TableColumn and call setHeaderRenderer(...) on that TableColumn. Incidentally, if you want to specify a renderer for all cell of a column, call setCellRenderer(...) on that column.

    You get the TableColumns from tables column model (aka. TableColumnModel), by calling getColumn(...)

  5. #5
    Join Date
    Dec 2009
    Posts
    4

    Re: How to set Font in JTable

    After reading through the preceding suggestions and struggling with getting the table to do what I wanted, I found something that works well, and has the advantage of efficiency. The whole thing was such a pain that I just had to sign up here and give something back.

    Having a cell renderer which extends DefaultTableCellRenderer is the right thing to do, but having it return a custom made JLabel for the cells is not. DefaultTableCellRenderer itself extends JLabel, but has important changes to improve the speed of the table display. Returning a JLabel undoes all of that. It also loses all the logic for setting the cell colors differently for selected or focused cells.

    You might think that simply calling setFont(Font customFont) on the instance of the
    DefaultTableCellRenderer already used by the JTable would do what you want, but it does not.

    However, if you extend the class and override Font getFont() to return the font you want to use, everything works great, and you don't lose any of the speed or color logic.

    import javax.swing.JTable;
    public class MyTable extends JTable {
    public MyTable(Font customFont) {
    FontMetrics metrics = getFontMetrics(customFont);
    int fontHeight = metrics.getHeight();
    setRowHeight(fontHeight);

    StringCellRenderer stringCellRenderer = new StringCellRenderer(customFont);
    NumberCellRenderer numberCellRenderer = new NumberCellRenderer(customFont);

    // assume you added some columns here
    TableColumn someStringColumn columnModel.getColumn(0);
    TableColumn someNumberColumn columnModel.getColumn(1);

    someStringColumn.setCellRenderer(stringCellRenderer);
    someNumberColumn.setCellRenderer(numberCellRenderer);
    }
    }

    Use this renderer for columns displaying strings, or values which should be left-justifed:

    import javax.swing.table.DefaultTableCellRenderer;
    public class StringCellRenderer extends DefaultTableCellRenderer {

    private Font customFont;

    public StringCellRenderer(Font customFont) {
    this.customFont = customFont;
    setFont(customFont);
    }

    @Override
    public Font getFont() {
    return customFont;
    }
    }

    Use this class for columns displaying numbers, or values which should be right-justified:

    import javax.swing.table.DefaultTableCellRenderer;
    public class NumberCellRenderer extends StringCellRenderer {

    public NumberCellRenderer(Font customFont) {
    super(customFont);
    setHorizontalAlignment(SwingConstants.RIGHT);
    }
    }

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How to set Font in JTable

    Quote Originally Posted by eleutieri View Post
    Having a cell renderer which extends DefaultTableCellRenderer is the right thing to do, but having it return a custom made JLabel for the cells is not. DefaultTableCellRenderer itself extends JLabel, but has important changes to improve the speed of the table display. Returning a JLabel undoes all of that. It also loses all the logic for setting the cell colors differently for selected or focused cells.
    Well quite - that's why Londbrok took the trouble to explain how it should be done - he assumed people knew to override the getTableCellRendererComponent(..) method.

    You might think that simply calling setFont(Font customFont) on the instance of the DefaultTableCellRenderer already used by the JTable would do what you want, but it does not.
    Because the renderer instance font (and other properties) is reset in the getTableCellRendererComponent(..) method for every cell. You have to change it after that method call, which means overriding the call, which means providing your own TableCellRenderer.

    However, if you extend the class and override Font getFont() to return the font you want to use, everything works great, and you don't lose any of the speed or color logic.
    That's fine if you want to set a single font for the whole table, or for a single column, you can have a renderer for each, but you lose all the information supplied by the table to the renderer for each cell. How do you change the font for a specific cell or a specific value, or depending on the focus or selected property of the cell?

    The getTableCellRendererComponent method is there to be overridden to do custom rendering. See Using Custom Renderers.

    The simplest and most flexible way to set the font in a table is to override the DefaultTableCellRenderer like this:
    Code:
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
        // override renderer preparation
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                       boolean hasFocus,
                                                       int row, int column)
        {
            // allow default preparation
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
            // replace default font
            setFont(new Font("Helvetica Bold", Font.ITALIC, 22));
            return this;
        }
    };
    This method has the table, the cell value, the cell selection and focus, and the cell row and column all in context, so you can use them all to decide whether to change the font and what font to use.

    Purists might want to do it like this (i.e. assuming no knowledge of the superclass implementation):
    Code:
    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
        // override renderer preparation
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                       boolean hasFocus,
                                                       int row, int column)
        {
            // allow default preparation
            Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
            // replace default font
            comp.setFont(new Font("Helvetica Bold", Font.ITALIC, 22));
            return comp;
        }
    };
    The cheapest, fastest, and most reliable components of a computer system are those that aren't there...
    G. Bell
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured