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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:55:49+00:00 2026-05-26T22:55:49+00:00

How can I have a JTable with both columns & rows have title? something

  • 0

How can I have a JTable with both columns & rows have title?
something like this:

+------+------+------+------+
|/\/\/\| col1 | col2 | col3 |
+------+------+------+------+
| row1 |      |      |      |
+------+------+------+------+
| row2 |      |      |      |
+------+------+------+------+
| row3 |      |      |      |
+------+------+------+------+

Thanks

  • 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-26T22:55:49+00:00Added an answer on May 26, 2026 at 10:55 pm

    I think that you searching RowTable

    enter image description here

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import javax.swing.UIManager.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    
    public class JTableRowHeader {
    
        private JFrame frame = new JFrame("JTable RowHeader");
        private JScrollPane scrollPane;
        private JTable table;
        private DefaultTableModel model;
        private TableRowSorter<TableModel> sorter;
        private JTable headerTable;
    
        public JTableRowHeader() {
            table = new JTable(4, 4);
            for (int i = 0; i < table.getRowCount(); i++) {
                table.setValueAt(i, i, 0);
            }
            sorter = new TableRowSorter<TableModel>(table.getModel());
            table.setRowSorter(sorter);
            model = new DefaultTableModel() {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public int getColumnCount() {
                    return 1;
                }
    
                @Override
                public boolean isCellEditable(int row, int col) {
                    return false;
                }
    
                @Override
                public int getRowCount() {
                    return table.getRowCount();
                }
    
                @Override
                public Class<?> getColumnClass(int colNum) {
                    switch (colNum) {
                        case 0:
                            return String.class;
                        default:
                            return super.getColumnClass(colNum);
                    }
                }
            };
            headerTable = new JTable(model);
            for (int i = 0; i < table.getRowCount(); i++) {
                headerTable.setValueAt("Row " + (i + 1), i, 0);
            }
            headerTable.setShowGrid(false);
            headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            headerTable.setPreferredScrollableViewportSize(new Dimension(50, 0));
            headerTable.getColumnModel().getColumn(0).setPreferredWidth(50);
            headerTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
    
                @Override
                public Component getTableCellRendererComponent(JTable x, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    
                    boolean selected = table.getSelectionModel().isSelectedIndex(row);
                    Component component = table.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(table, value, false, false, -1, -2);
                    ((JLabel) component).setHorizontalAlignment(SwingConstants.CENTER);
                    if (selected) {
                        component.setFont(component.getFont().deriveFont(Font.BOLD));
                        component.setForeground(Color.red);
                    } else {
                        component.setFont(component.getFont().deriveFont(Font.PLAIN));
                    }
                    return component;
                }
            });
            table.getRowSorter().addRowSorterListener(new RowSorterListener() {
    
                @Override
                public void sorterChanged(RowSorterEvent e) {
                    model.fireTableDataChanged();
                }
            });
            table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    model.fireTableRowsUpdated(0, model.getRowCount() - 1);
                }
            });
            scrollPane = new JScrollPane(table);
            scrollPane.setRowHeaderView(headerTable);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(scrollPane);
            frame.add(new JButton(new AbstractAction("Toggle filter") {
    
                private static final long serialVersionUID = 1L;
                private RowFilter<TableModel, Object> filter = new RowFilter<TableModel, Object>() {
    
                    @Override
                    public boolean include(javax.swing.RowFilter.Entry<? extends TableModel, ? extends Object> entry) {
                        return ((Number) entry.getValue(0)).intValue() % 2 == 0;
                    }
                };
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (sorter.getRowFilter() != null) {
                        sorter.setRowFilter(null);
                    } else {
                        sorter.setRowFilter(filter);
                    }
                }
            }), BorderLayout.SOUTH);
            frame.pack();
            frame.setLocation(150, 150);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            try {// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if (info.getName().equals("Nimbus")) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (Exception e) {
                //e.printStackTrace();
            }
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JTableRowHeader TestTableRowHeader = new JTableRowHeader();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anyone help me with this , I have a jtable and I want
I have a jTable with columns 'Job_no' and 'Status'with values such as: Job_no Status
I have a JTable displays the event accordingly, I want to do like when
I have this JTable on my Swing app with the autoCreateRowSorter enabled. My table
I have a JTable and I need to be able to reorder the columns.
I have a JTable where i enter some values. This is the listener function
I am using a jtable which have 8 rows by default. When I add
I have problems on inserting rows into a JTable and I don't know what
How can I create a JTable with a row header? This question seems simple
How can I use a JTable to display & edit attribute properties for entities

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.