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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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