How to add Button to JTable cell?

Hello,
how do I add JButton JTable cells.
I have created a default model. I pass Object data[ ][ ] and Object column[ ].
I have 5 columns. Out of 5 columns, 4 columns contain string value and in the fifth column I have to create JButton for each row.

Any help would be appreciated.
Thanks
T_ShahAsked:
Who is Participating?
 
objectsCommented:
I saw that, and it did not show anything inicating when the user has entered the date.
Unless you know when the update has been entered there is no way to know when editting can be stopped on the cell and the table updated.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
TheMajesticCommented:
0
 
T_ShahAuthor Commented:
Thanks for reply.

I have table with 5 columns. In Last column, I am trying to create date button for each row.
User click the button and select a date.
My problems are:
* when table loads, it does not display button in last column. but when I click on the cell
      button pop up the calendar from which user can pick a date. Is there any solution to display button?

* When I select a date, I can see the date on the button but it does not set the value in table model.
      It updates the value, only when I click on some other cell in the table atfet I pick the date.
      Any clue?


//---------------------------------------------------------------------------------------------------
Class Test extends JDialog implements ActionListener

      TableValues tv = new TableValues(data);
      table = new JTable(tv);
      table.setRowHeight(30);
      TableColumnModel tcm = table.getColumnModel();
      TableColumn tc = tcm.getColumn(4);
      tc.setCellEditor(new TableCellEditor());
//---------------------------------------------------------------------------------------------------
public class TableValues extends AbstractTableModel
Object[][] values;//contains all the data that displays in JTable

      public int getRowCount()
      {
            return values.length;
      }

      public int getColumnCount()
      {
            return values[0].length;
      }

      public Object getValueAt(int row, int column)
      {
            return values[row][column];
      }

      public String getColumnName(int column)
      {
            return columnNames[column];
      }

      public boolean isCellEditable(int row, int column)
      {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if(column == 4)
            {
                  return true;
            }
            else
            {
                  return false;
            }
      }
 
      public void setValueAt(Object value, int row, int column)
      {
            values[row][column] = value;
      }
//---------------------------------------------------------------------------------------------------
public class TableCellEditor extends AbstractCellEditor implements javax.swing.table.TableCellEditor
{
      // This is the component that will handle the editing of the cell value
      DateButton component = new DateButton();

    // This method is called when a cell value is edited by the user.
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int rowIndex, int vColIndex)
    {
        // 'value' is value contained in the cell located at (rowIndex, vColIndex)y           
          if (isSelected)
        {
            // cell (and perhaps other cells) are selected
        }

        // Configure the component with the specified value
        component.setText((String)value);
       
        // Return the configured component
        return component;
    }

    // This method is called when editing is completed.
    // It must return the new value to be stored in the cell.
    public Object getCellEditorValue()
    {
        return ((DateButton)component).getText();
    }
}
//---------------------------------------------------------------------------------------------------
0
 
T_ShahAuthor Commented:
I forget to add code for CellRender in previous post.

OK, I fix the problem to show button.

Now Only one problem:

* When I select a date, I can see the date on the button but it does not set the value in table model.
      It updates the value, only when I click on some other cell in the table atfet I pick the date.
      Any clue?
0
 
T_ShahAuthor Commented:
any clue??
0
 
objectsCommented:
u need an action listener on your button that ends cell editting (as shown in the example i posted)
0
 
T_ShahAuthor Commented:
Thanks for your reply. I tried the way your example show but does not work.
The problem is :
The date button is a custom class that is been provided. I can not see its code. This class create a button. when you click the button, it popup a calendar from which you can pick a date. Also, it has ok and clear button. When I pick the date and click ok button, new date set to that button.  If I call  fireEditingStopped(); on OK button, it should work fine but I don't know how to get action event of that button.

Is there any way, I can force fully call SetValueAt method at point where new date is set to that button?
If so, can you tell me where exactly I should call that?

I tried so many different ways but none works...

I really appreciate your help
0
 
objectsCommented:
try the popup updating the table model directly then.
0
 
T_ShahAuthor Commented:
How do I do that?
0
 
objectsCommented:
What indication do you have that the user has entered a date?
0
 
T_ShahAuthor Commented:
my code is mentioned above.
0
 
T_ShahAuthor Commented:
Thanks for your help. I solved the problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.