Link to home
Create AccountLog in
Java

Java

--

Questions

--

Followers

Top Experts

Avatar of zoobird
zoobird

How do you make a table column cell's contents bold?
I have a JTable, and I want to make one of the columns' contents to be bold. How do I do that? Should I customize the TableCellRenderer? How do I do it?

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of sanjay_thakursanjay_thakur

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of yongsingyongsing

This is an example of a table cell renderer. It extends the DefaultTableCellRenderer. What it does is
to set a cell's font to bold.

class MyTableCellRenderer extends DefaultTableCellRenderer {
 public Component getTableCellRendererComponent(
   JTable table, Object value, boolean isSelected,
   boolean hasFocus, int row, int column) {
    setOpaque(true);
    setText(value.toString());

    // set to desired font
    setFont(new Font("Courier", Font.BOLD, 13));

    // set cell's foreground to default cell foreground color
    setForeground(table.getForeground());

    // if cell is selected, set background color to default cell selection background color
    if (isSelected) {
      setBackground(table.getSelectionBackground());
    }

    // otherwise, set cell background color to white
    else {
         setBackground(table.white);
    }

    // draw border on cell if it has focus
    if (hasFocus) {
      setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
    }

    // position cell text at center
    setHorizontalAlignment(SwingConstants.CENTER);

    return this;
 }
}


As an example of its use, first we create an instance of our table cell renderer:

MyTableCellRenderer tableCellRenderer = new MyTableCellRenderer();

Then we set the renderer to the first column in the table:

TableColumn tableColumn = myTable.getColumnModel().getColumn(0);

// set our own table cell renderer for this column
tableColumn.setCellRenderer(tableCellRenderer);


package suncertify.gui;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

/**
*     This class renderers how JTableHeaders will look like
*     i used JList as a rendering component to split it on two lines the
* headers in case there were more then one word in a header.
*/
public class HeaderRenderer extends JList implements TableCellRenderer {
  public HeaderRenderer() {
    setOpaque(true);
    setForeground(UIManager.getColor("TableHeader.foreground"));
    setBackground(UIManager.getColor("TableHeader.background"));
    setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    ListCellRenderer renderer = getCellRenderer();
    ((JLabel)renderer).setHorizontalAlignment(JLabel.CENTER);
    setCellRenderer(renderer);
  }

  public Component getTableCellRendererComponent(JTable table, Object value,
                   boolean isSelected, boolean hasFocus, int row, int column) {
    setFont(table.getFont());
    String str = (value == null) ? "" : value.toString();
    BufferedReader br = new BufferedReader(new StringReader(str));
    String line;
    Vector v = new Vector();
    try {
      while ((line = br.readLine()) != null) {
        v.addElement(line);
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    setListData(v);
    return this;
  }
}

remove this line.
package suncertify.gui;

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


sorry i didnt read whatyou needed clearly, this should work.

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

/**
*     This class renderers how JTableHeaders will look like
*     i used JList as a rendering component to split it on two lines the
* headers in case there were more then one word in a header.
*/
public class HeaderRenderer extends JLabel implements TableCellRenderer {
 public HeaderRenderer() {
   setOpaque(true);
   setForeground(UIManager.getColor("TableHeader.foreground"));
   setBackground(UIManager.getColor("TableHeader.background"));
   setBorder(UIManager.getBorder("TableHeader.cellBorder"));
   ListCellRenderer renderer = getCellRenderer();
   ((JLabel)renderer).setHorizontalAlignment(JLabel.CENTER);
   setCellRenderer(renderer);
 }

 public Component getTableCellRendererComponent(JTable table, Object value,
                  boolean isSelected, boolean hasFocus, int row, int column) {
   setFont(table.getFont());
   String str = (value == null) ? "" : value.toString();
   setText("<html><head></head><body><b>" + str + "</b></body></html>");
   return this;
 }
}

To Marine: please read the guideline below on proposing answers. The questioner wants the cells in a JTable columns to be bold. Your solution is for a JTable header.

darn didnt pay attention sorry reject my proposal.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


A request has been made to delete this question. If there are no objections within 72 hours, the request will be granted.

Netminder
EE Admin

Why delete? Is the questioner not happy with the solution I provided?

yongsing,

I will assume that's an objection. The request will be denied, and this question will be closed either by the Asker's accepting a comment, or by the Moderators during the normal cleanup procedure.

EXPERTS: Please leave your recommendations for resolving this question here.

Netminder
EE Admin

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


yongsing

sanjay_thakur provided that solution before you, so I will force-accept his comment within the next 72 hours.

** Mindphaser - Community Support Moderator **

Force accepted

** Mindphaser - Community Support Moderator **
Java

Java

--

Questions

--

Followers

Top Experts

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.