?
Solved

Setting font of the celleditor in JTable

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

Author Comment

by:tuckkhai
ID: 8192375
urgent
0
 

Accepted Solution

by:
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);
  editComponent.setFont(newFont);

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".

Bernard
0
 
LVL 92

Expert Comment

by:objects
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.
0
 
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).

Mayank.
0

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