Setting cell editor for individual cell in JTable

Hi there,
I want to provide individual cell editor for my JTable. Basically, my JTable shows property names and values. I want to show different cell editors for different properties.
I followed the advice in this post:
http://forum.java.sun.com/thread.jsp?forum=57&thread=423318
but I have a question:
I looked at the code of DefaultCellEditor. It just has a single editor component (the one provided in the constructor), so all the methods use the same component and certain aspects are customized for the type of component. Again, there can be only one type of component at a time.The problem that I am facing is that I will have different components for different row/column of the same table. So how do I implement some of the methods (for example, getCellEditorValue()), when I have multiple editor components?
Also, how do I commit changes made by the user?
I am extremely confused.
Someone please help!
Thanks.

Actually, that's what I am currently doing.
Here is my cell editor class:
public class ObjectPropertyEditor extends DefaultCellEditor
       public ObjectPropertyEditor()
          super(new JTextField());
          Vector list = new Vector();
          list.add("Yes");
          list.add("No");
         myCombo = new JComboBox(list);
      public Component getTableCellEditorComponent(JTable table, Object value,
          boolean isSelected, int row, int column)
         String colName = (String)table.getValueAt(row,0);
         if(colName.equalsIgnoreCase("Leaf-Node?")) //if it is the "Leaf" property, return the combo box as the editor
             return myCombo;
        else  //for all other properties, use JTextField of the super class
            return super.getTableCellEditorComponent(table,value,isSelected,row,column);
    private JComboBox myCombo;
}The problem I have is that when I select a new item from the combo box, the new selection is not reflected in the tableModel. I don't know how I can achive that. I think I need the functionalities that DefaultCellEditor gives to its delegate when its constructor arguments is a combo box. But how can I get two different sets of functionalities (JTextField and JComboBox) ?
Please help!
Thanks.

