Set font in JTable

hello i just have an easy question that i am not smart enough to know yet...but how do u set a cell or a whole JTable to a specific font? Thanx Kevin

Simple, call the setFont(Font font) method of the
table or cell.
e.g.
theTable.setFont(new Font("serif", Font.BOLD, 12));
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JCo
ponent.html#setFont(java.awt.Font)Just to complete the answer to the original question. For a single sell its pritty much the same. You need to get the cell you are interested in from the table e.g. theTable.getCellRenderer(int row, int column) you then need to get the actual rendering component. using getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) on the returned TableCellRenderer from the above snipit. You can then just call setFont on that.
theTable.getCellRenderer(x, y).getTableCellRendererComponent(theTable, value, true, true, x, y).setFont(new Font("serif", Font.BOLD, 12));                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

  • How to set height of JTable Header ?

    How to set height of JTable Header and set the text to another size ?

    You can set the font of the header as you can for any component. The font will dictate the preferred height of the header. If you don't like the preferred height, you can set that as well.
    // add table to a scroll pane or the header will not appear
    JTable table = new JTable(...);
    container.add(new JScrollPane(table));
    // get a reference to the header, set the font
    JTableHeader header = table.getTableHeader();
    header.setFont(new Font(...));
    // set the preferred height of the header to 50 pixels.
    // the width is ignored by the scroll pane.
    header.setPreferredSize(new Dimension(1, 50));Mitch Goldstein
    Author, Hardcore JFC (Cambridge Univ Press)
    [email protected]

  • How Can I set up a JTable columns?

    Dear All,
    How can I set up my JTable columns to have the amount the user specifies?
    for example when the user types in 50 in JTextField 1 I want the JTables columns to then set to 50.
    How can this be done?
    Thanks
    lol
    import javax.swing.*;
    import javax.swing.table.TableModel;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class si1 extends javax.swing.JFrame implements ActionListener {
        JTextField name = new JTextField(15);
        JTextField name1 = new JTextField(15);
        public si1() {
            super("DataBase Loader");
            setSize(1025,740);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel pane = new JPanel();
            JPanel pane1 = new JPanel();
            JPanel pane2 = new JPanel();
            JPanel pane3 = new JPanel();
            JPanel pane4 = new JPanel();
            pane.setLayout(new GridLayout(20,1));
            pane1.setLayout(new BorderLayout());
            int j=10;
            String[][] data = new String[j][2];
            for (int k=0; k<j; k++){
               String[] row = {"",""};
               data[k] = row;
            String[] columnNames = {"First Name", "Last Name"};
            JTable perstab = new JTable(data, columnNames);
            perstab.setGridColor(Color.yellow);
            perstab.setPreferredScrollableViewportSize(new Dimension(500,500));
            JScrollPane scrollPane = new JScrollPane(perstab);
            pane1.add(new JPanel(), BorderLayout.EAST);
            JButton btn = new JButton("What are the names?");
            btn.addActionListener(this);
            btn.putClientProperty("DATABASE", perstab);
            pane1.add(new JPanel().add(btn), BorderLayout.SOUTH);
            pane2.add(name);
            pane3.add(name1);
            pane.add(pane2);
            pane.add(pane3);
            pane1.add(pane, BorderLayout.WEST);
            pane4.add(scrollPane);
            pane1.add(pane4, BorderLayout.CENTER);
            setContentPane(pane1);
            show();
        public static void main(String[] args) {
            si1 frame = new si1();
            frame.setVisible(true);
        public void actionPerformed(ActionEvent e) {
            JTable table = (JTable)((JButton)e.getSource()).getClientProperty("DATABASE");
            TableModel model = table.getModel();
            int count = model.getRowCount();
            String[] firstnames = new String[count];
            String[] lastnames = new String[count];
            for (int i=0; i < count; i++) {
               firstnames[i] = (String)model.getValueAt(i, 0);
                System.out.println("first name at row " + i + ": " + firstnames);
    lastnames[i] = (String)model.getValueAt(i, 1);
    System.out.println("lastname name at row " + i + ": " + lastnames[i]);

    As you can see I have tried this, but no success.
    If I am doing something wrong please accept my apology, and address me in the right direction.
    Thanks
    Lol
    import javax.swing.*;
    import javax.swing.table.TableModel;
    import java.io.*;
    import java.util.*;
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    public class si1 extends javax.swing.JFrame implements ActionListener {
        JTextField name = new JTextField(15);
        JTextField name1 = new JTextField(15);
        public si1() {
            super("DataBase Loader");
            setSize(1025,740);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel pane = new JPanel();
            JPanel pane1 = new JPanel();
            JPanel pane2 = new JPanel();
            JPanel pane3 = new JPanel();
            JPanel pane4 = new JPanel();
            pane.setLayout(new GridLayout(20,1));
            pane1.setLayout(new BorderLayout());
            int j=10;
            String[][] data = new String[j][2];
            for (int k=0; k<j; k++){
               String[] row = {"",""};
               data[k] = row;
            String[] columnNames = {"First Name", "Last Name"};
            JTable perstab = new JTable(data, columnNames);
         ((DefaultTableModel)perstab.getModel()).setColumnCount(Integer.parseInt(name.getText()));
            perstab.setGridColor(Color.yellow);
            perstab.setPreferredScrollableViewportSize(new Dimension(500,500));
            JScrollPane scrollPane = new JScrollPane(perstab);
            pane1.add(new JPanel(), BorderLayout.EAST);
            JButton btn = new JButton("What are the names?");
            btn.addActionListener(this);
            btn.putClientProperty("DATABASE", perstab);
            pane1.add(new JPanel().add(btn), BorderLayout.SOUTH);
            pane2.add(name);
            pane3.add(name1);
            pane.add(pane2);
            pane.add(pane3);
            pane1.add(pane, BorderLayout.WEST);
            pane4.add(scrollPane);
            pane1.add(pane4, BorderLayout.CENTER);
            setContentPane(pane1);
            show();
        public static void main(String[] args) {
            si1 frame = new si1();
            frame.setVisible(true);
        public void actionPerformed(ActionEvent e) {
            JTable table = (JTable)((JButton)e.getSource()).getClientProperty("DATABASE");
            TableModel model = table.getModel();
            int count = model.getRowCount();
            String[] firstnames = new String[count];
            String[] lastnames = new String[count];
            for (int i=0; i < count; i++) {
               firstnames[i] = (String)model.getValueAt(i, 0);
                System.out.println("first name at row " + i + ": " + firstnames);
    lastnames[i] = (String)model.getValueAt(i, 1);
    System.out.println("lastname name at row " + i + ": " + lastnames[i]);

  • Printing Problem with already set Font

    Post Author: tejas_kishanwala
    CA Forum: General
    hi,
    i have created a report using Crystal Report 9 with font Draft 10CPI using Printer EPSON FX-2175 on Windows XP with SP2.  after then when i call this report from my .NET Application (developed on VS 2005). the report is not showing as well printing with the already set Font (Draft 10CPI) Kindly do needful for the same. Tejas Kishanwala 

    Post Author: mewdied
    CA Forum: General
    Is the font Draft 10CPI a printer font?  If it is, you are running across a limitation of .NET not Crystal Reports.  .NET only supports True type and Open type fonts.  Microsoft has a KB about it, but I cannot find it right now.

  • Setting Font Color and Bold Property in ALV Grid

    I have a requirement to set the font in particular ALV Cells  in red color.
    Also this font (text) has to be set to Bold.
    I know how to set cell coloring in ALV Grid, but have no idea regarding how to set font color.
    Could please give me an idea/code segment/suggestion.
    Thank You!

    Please refer this post
    Coloring of ROWS in ALV tree

  • Can't set FONT in iCHAT

    When I try to hit SET FONT in iChat prefs, nothing happens.
    Fonts work in other apps.
    ?????

    I found it. DUH... the font window opened behind my prefs window.

  • [Solved] css setting font family, ff3 vs ie7

    In my css, I've set 'font-family: sans;' in body { }, and in ff3 on my Arch laptop, it displays correctlly. In ie7 on my wifes vista laptop, it still reverts back to w/e serif font it uses if it can't find one set by the web page. I have no idea why this is happening, and I can't seem to fix it. Can anyone give me a hand?
    Last edited by Sjoden (2009-01-04 03:07:09)

    Agh, well, I looked at thayer's blog to see if that worked in both browsers and it did, so I checked to see how he had his fonts set up.
    font-family: Arial, DejaVu Sans, sans-serif;
    The closest I got to that was 'font-family: Arial, DejaVu, sans;'

  • Setting font and color TextItems

    This script is failing to set font, and how can I set font color?; I am new to PS Scripting:
         var originalUnit = preferences.rulerUnits
          preferences.rulerUnits = Units.INCHES
         var docRef = app.documents.add( 11, 17, 300, "Test", NewDocumentMode.CMYK)
        MyLayer = docRef.artLayers.add()
         MyLayer.kind = LayerKind.TEXT
         var myFont = app.fonts.getByName("ArialMT");
         // Set the contents of the text layer.
         var textItemRef = MyLayer.textItem
         textItemRef.font = myFont;
         textItemRef.size = UnitValue(36, 'pt');
         textItemRef.font = '_Aviano-Regular';
         textItemRef.style = "Italic";
         textItemRef.contents = "Sample Text";
         // Restore original ruler unit setting
          app.preferences.rulerUnits = originalUnit
    Thanks

    The font needs to be the postscript name, this will give you a list of them.
    var Dlog =
    "dialog{text:'Script Interface',bounds:[100,100,500,270],"+
    "FontName:DropDownList{bounds:[30,40,370,60]},"+
    "PostScriptName:DropDownList{bounds:[30,100,370,120]},"+
    "statictext0:StaticText{bounds:[40,10,200,30] , text:'Font Name'},"+
    "statictext1:StaticText{bounds:[40,70,141,87] , text:'Postscript Name'},"+
    "button0:Button{bounds:[140,130,240,150] , text:'Ok' }}";
    win = new Window(Dlog,"Use the Postscript Name in your script.");
    var item;
    var item2;
    for (var i=0,len=app.fonts.length;i<len;i++) {
      item = win.FontName.add ('item', "" + app.fonts[i].name);
      item2 = win.PostScriptName.add ('item', "" + app.fonts[i].postScriptName);   
    win.FontName.selection = win.FontName.items[0] ;
    win.PostScriptName.selection = win.PostScriptName.items[0] ;
    win.FontName.onChange = function() {
    win.PostScriptName.selection = win.PostScriptName.items[parseInt(this.selection)] ;
    $.writeln(win.PostScriptName.items[parseInt(this.selection)]);
    win.PostScriptName.onChange = function() {
    win.FontName.selection = win.FontName.items[parseInt(this.selection)] ;
    win.center();
    win.show();
    Here is your code with colour addded...
         var originalUnit = preferences.rulerUnits
          preferences.rulerUnits = Units.INCHES
          var FillColor = new SolidColor;
        FillColor.rgb.hexValue = 'ff0000';
         var docRef = app.documents.add( 11, 17, 300, "Test", NewDocumentMode.CMYK)
        MyLayer = docRef.artLayers.add()
         MyLayer.kind = LayerKind.TEXT
         var myFont = "ArialMT";
         // Set the contents of the text layer.
         var textItemRef = MyLayer.textItem
         textItemRef.font = myFont;
         textItemRef.size = UnitValue(36, 'pt');
         textItemRef.font = myFont;
         //textItemRef.style = "Italic";
         textItemRef.fauxItalic = true;
         textItemRef.contents = "Sample Text";
         textItemRef.color = FillColor;
         // Restore original ruler unit setting
          app.preferences.rulerUnits = originalUnit

  • How do I set font default in aperture?

    how do I set font default in aperture?

    how do I set font default in aperture?
    Do you want to change the size of the fonts or the shape for the fonts in the user interface or in the products (books, webpages)?
    Aperture does not allow to change the fonts used in the graphical user interface, neither the size nor the shape. To change the fonts for the products (books, etc.), pick a theme with the fonts you like.
    Regards
    Léonie

  • Set font size for ChoiceGroup

    Hi,
    Is there any way to set font size of the String elements of ChoiceGroup

    Hi,
    Yes, there is, but only if you're using MIDP2.0.
    Check the API documentation of ChoiceGroup and see that there's a setFont(.., ..) method. The given index (int value) identifies the index of the element in the list for which the font will be set. This means that different elements in one choicegroup can have different fonts.
    Test it carefully, I've noticed that not all devices show correct behaviour on this. Also note that certain devices ignore certain font settings (for example italics, because a lot of native implementations won't have a font in italics).
    Good luck!

  • How to set background of jtable in no data?

    How to set background of jtable in no data?
    I use table.setBackground(Color.black) but no successful
    Please help me. thanks very much

    maybe, you don't understand me.
    I have below jtable:
    ============================
    I column1 name l column2 name I
    ============================
    l data l data l
    ============================
    l data l data l
    ============================
    XXX
    ============================
    I wrote:
    table.setBackground(Color.black) then only data area has black colour, but XXX area hasn't black colour.
    How to XXX area also has black colour?
    help me, thanks very much.

  • Set font on a text

    Hi!
    I've got some problem with setting font on a text, this is my coed:
    var ar_delaney_e:Font = new ar_delaney();
    textcount=1;
    //Setting a variable to     ar_delaney_e
    this["font_"+String(textcount)] = "ar_delaney_e";
    //Create a textFormat
    var textFormat:TextFormat = new TextFormat();
    //Setting font textformat fon to ar_delaney_e??
    textFormat.font = this["font_"+String(textcount)];
    //Set textformat to my text
    myText.defaultTextFormat = textFormat;
    Fon is embedded hav also tried textFormat.font = this["font_"+String(textcount)].fontName; instead of textFormat.font = this["font_"+String(textcount)]; but no one of this will work, how can you fix this?

    On stage I got a button with instance name add_text_btn, and a mc called "knapp_bild". So when pressing add_text_btn the function func_add_text_btn creates a movie clip with a text in it and also variables for size, color and font for the text. the two variables size and color works fine.
    add_text_btn.addEventListener(MouseEvent.CLICK, func_add_text_btn);
    function func_add_text_btn(event:MouseEvent):void{
    textcount++;
    var MovieClip:MovieClip = new MovieClip();
    MovieClip.x = 80;
    MovieClip.y = 60;
    MovieClip.alpha = 1;
    MovieClip.name="mc_text"+String(textcount);
    //Variables for font, size and color
    this["size_"+String(textcount)] = 40;
    this["color_"+String(textcount)] = "0x00CC00";
    this["font_"+String(textcount)] = "ar_delaney_e";
    textFormat.font = this["font_"+String(textcount)].fontName;
    textFormat.size = this["size_"+String(textcount)];
    textFormat.color = this["color_"+String(textcount)];
    var textField:TextField = new TextField()
    textField.defaultTextFormat = textFormat;
    textField.autoSize="left";
    textField.text = 'Text '+String(textcount);
    textField.name = "text"+String(textcount);  // give them names
    textField.background=false;
    textField.embedFonts=true;
    var mc_name=String("mc_text"+String(textcount));
    knapp_bild.addChild(MovieClip);
    MovieClip.addChild(textField);
    textField.mouseEnabled=false;
    MovieClip.width=textField.width;
    Later on in the program this["font_"+String(textcount)] will change but it dosen't work in the func_add_text_btn function now so let's just try to fix that first. Hope you can see something that mabe won't work and just ask if you got any qusetion. Thanks for your help!

  • How to set Font in JEditorPane

    i have problem set Font in JEditorPane.please help how to set Font in JEditorPane.
    My Code,
    JEditorPane view = new JEditorPane();
    view.setEditable(false);
    view.setContentType("text/html");
    JScrollPane scroll = new JScrollPane(view);
    scroll.setMinimumSize(new java.awt.Dimension(50, 50));
    scroll.setPreferredSize(new java.awt.Dimension(450, 300));
    scroll.setAutoscrolls(true);
    scroll.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scroll.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    view.setBorder(null);
    view.setFont(new java.awt.Font("Monospaced", 0, 12));
    view.setDragEnabled(true);
    view.setMargin(new java.awt.Insets(10, 10, 10, 10));
    view.setMinimumSize(new java.awt.Dimension(100, 20));
    view.setPreferredSize(new java.awt.Dimension(450, 300));

    Have a look at SimpleAttributeSet.

  • Set Font globally in JavaFX

    How I can set the Font type globally in JavaFX application?
    Is there any solution which I can use? in JavaFX 8 the default Font is changed I would like to use the same Font used in JavaFX 2.2.
    Ref Set Font globally in JavaFX - Stack Overflow

    Add a stylesheet with the rule
    .root { -fx-font: 13 "Comic Sans MS"; }
    or whatever font size and family you want.
    You can also use an inline style on the root node, e.g.,
    Parent root = scene.getRoot();
    root.setStyle("-fx-font: 13 \"Comic Sans MS\"; ");

  • How to set font for title and message in JOptionPane?

    Hi,
    I have following test code.
    JOptionPane.showMessageDialog(null, "Some Alert Message", "Alert", JOptionPane.ERROR_MESSAGE);In this code can we set font for "Some Alert Message", "Alert"?
    Regards,
    Kalyani......

    roll-your-own?
    import javax.swing.*;
    class Testing
      public static void main(String[] args)
        JDialog.setDefaultLookAndFeelDecorated(true);
        JOptionPane optionPane = new JOptionPane("<html><font size='5'>Your message here</font></html>");
        optionPane.setOptionType(JOptionPane.DEFAULT_OPTION);
        optionPane.setMessageType(JOptionPane.ERROR_MESSAGE);
        JDialog dialog = optionPane.createDialog(null, "Alert");
        dialog.getLayeredPane().getComponent(1).setFont(dialog.getFont().deriveFont(18f));//size = 18/whatever
        dialog.setModal(true);
        dialog.setVisible(true);
    }

Maybe you are looking for

  • Where can i get a new screen for my ipod touch 8gb

    Screen is cracked and there is no picture only a white page tried to restore and update it it doesnt work. Can anybody help me

  • ERR-3331 Appearing regularly when working on APEX 3.0 page definitions

    ERR-3331 This page was already submitted and can not be re-submitted. I cannot consistently reproduce it but it happens every now and then. Refreshing the browser (ctrl-F5) solves the problem but you lose a bit of work every time. Cannot remember hav

  • Computer cannot remove Quicktime to install newer version

    when i try to download the newest itunes it also downloads the newest quicktime of course. Well, It was trying to and then a message poped up saying, "The older version of Quicktime cannot be removed. Contact your technical support group." This also

  • Possible bug on "undo"

    The undo action goes back by 2 steps. It happens quite often when I make an "undo" with "cmd+z" in develop module and the missed step is lost: not sure if this happen also using the "edit" menu though (which is not practical, so useless!). Working wi

  • Wheel doesnt work

    the spin wheel doesnt work but the select, menu,play,->,<- nuttons work