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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:14:31+00:00 2026-05-20T21:14:31+00:00

I want to add a combo box as cell editor to my JTable. To

  • 0

I want to add a combo box as cell editor to my JTable. To achieve this I have used the column.setCellEditor method as follows. But I found that it wasn’t working. How can I fix this?

def treeModel = new GrvTableModel()
            table = new JTable( treeModel)

            TableColumn col = table.getColumnModel().getColumn(1);          
            JComboBox com = new JComboBox();
            com.addItem("one");
            com.addItem("two");
            com.addItem("three");
            col.setCellEditor (new DefaultCellEditor(com));
....................... 
class GrvTableModel extends AbstractTableModel {
public String[] columnNames = [ "key" , "value"]
public Object[][] data = []

public selectedObjArray

    //Returns the number of columns in the datamodel
    public int getColumnCount() {
        return columnNames.length;
    }

    //Returns the number of rows in the datamodel
    public int getRowCount() {
        return data.length;
    }

    //Returns the name of column in the datamodel at the specified index
    public String getColumnName(int col) {
        return columnNames[col];
    }

    //Returns the object at the specified [row,column] index
    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

     public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col == 1) {
            return true;
        } else {
            return false;
        }
     }

//sets the object at the row and column as specified
    public void setValueAt(Object value, int row, int col) {
        println "change table row: " + row

        data[row][col] = value;
        fireTableCellUpdated(row, col);
        selectedObjArray[row].val = value
    }

}

PS: The table data is added dynamically within an action performed method.

  • 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-20T21:14:31+00:00Added an answer on May 20, 2026 at 9:14 pm

    This seems to work (I had to make up test data as you didn’t supply a runnable example)

    import java.awt.BorderLayout
    import javax.swing.DefaultCellEditor
    import javax.swing.JComboBox
    import javax.swing.JFrame
    import javax.swing.JTable
    import javax.swing.table.AbstractTableModel
    
    // Define the model class with hardcoded data
    class MyModel extends AbstractTableModel {
      Object[][] data = [ [ 'a', 'one' ], [ 'b', 'two' ], [ 'c', 'three' ] ]
      String[] columnNames = [ 'Key', 'Value' ]
    
      int     getColumnCount()                   { columnNames?.length ?: 0 }
      int     getRowCount()                      { data?.length ?: 0 }
      String  getColumnName( int col )           { columnNames[ col ] }
      Object  getValueAt( int row, int col )     { data[ row ][ col ] }
      Class   getColumnClass( int col )          { getValueAt( col, 0 ).class }
      boolean isCellEditable( int row, int col ) { col == 1 }
    
      void setValueAt( Object value, int row, int col ) {
        data[ row ][ col ] = value
        fireTableCellUpdated( row, col )
      }
    }
    
    // Test the model class
    table = new JTable( new MyModel() )
    combo = new JComboBox( [ 'one', 'two', 'three' ] as Object[] )
    table.columnModel.getColumn( 1 ).cellEditor = new DefaultCellEditor( combo )
    
    // And make a frame to run it all in
    new JFrame( 'Test' ).with {
      layout = new BorderLayout()
      add table, BorderLayout.CENTER
      pack()
      defaultCloseOperation = HIDE_ON_CLOSE
      visible = true
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want add my custom sidebar next right column all page. Please check this
I want to add simple list/combo box with action script 3 . but i
I want to add editable combo box in GWT , so please tell me
I have created a jframe using net beans and add combo box to that.
I want to display member of combo box(add) in windows form before clicking drop
I have a combo box (winform). This combo box has some items (eg. 1,2,3,4).
I want to add checkbox element to a combo box / select element dynamically.
Pretty self-explanatory. I want to just have the combo box without any text outside
I want to add a series of strings to a combo box using std::for_each.
I want to add a combo box to the toolbar (coolbar) of eclipse using

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.