Setting font of the celleditor in JTable

Posted on 2003-03-23
Medium Priority
Last Modified: 2013-12-02
Does anyone know how to set the font of the default editor in the JTable?
Please help....
Question by:tuckkhai

Author Comment

ID: 8192375

Accepted Solution

bfarrell earned 800 total points
ID: 8192637
One way to figure this out is to browse the JDK source code. In this answer, I assuming you're using JDK 1.4, though the answer should hold for JDK 1.3.

The default editor will take several forms depending on the class contained in the cell. You would change this by calling setDefaultEditor() on the table.

Assuming that the default editor is being used to edit a text cell, it will be a JTextField.

You can retrieve the cell editor for the table by using getCellEditor(), or you can use getDefaultEditor(Class) to retrieve the default editor for a specific class.

So if you have a JTable myTable. Your code might look like the following:
  JTable myTable = new JTable(12, 10);
  // Initialization code goes here

  // Retrieve the Cell Editor for String values.
  TableCellEditor defaultEditor = myTable.getDefaultEditor(String.class);

  // Retrieve the actual Java Component used in editing
  Component editComponent = defaultEditor.getComponent();
  // Now set the font that I want to use. Something BIG.
  Font newFont = new Font("TimesRoman", Font.BOLD, 20);

Alternately you can use the UIManager to set the font for the TextFields in your application, and that should also change the font for editing String cells (and also the font for all JTextFields and other things).

The call would be something like:

UIManager.put("TextField.font", new Font("TimesRoman", Font.BOLD, 20));

I hope this helps. To paraphrase a well-known character, my main advice is "Use the source, Luke".

LVL 92

Expert Comment

ID: 8192867
Try setting the font used by the table.
If this doesn't have the desired effect, then you will proabky need to supply your own custom cell editor.
LVL 30

Expert Comment

by:Mayank S
ID: 8193331
JTable table = new JTable ( .... ) ;
table.setFont ( new Font ( "FontName", Font.STYLE, <size> ) ) ;

"FontName" can be "Default", or "TimesRoman", or one of the available fonts. The STYLE can be BOLD, or PLAIN, or ITALIC, or combined as Font.BOLD | Font.ITALIC. The size should be an integer (try 12 or something close to it).


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question