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

  • Home
  • SEARCH
  • 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 8975859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:59:09+00:00 2026-06-15T18:59:09+00:00

Need a simple Swing code to demonstrate how to add a button in a

  • 0

Need a simple Swing code to demonstrate how to add a button in a column of a Jtable using tablecellrenderer and tablecelleditor.

  • 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-15T18:59:11+00:00Added an answer on June 15, 2026 at 6:59 pm

    Add button to JTable

    JTable table = new JTable(new JTableModel()); 
    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true); 
            
    TableCellRenderer buttonRenderer = new JTableButtonRenderer();
    table.getColumn("Button1").setCellRenderer(buttonRenderer);
    table.getColumn("Button2").setCellRenderer(buttonRenderer);
    

    Sample JTableModel, This is manage the columns and rows, Setting components

    public static class JTableModel extends AbstractTableModel {
            private static final long serialVersionUID = 1L;
            private static final String[] COLUMN_NAMES = new String[] {"Id", "Stuff", "Button1", "Button2"};
            private static final Class<?>[] COLUMN_TYPES = new Class<?>[] {Integer.class, String.class, JButton.class,  JButton.class};
            
            @Override public int getColumnCount() {
                return COLUMN_NAMES.length;
            }
    
            @Override public int getRowCount() {
                return 4;
            }
            
            @Override public String getColumnName(int columnIndex) {
                return COLUMN_NAMES[columnIndex];
            }
            
            @Override public Class<?> getColumnClass(int columnIndex) {
                return COLUMN_TYPES[columnIndex];
            }
    
            @Override public Object getValueAt(final int rowIndex, final int columnIndex) {
                /*Adding components*/
                switch (columnIndex) {
                    case 0: return rowIndex;
                    case 1: return "Text for "+rowIndex;
                    case 2: // fall through
                   /*Adding button and creating click listener*/
                    case 3: final JButton button = new JButton(COLUMN_NAMES[columnIndex]);
                            button.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent arg0) {
                                    JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(button), 
                                            "Button clicked for row "+rowIndex);
                                }
                            });
                            return button;
                    default: return "Error";
                }
            }   
        }
    

    Sample Button click listener , This manage the when mouse is clicked over the component

    private static class JTableButtonMouseListener extends MouseAdapter {
            private final JTable table;
            
            public JTableButtonMouseListener(JTable table) {
                this.table = table;
            }
    
            public void mouseClicked(MouseEvent e) {
                int column = table.getColumnModel().getColumnIndexAtX(e.getX()); // get the coloum of the button
                int row    = e.getY()/table.getRowHeight(); //get the row of the button
    
                /*Checking the row or column is valid or not*/
                if (row < table.getRowCount() && row >= 0 && column < table.getColumnCount() && column >= 0) {
                    Object value = table.getValueAt(row, column);
                    if (value instanceof JButton) {
                        /*perform a click event*/
                        ((JButton)value).doClick();
                    }
                }
            }
        }
    

    Sample JTable Cell Renderer, Managing the cell component

    private static class JTableButtonRenderer implements TableCellRenderer {        
            @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                JButton button = (JButton)value;
                return button;  
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple game in Java using swing and am having problems with
Till now, I have developed simple swing applications so there was no need to
I need help setting up a simple C++/C# SWIG project. I am having a
How do I create unique key value in PHP? I need simple unique keys
I need a simple C program which creates tcl interpreter, initializes tcl and tk
I need a simple web based tool/CMS/app which could be linked to some repository
I need a simple communication protocol between two devices (a PC and a microcontroller).
I need something simple like date , but in seconds since 1970 instead of
I need a simple application, preferably a cross-platform one, that enables sending of files
I need a simple proxy PHP function/script that can download a file from a

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.