CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2010
    Posts
    5

    Font is changing While editing cell in JTable - Need a fix

    Hi all,

    The font type of the cell is set to 'Arial Unicode MS'. While editing the cell during the runtime, the font of the cell is changed to the default font.

    Thanks.

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

    Re: Font is changing While editing cell in JTable - Need a fix

    How do you set the font type of the cell?

    JTable uses two different components to show cell contents - a TableCellRenderer to display the contents when not being edited and a TableCellEditor to edit the contents. You need to set the font for both if you want them both to be different from the default. See Editors & Renderers.

    In the particular is contained the universal...
    J. Joyce
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jun 2010
    Posts
    5

    Resolved Re: Font is changing While editing cell in JTable - Need a fix

    Thanks for your reply.

    I found the solution. 've set the font of the Textfield.


    for (int i = 0; i < jTable1.getColumnCount(); i ++) {
    TableColumn col = jTable1.getColumnModel().getColumn(i);
    col.setCellEditor(new MyTableCellEditor());
    }


    public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
    JComponent component = new JTextField();
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
    ((JTextField)component).setText((String)value);
    ((JTextField)component).setFont(new java.awt.Font("Arial Unicode MS", 0, 12));
    return component;
    }

  4. #4
    Join Date
    Jun 2010
    Posts
    5

    Re: Font is changing While editing cell in JTable - Need a fix

    Quote Originally Posted by dlorde View Post
    How do you set the font type of the cell?
    Sorry, My question was wrong. I didn't set the font for a single cell but for a column.

  5. #5
    Join Date
    Jun 2010
    Posts
    5

    Re: Font is changing While editing cell in JTable - Need a fix

    Quote Originally Posted by dlorde View Post
    How do you set the font type of the cell?

    for (int i = 0; i < jTable1.getColumnCount(); i ++) {
    TableColumn col = jTable1.getColumnModel().getColumn(i);
    col.setCellEditor(new MyTableCellEditor());
    col.setCellRenderer(new MyTableCellRenderer());
    }


    public class MyTableCellRenderer
    extends DefaultTableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if (row == 0 && column == 0) {
    component.setFont(new java.awt.Font("Arial Unicode MS", 0, 12));
    }
    else {
    // Some other font.
    }

    return component;
    }
    }

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

    Re: Font is changing While editing cell in JTable - Need a fix

    If you're setting the same renderer and editor for every column, you can just call the default mutators:
    Code:
    table.setDefaultRenderer(Object.class, new MyTableCellRenderer());
    table.setDefaultEditor(Object.class, new MyTableCellEditor());
    This simplifies and saves you creating multiple copies of renderer and editor when you only need one.

    Controlling complexity is the essence of computer programming...
    B. Kernighan
    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.

  7. #7
    Join Date
    Jun 2010
    Posts
    5

    Re: Font is changing While editing cell in JTable - Need a fix

    Thanks a lot

Tags for this Thread

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