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 7195949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:40:58+00:00 2026-05-28T20:40:58+00:00

I have a table with 3 columns, the first column is special and contains

  • 0

I have a table with 3 columns, the first column is special and contains a checkbox instead of a title.

The problem is that the layout (appareance) of the checkbox header is different from the other headers. Can anyone help?

The Code:

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

public class JTableHeaderCheckBox  
{  
  Object colNames[] = {"", "String", "String"};  
  Object[][] data = {};  
  DefaultTableModel dtm;  
  JTable table;  
  public void buildGUI()  
  {  
    dtm = new DefaultTableModel(data,colNames);  
    table = new JTable(dtm);  
    for(int x = 0; x < 2; x++)  
    {  
      dtm.addRow(new Object[]{new Boolean(false),"Row "+(x+1)+" Col 2","Row "+(x+1)+" Col 3"});  
    }  
    JScrollPane sp = new JScrollPane(table);  
    TableColumn tc = table.getColumnModel().getColumn(0);  
    tc.setCellEditor(table.getDefaultEditor(Boolean.class));  
    tc.setCellRenderer(table.getDefaultRenderer(Boolean.class));  
    tc.setHeaderRenderer(new CheckBoxHeader());  
    JFrame f = new JFrame();  
    f.getContentPane().add(sp);  
    f.pack();  
    f.setLocationRelativeTo(null);  
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    f.setVisible(true);  
  }  

  public static void main (String[] args)  
  {  
    SwingUtilities.invokeLater(new Runnable(){  
      public void run(){  
        new JTableHeaderCheckBox().buildGUI();  
      }  
    });  
  }  
}  

class CheckBoxHeader extends JCheckBox implements TableCellRenderer {  
    protected CheckBoxHeader rendererComponent;  
    protected int column;  

    public CheckBoxHeader() {  
        rendererComponent = this;   
    }  

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {  
        setColumn(column);   
        return rendererComponent;  
    }  

    protected void setColumn(int column) {  
        this.column = column;  
    }  
    public int getColumn() {  
        return column;  
    }    
} 

The wierd output:

enter image description here

UPDATE:
What I want:

  1. Center align the checkbox in the header
  2. If you pay close attention, you notice the background of the checkbox Header (it seems popping to the inside) is different from the other 2 headers (they seem popping out)
  • 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-28T20:40:59+00:00Added an answer on May 28, 2026 at 8:40 pm

    If you’re referring to the border of the checkbox you can try this to make the header look more consistent:

    public CheckBoxHeader() {  
        rendererComponent = this;  
    
        setHorizontalAlignment(JLabel.CENTER);
        setBorderPaintedFlat(true);
        setBorderPainted(true);
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with three columns; the first column contains IDs and the
I have created a table with two columns. The first column contains the hyperlinks,
Suppose I have two columns in a table that represents a graph, the first
I have a table with 3 columns and first and third column are divided
I have a table of four columns. The data in the first column is
Ok, so lets say I have a table comprising three columns. The first column
i have a table with two columns first column is a (currentdate)timestamp and the
I have created an HTML table with two columns. I want the first column
I need a table which has two columns: First column have an ID Second
I have a table with 4 columns. The first column is unique for each

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.