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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:56:43+00:00 2026-06-01T17:56:43+00:00

In my JTable, I want to rename the table header using right click option.on

  • 0

In my JTable, I want to rename the table header using right click option.on Right clicking on any header it gives option for rename header.but when I am doing this it will do rename of selected column’s header. So I have to first left click on that column then I can further processed.
Is there any way that when I am Right click on any column,it also get selected so that after rename it’s value is got renamed.

My action listener is as below :

int column = jTable1.getSelectedColumn();
                        if (column == -1) {
                            return;
                        }
                        if (column == 0) {
                            JOptionPane.showMessageDialog(jTable1, "You can not rename this column.");
                            return;
                        }
                        boolean blag = true;
                        String sColumnName = null;
                        while (blag) {

                        sColumnName = (String) JOptionPane.showInputDialog(jTable1, "Enter Column Name", "Add Column", JOptionPane.INFORMATION_MESSAGE, null, null, null);
                        if (sColumnName == null) {
                            return;
                        }
                        if (sColumnName.trim().equalsIgnoreCase("")) {
                            JOptionPane.showMessageDialog(jTable1, "Column name can not be blank.");
                            blag = true;
                        } else {
                            blag = false;
                        }
                        Vector vColumns = new Vector();
                        for (int i = 0; i < defaultTableModel.getColumnCount(); i++) {
                            vColumns.add(defaultTableModel.getColumnName(i));
                        }
                        if (vColumns.contains(sColumnName)) {
                            JOptionPane.showMessageDialog(jTable1, "Column name " + sColumnName + " already exist.");
                            return;
                        }
                    }
                    int viewColumn = jTable1.convertColumnIndexToView(column);
                    int totalColumn = defaultTableModel.getColumnCount();
                    Object[] headers = new Object[totalColumn];
                    for (int i = 0; i < totalColumn; i++) {
                        if (i == column) {
                            headers[i] = sColumnName;
                        } else {
                            headers[i] = defaultTableModel.getColumnName(i);
                        }
                    }
                    defaultTableModel.setColumnIdentifiers(headers);
  • 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-06-01T17:56:44+00:00Added an answer on June 1, 2026 at 5:56 pm

    I miss there

    1) MouseListener and determine selected Column with value retuns

        header.addMouseListener(new MouseAdapter() {
    
            @Override
            public void mouseClicked(MouseEvent e) {
                JTableHeader h = (JTableHeader) e.getSource();
                int i = h.columnAtPoint(e.getPoint());
                Object o = h.getColumnModel().getColumn(i).getHeaderValue();
                if (i < 0 || o == selectedColumn) {
                    selectedColumn = null;
                    return;
                }
                selectedColumn = o;
                h.requestFocusInWindow();
            }
        });
    

    2) code for change header.getColumnModel().getColumn(i).setHeaderValue("Clicked");

    simple example

    enter image description hereenter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class SelectedTableHeader {
    
        private JFrame frame = new JFrame("Table Demo");
        private JTableHeader header;
        private Object selectedColumn = null;
        private String[] columnNames = {"String", "Integer", "Float", "Double", "Locale & Double", "Boolean"};
        private Object[][] data = {
            {"aaa", new Integer(12), new Float(12.15), new Double(100.05), new Double(12.05), true},
            {"bbb", new Integer(5), new Float(7.154), new Double(6.1555), new Double(417.55), false},
            {"CCC", new Integer(92), new Float(0.1135), new Double(3.1455), new Double(11.05), true},
            {"ddd", new Integer(12), new Float(31.15), new Double(10.05), new Double(23.05), true},
            {"eee", new Integer(5), new Float(5.154), new Double(16.1555), new Double(17.55), false},
            {"fff", new Integer(92), new Float(4.1135), new Double(31.1455), new Double(3.05), true}};
        private TableModel model = new DefaultTableModel(data, columnNames) {
    
            private static final long serialVersionUID = 1L;
    
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        private JTable table = new JTable(model);
    
        public SelectedTableHeader() {
            header = table.getTableHeader();
            header.addMouseListener(new MouseAdapter() {
    
                @Override
                public void mouseClicked(MouseEvent e) {
                    JTableHeader h = (JTableHeader) e.getSource();
                    int i = h.columnAtPoint(e.getPoint());
                    Object o = h.getColumnModel().getColumn(i).getHeaderValue();
                    if (i < 0) {
                        selectedColumn = null;
                        return;
                    }
                    selectedColumn = o;
                    header.getColumnModel().getColumn(i).setHeaderValue("Clicked");
                    h.requestFocusInWindow();
                }
            });
            final TableCellRenderer hr = table.getTableHeader().getDefaultRenderer();
            header.setDefaultRenderer(new TableCellRenderer() {
    
                private JLabel lbl;
    
                @Override
                public Component getTableCellRendererComponent(
                        JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    if (selectedColumn == value) {
                        lbl = (JLabel) hr.getTableCellRendererComponent(table, value, true, true, row, column);
                        lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(), BorderFactory.createLineBorder(Color.red, 1)));
                        lbl.setHorizontalAlignment(SwingConstants.LEFT);
                    } else {
                        lbl = (JLabel) hr.getTableCellRendererComponent(table, value, false, false, row, column);
                        lbl.setBorder(BorderFactory.createCompoundBorder(lbl.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 0)));
                        lbl.setHorizontalAlignment(SwingConstants.CENTER);
                    }
                    if (column == 0) {
                        lbl.setForeground(Color.red);
                    } else {
                        lbl.setForeground(header.getForeground());
                    }
                    /*return (value == selectedColumn) ? hr.getTableCellRendererComponent(
                    table, value, true, true, row, column) : hr.getTableCellRendererComponent(
                    table, value, false, false, row, column);*/
                    return lbl;
                }
            });
    
            table.setRowHeight(20);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scroll = new JScrollPane(table);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(scroll);
            frame.pack();
            frame.setLocation(150, 150);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    SelectedTableHeader selectedTableHeader = new SelectedTableHeader();
                }
            });
        }
    }
    

    3) another way is add CellEditor to the JTableHeader (notice never tried, and not sure if editor works as expects, only example, I’m sure that required a few modifications)

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

Sidebar

Related Questions

i want to add all my data from DB to jtable, but using this
I want to add a right click popupmenu to a JTable in NetBeans IDE
I want to create a JTable with a header inside some cells. But I
I have a JTable and I want to open a PopupMenu by a right-click
I want to implement DropTargetListener with a JTable as a target. This is roughly
I'm currently using JTable to show the contents in database. I want to provide
can anyone help me with this , I have a jtable and I want
I am populating Employee Data in JTable using Netbeans. I want to add Radiobutton
I'm trying to print a JTable and I want a header and footer on
I'm using the Netbeans RCP and want to make cell selection in a jtable

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.