How to display multiple rows in JTable Cell

I would like to create JTable

like below:enter image description here

What class java

will be used and perhaps how?


source to share

7 replies

basically you can put any type JComponents

in a cell JTable

, depends on whether the content is editable, which I'm talking about below.

  • JTable

    with one TableColumn


  • JPanel

    ( GridBagLayout

    ) with JLabels


  • JList



Multi-line cell JTable

can be easily used with customizable TableCellRenderer

. Use the following steps to create TableCellRenderer


Step 1: Create TableCellRenderer

The following code shows how to create multi-line values TableCellRenderer

for String[]

. Can be changed String[]

to a Vector

or another typeCollection

public class MultiLineTableCellRenderer extends JList<String> implements TableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        //make multi line where the cell value is String[]
        if (value instanceof String[]) {
            setListData((String[]) value);

        //cell backgroud color when selected
        if (isSelected) {
        } else {

        return this;


Step 2: install TableCellRenderer


    MultiLineTableCellRenderer renderer = new MultiLineTableCellRenderer();

    //set TableCellRenderer into a specified JTable column class
    table.setDefaultRenderer(String[].class, renderer);

    //or, set TableCellRenderer into a specified JTable column


This is my screenshot:

TableCellRenderer Multi-Line Test



It won't work if you are using DefaultTableModel

. You should use a custom model for your table that extends AbstractTableModel

and overrides the method getColumnClass()

, along with other abstract methods.

You will also set the table row height as mentioned in the comment above .

The official documentation from Oracle clarifies this.





Try this code :)



jTable1.setShowHorizontalLines(true); // only HorizontalLines
  jTable1.setShowVerticalLines(true); //  only VerticalLines
  jTable1.setShowGrid(true);          // show Horizontal and Vertical
  jTable1.setGridColor(Color.yellow); // change line color 




Besides TableCellRenderer

based on JList<String>

as Channa Jayamuni suggests, you can also create a multi-line renderer on topDefaultTableCellRenderer

. The trick is to use the HTML feature of the component JLabel

and adjust the line height accordingly. This preserves more of the default renderer capabilities.

Enabling this renderer for all lines will do what you want in most cases:

setDefaultRenderer(String.class, new MultiLineCellRenderer());


The solution handles automatic line height to some extent . Those. this will not reduce the table row height if the content is updated with fewer rows. This is a rather tricky task as it depends on the content of any other cell in the same row.

/** Variant of DefaultTableCellRenderer that automatically switches
 *  to multi-line HTML when the value contains newlines. */
class MultiLineCellRenderer extends DefaultTableCellRenderer
  @Override public Component getTableCellRendererComponent(JTable table,
    Object value, boolean isSelected, boolean hasFocus, int row, int column)
  { Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    int height = c.getPreferredSize().height;
    if (table.getRowHeight(row) < height)
      table.setRowHeight(row, height);
    return c;

  @Override protected void setValue(Object value)
  { if (value instanceof String)
    { String sVal = (String)value;
      if (sVal.indexOf('\n') >= 0 && // any newline?
        !(sVal.startsWith("<html>") && sVal.endsWith("</html>"))) // already HTML?
        value = "<html><nobr>" + htmlEncodeLines(sVal) + "</nobr></html>";

  /** Encode string as HTML. Convert newlines to &lt;br&gt; as well.
   * @param s String to encode.
   * @return Encoded string. */
  protected static String htmlEncodeLines(String s)
  { int i = indexOfAny(s, "<>&\n", 0); // find first char to encode
    if (i < 0)
      return s; // none
    StringBuffer sb = new StringBuffer(s.length() + 20);
    int j = 0;
    { sb.append(s, j, i).append(htmlEncode(s.charAt(i)));
      i = indexOfAny(s, "<>&\n", j = i + 1);
    } while (i >= 0);
    sb.append(s, j, s.length());
    return sb.toString();

  private static String htmlEncode(char c)
  { switch (c) {
    case '<': return "&lt;";
    case '>': return "&gt;";
    case '&': return "&amp;";
    case '\n': return "<br>";
    default: return Character.toString(c);
  } }

  private static int indexOfAny(String s, String set, int start)
  { for (int i = start; i < s.length(); ++i)
    if (set.indexOf(s.charAt(i)) >= 0)
      return i;
    return -1;




If any table has a large amount of data, the use of the scrollbar applies to the JTable. JScrollPane provides a scrollable scroll bar and you get all the content. A simple example using JTable:

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

public class ScrollableJTable{
public static void main(String[] args) {
new ScrollableJTable();
public ScrollableJTable(){
JFrame frame = new JFrame("Creating a Scrollable JTable!");
JPanel panel = new JPanel();
 String data[][] =
  JTable table = new JTable(data,col);
  JTableHeader header = table.getTableHeader();
  JScrollPane pane = new JScrollPane(table);


Now you can modify this table according to your requirements.



All Articles