Similar Messages

  • Set Traffic Light for Individual Cell in a Row

    Hi,
    I have 3 columns in one Row and I need to show 3 COLUMNS using Traffic lights one with Red, Yellow, Green. How it is possible in SET_TABLE_FOR_FIRST_DISPLAY.
    Regards,
    Deepthi.

    I got the answer:
    Go to SE11 Tcode and ICON and press F7.
    Look for F4 help of field ID.
    See code below:
      if ls_fieldcatalog-fieldname eq 'LIGHT1'
      or ls_fieldcatalog-fieldname eq 'LIGHT2' 
      or ls_fieldcatalog-fieldname eq 'LIGHT3'.
         ls_fieldcatalog-ICON = 'X'.
      endif.
    BEGIN OF t_final,
    bukrs   TYPE bukrs,     "Company Code
    gjahr   TYPE gjahr,     "Fiscal Year
    light1 TYPE ICON_D,
    light2 TYPE ICON_D,
    light3 TYPE ICON_D,
    end of t_final.
    data: i_final type standard table of t_final.
    LOOP AT i_anlp_temp INTO w_anlp.
        IF sy-tabix = 1.
          w_final-bukrs = w_anlp-bukrs.
          w_final-gjahr = w_anlp-gjahr.
        ENDIF.
        CASE w_anlp-peraf.
          WHEN '001'.
            w_final-peraf1 = w_anlp-peraf.
            w_final-light1 = '@0A@'. "RED.
          WHEN '002'.
            w_final-peraf2 = w_anlp-peraf.
            w_final-light2 = '@09@'. "Yellow.
          WHEN '003'.
            w_final-peraf3 = w_anlp-peraf.
            w_final-light3 = '@08@'. "Green.
    endcase.
    append w_final to i_final.
    endloop,

  • Setting JcomboBox editor for a table cell

    Hi,
    I have a ComboBox editor for a cell in my table. I want to set this editor depending on the value of another column
    qualifierTable.addMouseListener(new MouseAdapter(){
       public void mouseClicked(MouseEvent e){
        int selRow = qualifierTable.getSelectedRow();
        int selCol = qualifierTable.getSelectedColumn();
       if (someCondition)
                JComboBox comboBoxEditor = new JComboBox();
                comboBoxEditor.addItem("Private");
                 comboBoxEditor.addItem("Protected");
                 comboBoxEditor.addItem("Public");
                 qualDataValueCol.setCellEditor(new DefaultCellEditor(comboBoxEditor));
                 Object dataValue = comboBoxEditor.getSelectedItem();
                                          if (dataValue!= null)
                 {                              qualifierTablemodel.setValueAt(dataValue, selRow, selCol);
    }The problem is this sets the editor for all cells in the column. However I want the editor to revert to JTextField if this condition is not met. Where should I set it back. It does not work if I set it in the else part

    Override the getCellEditor(...) method to return the appropriate editor. Something like this:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=637581

  • Cell Editor for a JTable within A JTable Cell

    My individual cells each contain a seperate JTable and I need to write a cell editor for it. I'm not really sure how to write one so if someone show me where I can find some useful examples of such a cell editor I would greatly appreciate it. Thank you for your time. :-)

    So, you have a big table whose cells each contain a little table. I assume you already have the little tables working correctly with cell editors and so on, and you want to write a cell editor for the big table. Correct?
    My problem with this is that I can't imagine how this would actually work in practice. Suppose I click on a cell in the big table; then the cell renderer displays the appropriate little table. At this point should I see one of the cells in that little table selected, and should I be able to use the Tab key to move around the little table? And if so, then I can't use the Tab key to move around the big table. Next, suppose I press F2 indicating that I want to edit the little table. What should change? Should I now be able to tab around the little table, whereas I couldn't before? And if so, how do I indicate that I have stopped editing the little table? Pressing Enter indicates to the little table's cell editor that its editing is finished, but how should I tell the big table's cell editor that its editing is finished?
    Perhaps if you can answer this, you may have a better idea of how to solve your problem. Sorry I only have questions and not answers.

  • JTree: How to set different cell editor for different tree Nodes.

    I have a JTree and I want to set different cell editors for different node depending on some condition. E.g. I want to set ComboBox as editor for leaf node but each leaf node will have its own set of data.
    Any help or pointer?
    Thanks in advance
    Sachin

    take there:
    http://www.mutualinstrument.com/Easy/FAQ/Tree/tree.html

  • Various cell renders for cells (not for columns only) in JTable

    Hello, I need to create a property list with some various values (strings, colors, booleans) just like in Netbeans do. I will use JTable component, but I found I cannot have various cell editors for cells. In JTable cell editors can be only changed for COLUMNS. What sould I do?
    ps - is there any JPropertyPanel avaiable for free on the web?

    I had a similar problem recently whilst working on a Swing GUI designer (you're not doing the same are you? ;o). Presuming you already know how to create TableCellEditors you can use a very simple hack to achieve your goal.
    Either extend the JTable class and override the getCellEditor(int row, int column) method, or override the same method when constructing the JTable (this is what I did). You would need to write something like:
    JTable myTable = new JTable(){
        public TableCellEditor getCellEditor(int row, int column) {
            TableModel model = getModel();
            Object data = model.getValueAt(row, column);
            return getDefaultEditor(data.getClass());
    }The getCellEditor method is called by JTable whenever a cell is to edited, normally it would check to see if the TableColumn has an editor associated with it and return that, if there wasn't one it would get the class of data for the column and return the default editor for that type of class, which is similar to what it does here except it returns the default editor for the class of an individual item of datum.
    Now all you need to do is associate your TableCellEditors with the class types they are to edit with the setDefaultEditor(Class c, TableCellEditor editor) method of JTable. If, for instance, you had an editor for the Color class you would have a line something like:
    myTable.setDefaultEditor(Color.class, new ColorCellEditor());Good luck, and I hope this helps.
    MS.

  • Adding ToolTip for individual cell in the table

    Hi everybody,
    Its urgent. I want to add ToolTip for individual cells. What I have implemented, it show same ToolTip for each cell. I want different ToolTip for individual cell.
    My cells are not editable, as i need this.
    Pleae help me.
    Thanks in Advance.
    Dawoodzai

    Hi,
    See this demo pasted below-
    import java.awt.*;
    import javax.swing.*;
    public class SimpleTableDemo extends JFrame {
         public SimpleTableDemo() {
              super("SimpleTableDemo");
              Object[][] data = {
                   {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)},
                   {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},
                   {"Kathy", "Walrath", "Chasing toddlers", new Integer(2), new Boolean(false)},
                   {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)},
                   {"Angela", "Lih", "Teaching high school", new Integer(4), new Boolean(false)}
              String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"};
              final JTable table = new MyTable(data, columnNames);
              table.setPreferredScrollableViewportSize(new Dimension(500, 70));
              JScrollPane scrollPane = new JScrollPane(table);
              getContentPane().add(scrollPane, BorderLayout.CENTER);
         public static void main(String[] args) {
              SimpleTableDemo frame = new SimpleTableDemo();
              frame.pack();
              frame.setVisible(true);
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class MyTable extends JTable {
         public MyTable(Object[][] rowData, Object[] columnNames) {
              super(rowData,columnNames);
         public String getToolTipText(MouseEvent e) {
              int r = rowAtPoint(e.getPoint());
              int c = columnAtPoint(e.getPoint());
              return getValueAt(r,c).toString();
    }

  • How to set exponential format for a cell?

    Hello! Can someone advise how to set exponential format for a cell in Numbers?

    Hi Alejandro,
    If you mean 1000 as 1E+03
    Format Panel > Cell > Data Format > Scientific
    Regards,
    Ian.

  • Creating a Cell Editor for a JTree

    Hi...
    I would like to be able to edit the nodes of a JTree dynamically, but I'm not sure how to do it. I have unsuccessfully tried to implement a celleditor by extended the DefaultTreeCellEditor. The editing works fine, but once the cell has been edited, I get a ClassCastException.
    I know that I need to get the edited value and then make appropriate changes to the DefaultMutableTreeNode object stored, but I am not sure where to do all this. I would really appreciate some tips on where to get the edited value entered, and how I can access the object being edited. Thanks.
    ..VJ..

    So, you have a big table whose cells each contain a little table. I assume you already have the little tables working correctly with cell editors and so on, and you want to write a cell editor for the big table. Correct?
    My problem with this is that I can't imagine how this would actually work in practice. Suppose I click on a cell in the big table; then the cell renderer displays the appropriate little table. At this point should I see one of the cells in that little table selected, and should I be able to use the Tab key to move around the little table? And if so, then I can't use the Tab key to move around the big table. Next, suppose I press F2 indicating that I want to edit the little table. What should change? Should I now be able to tab around the little table, whereas I couldn't before? And if so, how do I indicate that I have stopped editing the little table? Pressing Enter indicates to the little table's cell editor that its editing is finished, but how should I tell the big table's cell editor that its editing is finished?
    Perhaps if you can answer this, you may have a better idea of how to solve your problem. Sorry I only have questions and not answers.

  • Setting a cell renderer for a Cell?

    Hi everybody,
    I'm sitting here with a problem: I'm trying to set a Cell renderer for a cell.
    The problem is in all of the examples that i saw till now this was made only with columns.
    Is it possible to set different CellRenderer foor different cells in the same column?

    Hi,
    if you implement your cellRenderer you return the Object you get for all columns but the one you want to be handled im a special way. As you know the row as well you can return sth different for each row.
    Phil

  • Howto set tool tip for a row in JTable

    I want to set a seperate tool tip for each of the rows in JTable. I tried getting cell renderer of all columns in a row and then setting tool tip for them. I expected that setting tool tip for a cell renderer will set tool tip for that cell. Thus setting tool tip for all the cells in a row will indirectly set tool tip for that row. But it does not happen that way. By setting a tool tip for a renderer it will use that tool tip for all those cells which use that renderer.
    Is there any problem in my approach or we cannot do this in Java.
    regards.

    Thanks.You're wellcome.
    >
    I just saw this method in JComponent and realized this
    solution.
    But then thought that for doing this i'll have to
    create my JTable class for just overiding one method
    thereby increasing one more level of abstraction.Well, you could override the method in an inner class if I understand you correctly in assuming that you don't want to create a JTable as new separate class. You could do this:
    JTable yourtable = new JTable() {
      public String getToolTipText( MouseEvent e ) {
        return getToolTipForRow( rowAtPoint( e.getPoint() ));
    // some where else in your class
    getToolTipForRow( int row ) {
      // determine your tooltip here
    }I think this would be a bit nicer and slightly faster approach because as far as I understand the getToolTipText() method is called after a similar MouseEvent for every JComponent i.e. a small pause of cursor movement over the component in question. I admit that I haven't tested either approach myself but noticed that JTable actually overrides the getToolTipText( MouseEvent e) method already. In your code you determine and set the tooltip every time the mouse moves. If you override getToolTipText( MouseEvent e) you have to determine the tooltip text only when the tooltip is actually about to be shown.
    Regards, JMorko

  • T5SSCXSSSERVICES - Set Data Tracking for Individual Self-Services

    Hi,
    We have a requirement for BI Report to fetch data from table :T5SSCXSSSERVICES
    The above table could be filled with data when we perform any activities in ESS and MSS.
    I wanted to know what are all required configuration to get data in to the table T5SSCXSSSERVICES
    I understand below mentioned configuration are required:
    IMG Node 1: Activate Data Tracking for All Self-Services
    IMG Node2: Set Data Tracking for Individual Self-Services
    Is it required to activate Business FunctionS et - HR Administartive Servcices?
    Guide me with more inputs,
    Regards
    Ramanathan

    Hi Ramanathan,
    I am facing the same doubt now, are you able to share your experience on how to populate  T5SSCXSSSERVICES ? Appreciate that, and thank you in advance.
    Regards
    Kir Chern

  • Table cell Render for one cell

    Hi all
    I am hoping that someone can help me with a cell render for one cell only.
    I am needing to make some cells bold.
    What i was hoping is that there is a way of passing in a row and column number to make that cells font bold?
    Any ideas ??
    I am building a List of accounts that I what to look like this.
    1-0000 Asset |Asset|Balance
    1-1000 Cheque Account |Bank|Balance
    1-2000 Credit Cards |Credit|Balance
    1-2020 Visa |Credit Card|Balance
    1-2030 Master Card |Credit Card|Balance

    Create a renderer like you've done in the past:
    http://forum.java.sun.com/thread.jspa?threadID=679718&start=2
    The renderer knows the row and column you are doing the renderering on so you just need to add a conditional check:
    if (aBoldCell)
    setFont( getFont().deriveFont(...) );
    else
    setFont( table.getFont() );

  • Using editor for multiple cells in JTable

    Hi,
    Here is my requirement -
    On clicking any column of a particular row in a JTable, a pop-up window containing the cells of that row will be displayed as JTextFields for the user to enter the value and on pressing OK, the entered values should be set in the corresponding columns of that row.
    I used a customised editor for this and the problem here is - getTableCellEditorComponent() method returns the cell value which got clicked/selected by the user. But in my case, all the cells have to be edited and returned to the corresponding columns in the table. The same is the problem with the method getCellEditorValue().
    So, please let me know how do i resolve the above issue?
    Thanks in advance!!

    Hi,
    No, the problem is not with showing the popup. I can get the pop-up containing the fields corresponding to each column in the Jtable and in the pop-up window, I have to change the Text fields and press OK.
    After pressing OK, the changed values in the pop-up will be rendered on the corresponding columns of the JTable.
    So, while using the customised editor, when the pop-up is displayed, the method public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) is basically used to return the selected component (cell).
    I can do the above using
    TableModel model = table.getModel();
    model.getValueAt(table.getSelectedRow,column)
    for all the columns but cannot return all these cells. Only one editorComponent can be returned.
    Similarly, after getting the pop-up and changing the values and pressing OK, getCellEditorValue() will be called which returns the edited cell value, again all the cell values cannot be returned here.
    So, is there a way by which all the cells of a particular row can be edited at a time? Hope the explanation is clear now.
    Please help!
    Thanks in advance!!

  • JTable - different editor for each cell?

    I have a JTable where two columns have combo box editors. When the user makes a selection in one combo box, the other combo box's options must change, as its allowable options depend on selection in the other combo box.
    So in short, I need a way of changing the combo box options for ONLY a specific cell, NOT the entire column. I currently only know how to do this for a whole column:
    // first put stuff in myComboBox, and then...
    myJTable.getColumnModel().getColumn().setCellEditor( new DefaultCellEditor( myComboBox ) );
    Is there any way to change the values in the combo box of a specific cell, rather than the entire column?
    I apologize if this is a newbish question... I am fairly new to Swing, but I didn't see anything addressing this in the documentation.
    -Vern

    The solution is like the one I gave you in one of
    your previous postings. In that posting you wanted a
    renderer for a specific cell. In this posting you
    want a editor for a specific cell. The solution is
    the same, only the method name changes.Okay, thanks. Sorry if it seems like I'm a slow learner. ;-)
    -Vern

Maybe you are looking for