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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:28:37+00:00 2026-05-20T18:28:37+00:00

I want to create a JTable with few columns which has JButtons on it.

  • 0

I want to create a JTable with few columns which has JButtons on it. I can create JCheckBoxes and also JComboBoxes in the JTable cells but not the JButtons. How do i do this ?

  • 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-20T18:28:38+00:00Added an answer on May 20, 2026 at 6:28 pm

    In my project, this is how I did it for a 4-row table with 3 columns where the first column has JToggleButton in every row:

    The necessary objects…

    private JTable table1;
    private DefaultTableModel table1Model;
    private JScrollPane scrollPane1;
    String[] columnName = {"Button", "Boolean", "Int" };
    

    and under the code where we create the table…

        {
            JToggleButton[] jTableButton = new JToggleButton[] { new JToggleButton("one"), new JToggleButton("two"), new JToggleButton("three"), new JToggleButton("four")};
            table1Model =
                new DefaultTableModel(
                        new Object[][] { { jTableButton[0], false, 1 },
                                         { jTableButton[1], false, 1 },
                                         { jTableButton[2], false, 1 },
                                         { jTableButton[3], false, 1 }},
                                         columnName);
    
            table1 = new JTable(table1Model){
                /**
                 *
                 */
                private static final long serialVersionUID = 1L;
    
                @Override
                public boolean isCellEditable(int rowIndex, int colIndex) {
                    int value = (Integer) table1Model.getValueAt(rowIndex, 2);
    
                    String blueColor = Color.BLUE;
                    String redColor = Color.RED;
                    String selectedColor = Color.GREEN;
    
                    String colorValue = selectedColor;
    
                    if ( value > 0) {
                        colorValue = blueColor; //blue
                    }
                    else {
                        colorValue = redColor; //red
                    }
    
                    return !colorValue.equals(redColor); // Disallow the editing of red cell
                }
    
                @Override
                public Class<?> getColumnClass(int c) {
                    return getValueAt(0, c).getClass();
                }
    
                @Override
                public Component prepareRenderer(TableCellRenderer renderer,int rowIndex, int vColIndex)
                {
                    int value = (Integer) table1Model.getValueAt(rowIndex, 2);
    
                    String blueColor = Color.BLUE;
                    String redColor = Color.RED;
                    String selectedColor = Color.GREEN;
    
                    String colorValue = selectedColor;
    
                    if ( value > 0) {
                        colorValue = blueColor; //blue
                    }
                    else {
                        colorValue = redColor; //red
                    }
    
                    Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
                    if (!isCellSelected(rowIndex, vColIndex))
                    {
    
                        if (vColIndex != 0 ) {
                            c.setEnabled(!colorValue.equals(redColor));
                        }
                        else {
                            c.setEnabled(!colorValue.equals(redColor));
    
                            if (colorValue.equals(redColor))
                                c.setBackground(Color.decode(colorValue));
                        }
    
                        if (c instanceof JToggleButton && vColIndex == 0){
                            JToggleButton button = (JToggleButton) c;
                            button.setToolTipText(((JToggleButton) c).getText());
                        }
    
                        if (   Color.decode(colorValue).getRed() >= 224
                                && Color.decode(colorValue).getGreen() >= 224
                                && Color.decode(colorValue).getBlue() >= 224){
    
                            c.setForeground(Color.gray);
    
                        }
                        else {
    
                            c.setForeground(Color.white);
                        }
                    }
                    return c;
                }
    
            };
    
            scrollPane1.setViewportView(table1);
            table1.setPreferredSize(new java.awt.Dimension(187,290));
            table1.setFont(new java.awt.Font("Segoe UI",0,14));
            table1.getColumnModel().getColumn(0).setCellRenderer(new JToggleButtonRenderer());
        }
    

    JToggleButtonRenderer.java

    class JToggleButtonRenderer extends JToggleButton implements TableCellRenderer {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            if (value == null)
                return null;
    
            if (isSelected) {
                // cell (and perhaps other cells) are selected
            }
    
            if (hasFocus) {
                // this cell is the anchor and the table has the focus
            }
    
            return (Component) value;
        }
    
        // The following methods override the defaults for performance reasons
    
        @Override
        public void validate() {}
    
        @Override
        public void revalidate() {}
    
        @Override
        protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
    
        @Override
        public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}  
    }
    

    As you can see, the important part is the table model, table1Model

    To add a new row…

    //id is something you want put on the button text
    table1Model.addRow(new Object[] { new JToggleButton(id), false, 1} ); 
    

    edited:

    Added missing JToggleButton cell renderer

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

Sidebar

Related Questions

I want to create a JTable with a header inside some cells. But I
I want to create an allocator which provides memory with the following attributes: cannot
I want create a pdf using iText. The method which does this is a
i have jtable filed with data. i want to create java code for Action
I create a thread in my main class. The thread has a timer which
HY! I want to create a TabbedPane with a jTable. Designer: Constructor Code in
I'm trying to create a list that has a header, just like a JTable,
I use JTable GUI component with NetBeans. I want to create multi-line headers I
I am trying to use a JSplitPane in Netbeans 6.9.1, but I can not
I want create a drop shadow around the canvas component in flex. Technically speaking

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.