Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1008975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:51:19+00:00 2026-05-16T08:51:19+00:00

I have a JTable instance, containing a number of rows. The columns in this

  • 0

I have a JTable instance, containing a number of rows. The columns in this table are JLabel instances containing an HTML-formatted string.

One of my requirements is that all the data in these columns should be displayed. If the column width (for whatever reason) is not wide enough to display all the data, the text should wrap onto the next line. Currently the text is just cut off.

This piece of code illustrates the problem:

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableTest {

 public static void main(final String[] args) {

  final JTable jTable = new JTable();

  jTable.setModel(new DefaultTableModel() {
   private static final long serialVersionUID = 1L;
   @Override
   public Object getValueAt(final int row, final int column) {
    final StringBuffer sb = new StringBuffer();
    sb.append("<html>");
    sb.append("<font color=\"red\">this text is red</font> ");
    sb.append("<font color=\"green\">this text is green!</font>");
    sb.append("</html>");
    return sb.toString();
   }
   @Override
   public int getColumnCount() {
    return 2;
   }
   @Override
   public int getRowCount() {
    return 2;
   }
  });

  final JFrame jFrame = new JFrame();
  jFrame.getContentPane().add(jTable);
  jFrame.setSize(120, 80);
  jFrame.pack();
  jFrame.setTitle("Table test");
  jFrame.setLocationRelativeTo(null);
  jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  jFrame.setVisible(true);

 }

}

My problem might become more clear when running the SSCCE above. If the JFrame is resized, I want the row height to automatically be modified to allow the entire contents of the JPanel to be displayed.

Due to the requirement of HTML, I am not able to use a JTextArea.

Thanks,

Ben

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:51:19+00:00Added an answer on May 16, 2026 at 8:51 am

    This was a good challenge for a Friday morning.

    The answer is to use a custom TableCellRenderer which update the table height on the fly:

    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import java.awt.*;
    
    public class ScratchSpace {
    
    
        public static void main(final String[] args) {
    
            final JTable jTable = new JTable();
    
            jTable.setModel(new DefaultTableModel() {
                private static final long serialVersionUID = 1L;
    
                @Override
                public Object getValueAt(final int row, final int column) {
                    final StringBuffer sb = new StringBuffer();
                    sb.append("<html>");
                    sb.append("<font color=\"red\">this text is red</font> ");
                    sb.append("<font color=\"green\">this text is green!</font>");
                    sb.append("</html>");
                    return sb.toString();
                }
    
                @Override
                public int getColumnCount() {
                    return 2;
                }
    
                @Override
                public int getRowCount() {
                    return 2;
                }
            });
    
    
            jTable.getColumnModel().getColumn(0).setCellRenderer(new MyTableCellRenderer());
            jTable.getColumnModel().getColumn(1).setCellRenderer(new MyTableCellRenderer());
            final JFrame jFrame = new JFrame();
            jFrame.getContentPane().add(jTable);
            jFrame.setSize(120, 80);
            jFrame.pack();
            jFrame.setTitle("Table test");
            jFrame.setLocationRelativeTo(null);
            jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jFrame.setVisible(true);
    
        }
    
        private static class MyTableCellRenderer extends JLabel implements TableCellRenderer {
    
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                setText(String.valueOf(value));
                table.setRowHeight(row, getPreferredSize().height);
                return this;
            }
        }
    }
    

    I think there is a problem with my solution though – the last column to be updated in each row will ultimately be the only one that gets a say in the row height. I leave solving that issue as an exercise for the reader. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.