jComboBox in specific JTable cell
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: jComboBox in specific JTable cell

  1. #1
    Join Date
    Feb 2012

    Arrow jComboBox in specific JTable cell

    I've a problem that is driving me crazy.

    I have a classic JTable, with several columns. I need that a particular column, instead of simple texts values in its cell, it must contain a ComboBox. I searched A LOT, all I found was examples that would implement the same JComboBox in each cell of the column, that it's not what I need: I need that each cell of the column has a combo box with different values.

    Can anyone give me some practical example of how to do it, please?


    PS: I'm using NetBeans.

  2. #2
    Join Date
    Nov 2011

    Re: jComboBox in specific JTable cell

    TableColumn sportColumn = table.getColumnModel().getColumn(2);
    JComboBox comboBox = new JComboBox();
    comboBox.addItem("Chasing toddlers");
    comboBox.addItem("Speed reading");
    comboBox.addItem("Teaching high school");
    sportColumn.setCellEditor(new DefaultCellEditor(comboBox));

    i got this from docs.Oracle.. Here's the link http://docs.oracle.com/javase/tutori...nts/table.html

    The pic looked promising but i cant grant you anything since i have no experience with jtables
    Last edited by cens; February 13th, 2012 at 03:33 PM.

  3. #3
    Join Date
    May 2006

    Re: jComboBox in specific JTable cell

    @Cens: That gives the same JCombox for each row.

    I searched A LOT,
    Really, I searched once and found several code examples including this one:

    And there's a slightly clearer example of how to use this code here: http://stackoverflow.com/questions/6...table-in-swing
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Apr 2007

    Re: jComboBox in specific JTable cell

    How do you specify which values belong to which row? Who is in charge of those, the table model?

    You can write an extension of DefaultCellEditor. When getTableCellEditorComponent(...) is called, you have reference to the row, cell and model. If the model has capacity to define the combobox values, you can populate the combobox's model with desired values in that method.

    This would be neat in the sense that it is dynamic, reuses the same code / combobox, yet however provides varying values per cell/row context. You do not need to worry about "all editors having reference to the same combobox", because any editor is alive only when triggered by user and user may edit only one cell at a time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a Codeguru.com survey!

HTML5 Development Center