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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:25:09+00:00 2026-05-15T15:25:09+00:00

I need to highlight a particular column header in a JTable.Is it possible to

  • 0

I need to highlight a particular column header in a JTable.Is it possible to do that?
Also is it possible to draw a vertical line inside a JTable ?

  • 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-15T15:25:10+00:00Added an answer on May 15, 2026 at 3:25 pm

    Both are possible. Example:

    public class Test extends JPanel{
    
    private JTable table;
    private String COLUMN1 = "COLUMN1";
    private String COLUMN2 = "COLUMN2";
    
    public Test() {
        setLayout(new BorderLayout()); 
        table = new LineTable(new Object[][]{{"1", "2"}, {"3", "4"}}, new Object[]{COLUMN1, COLUMN2});
    
        TableColumn col = table.getColumn(COLUMN1);
        col.setIdentifier(COLUMN1);
        col.setHeaderValue("Column1");
    
        col = table.getColumn(COLUMN2);
        col.setIdentifier(COLUMN2);
        col.setHeaderValue(new ColorHeaderValue("Column2"));
        col.setHeaderRenderer(new ColorHeaderRenderer());
        col.setCellRenderer(new LineCellRenderer());
    
        JPanel pn = new JPanel();
        pn.setLayout(new BoxLayout(pn, BoxLayout.X_AXIS));
    
        pn.add(new JButton(new AbstractAction("Highlight Column2 in red"){
    
            @Override
            public void actionPerformed(ActionEvent e){
                setColumnColor(COLUMN2, Color.RED);
            }
        }));
        pn.add(new JButton(new AbstractAction("Clear Column2 header color"){
    
            @Override
            public void actionPerformed(ActionEvent e){
                setColumnColor(COLUMN2, null);
            }
        }));
        pn.add(Box.createHorizontalGlue());
        add(pn, BorderLayout.NORTH);
        add(new JScrollPane(table), BorderLayout.CENTER);
    }
    
    private void setColumnColor(final String columnID, final Color cl){
        SwingUtilities.invokeLater(new Runnable(){
    
            @Override
            public void run(){
                Object headerValue = table.getColumn(columnID).getHeaderValue();
                if(headerValue instanceof ColorHeaderValue){
                    ColorHeaderValue clHeaderValue = (ColorHeaderValue) headerValue;
                    clHeaderValue.setBrgColor(cl);
                    table.getTableHeader().repaint();
                }
            }
        });
    
    }
    
    private static class LineTable extends JTable{
    
        public LineTable(Object[][] rowData, Object[] columnNames) {
            super(rowData, columnNames);
    
        }
    
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            if (g != null){
                g.drawLine(0, 0, this.getWidth(), getHeight());
            }
    
        }
    }
    private static class LineCellRenderer extends DefaultTableCellRenderer{
    
        @Override
        protected void paintComponent(Graphics g){            
            super.paintComponent(g);
            if (g != null){
                int halfX = getWidth()/2;
                g.drawLine(halfX, 0, halfX, getHeight());
            }
        }
    
    }
    private static class ColorHeaderRenderer extends DefaultTableCellHeaderRenderer{
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
            Component res = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (value instanceof ColorHeaderValue){
                ColorHeaderValue headerValue = (ColorHeaderValue) value;
                if (headerValue.getBrgColor() != null){
                    setBackground(headerValue.getBrgColor());
                }
            }
            return res;
        }
    
    }
    private static class ColorHeaderValue{
    
        private Color brgColor;
        private String title;
    
        public ColorHeaderValue(String title) {
            this.title = title;
        }
    
        public final Color getBrgColor(){
            return brgColor;
        }
    
        public final void setBrgColor(Color brgColor){
            this.brgColor = brgColor;
        }
    
        public String toString(){
            return title;
        }
    
    }
    
    public static void main(String[] args){
        JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        // Add content to the window.
        frame.add(new Test());
    
        // Display the window.
        frame.pack();
        frame.setSize(300, 200);
        frame.setVisible(true);
    }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to highlight line with caret in NSTextView using CALayer overlay and grab
I need to find text in html and highlight that html without corrupting html
I need to have a small identifier to highlight the particular city on Map
I basically need to highlight a particular word in a block of text. For
I need to highlight specific text inside richtext box during runtime, based on a
After doing an async $.ajax call, I need to highlight the table cell that
I need to highlight changes(diff) between 2 database text fields in a asp.net application.
I'm programming an application in Struts and I need to highlight some reminders in
I have a regular texbox control. I need to highlight some words with a
I need to be able do the following on a TR: onmouseover highlight the

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.