CellRenderer / Editor for single cell in JTable

        table.setDefaultRenderer(Color.class,
                                 new ColorRenderer(true));I thought the code above would set up ColorRenderer() to be run if, and only if, a specific cell contains the class in the first input paramter (in this case, java.awt.Class).
However, this doesn't seem to be the case. If I have one column with various classes in them (eg. java.awt.Dimension, etc.) the renderer for the Color.class is run every time! ..
This happens if you eg. change the data in the example:
http://java.sun.com/docs/books/tutorial/uiswing/components/example-swing/TableDialogEditDemo.java
to contain one element in the color colum that's eg. a java.awt.Dimension (you will get a ClassCastException).
How come this happens? This can't be the way it should be ...

Yeah .. but that wouldn't be as object oriented
thoughAgreed.
I had a look at the source of JTable. The problem seems to be the method:
public TableCellRenderer getCellRenderer(int row, int column)
that makes no use of the row argument.
I think you could fix this by subclassing JTable and overriding this method with something like:
public TableCellRenderer getCellRenderer(int row, int column)
    Object obj = dataModel.getValueAt(row, column);
    TableCellRenderer renderer getDefaultRenderer(obj.getClass());
    return renderer;
}Let me know how you get on.
Regards.

Similar Messages

  • 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.

  • 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!!

  • Set_cell_Editor for single cells rather then complete column

    Is it possible to set a cell editor (eg a link) only for single cells of a column.
    Regards
    Rene

    hi rene.....
            you cannot do that......
            may be you can have an extral column called enabled or disabled and bind that property to the enabled property of the cell editor so that only one cell will be enabled while others disabled.
    ---regards,
       alex b justin

  • 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

  • In dreamweaver CS4, I can't make border for single cell

    Hi folks,
    Having given up trying to make a single cell border in the dreamweaver CS4 template, I reverted to an html page and put in my own header, 2 columns and footer as cells in the html table. I know there's html code for adding borders to single cell, but I can't find it. I hope someone can give me a sample line of html code for giving a cell a border on left side and on right side, including 1px solid color border. Anyone?

    Not sure if this is what you want, at least it will give you an example
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <style>
    body {width: 980px; margin: auto; background: #FEE49A;}
    #header {height: 120px; background: #060;}
    #article {height: 400px; width: 749px; float: right; background: #FFF; border-right: 1px solid #060;}
    #aside {height: 400px; width: 228px;    float: left; border-right: 1px solid #060; border-left: 1px solid #060; background: #CCC;}
    #footer {height: 50px; background: #060; clear: both;}
    </style>
    </head>
    <body>
    <div id="header"></div>
    <div id="aside"></div>
    <div id="article"></div>
    <div id="footer"></div>
    </body>
    </html>
    Gramps

  • Aligning the Text in Centre for MultiLine Cells in JTable

    Hi;
    Does anyone know how I could make a java table that has multiline text cells AND this multiline text is aligned to center? I tried to extend JTextArea and to implement TableRenderer - multiline was OK but it did not do anything on setHorizontalAlign. If I extend DefaultTableRender than I can align the text to center but I got no multiline...
    Any ideas?
    Kindly to provide me a solution for this its urgent.

    Hi;
    Thanks for the reply,on using Text Area as renderer i.e using Multi-Line cells in JTable, i achieved the objective of displaying the text on multiple lines in the cell."In my Table there are 5 columns and i'm setting renderer on 3rd and 5th column,If the text is too large in column 5 it automatically gets wrapped off for all the cells in column 5.For column 3 my text is not so large as compared to column 5 and due to which the Text in the column 3 starts from the top for all the cells i.e (the first row in column 3 is parallel to first row in column 5 ),Now the problem is i can't display the text in the center of all cells for Column 3 .
    Thanks in advance

  • Creating a custom editor for each cell in a Jtable

    camirk, i know its swing ;) so
    http://forum.java.sun.com/thread.jspa?threadID=652383
    im just posting this link here for people who dont read the swing forum but are looking for the same solution
    hope it helps someone :)

    Two days after you posting you original question: http://forum.java.sun.com/thread.jspa?threadID=651625&messageID=3831712
    you find the answer on your own. Yes, the general Java forum sure is helpfull.
    If the question was posted in the Swing forum you would have had the answer in a couple of hours:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=637581

  • SmartForms: Template: Box for Single Cell

    Hi all,
    I am using template of one row and two Column, I want to draw a box around for Column(1,1), where don't want to draw any box around Column(1,2).
    Which pattern should i select so that i can achieve this?
    Thanks in advance.

    Make two box one with the border and the other without.
    Sincerely

  • Setting background for single row in JTable

    Hi,
    I have a table which uses the DefaultCellRenderer for cols0,1,3-5
    while column 2 uses a custom renderer.
    When user right clicks mouse on one of the rows i get the rowindex and colindex selected and tries to set background color for this row. but the whole table get painted(of course except 2nd column)...rather i need just the row to be colored.....any solution is greatly appreciated.the code is below....
    int rowIndex=table.rowAtPoint(p);
         int colIndex = table.columnAtPoint(p);           
         for (int i = 0; i < 6; i++) {                    
              if (i!=2){
                   DefaultTableCellRenderer r = (DefaultTableCellRenderer) table.getCellRenderer(rowIndex,i);
                   Component c = taskListTbl.prepareRenderer(r, rowIndex, i);
    c.setBackground(Color.yellow);
              }

    Maybe my reply in this posting will help:
    http://forum.java.sun.com/thread.jspa?forumID=57&messageID=1730267

  • 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

  • Setting value in a cell in JTable where editor for cell is a JComboBox

    Hi,
    I have a column in a JTable where it holds a comboBox . As the comboBox has different
    values depending on the row I've a cell editor for each row. That's fine. My problem is that one of the
    values on the comboBox launches a dialog where the user selects a new value to add to the comboBox.
    The comboBox gets updated with this new value and I call setValueAt on the table. Both get updated
    but the cell doesn't display the new value added in the JTable. I click it and the comboBox has the
    value and I've to select it to get it displayed in the cell.
    Here's the code for updating the tableModel -
    public void updateCellDetails(String value, int row)
    JComboBox combo = (JComboBox) rowEditor.getTableCellEditorComponent(
                        this,
                        value,
                        true,
                        row,
                        4);
    if (combo != null)
    combo.insertItemAt(value, combo.getItemCount() - 1);
    combo.setSelectedItem(value);
    rowEditor.setEditorAt(row, new DefaultCellEditor(combo));
    MyTableModel tableModel = (MyTableModel) getModel();
    tableModel.setValueAt(value, row, 4);
    Thanks

    Not sure if this'll help, but did you ever fire an editing-stopped event? If such an event is never fired, then editing-cancelled will be fired when you lose focus from the editor and the original value will be restored.

  • 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.

  • Unable to edit cells in JTable on single click of the cell.

    Hi,
    I am unable to edit a cell in JTable on single click of the cell. If I double click on the cell, I am able to edit it. Please help me.
    Thanks
    Subbu

    Thanks for all replies. Now, i am able to edit the cell on single click.

  • 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.

Maybe you are looking for

  • Re: How to connect camcorder to my Satellite L300-1AD?

    Hi everyone I hope someone out there can help me - my problem is driving me mad!! I have a Satellite L300 1AD(?) laptop and I am trying to connect my camcorder to it so that I can watch and edit my holiday videos. Apparently I can't use a USB cable a

  • What is happening with quote substitution in Mail since Mountain Lion?

    What is happening with quote substitution in Mail since Mountain Lion? Now, whenever I type an apostrophe and it's replaced with a smart quote, it creates an unwanted space within my word and starts typing from there. It will also revert the font fro

  • Preloader not working in IE.

    I created a preloader for an external swf. It works just fine in Firefox but when i tried to view it in IE the progress bar doesnt move at all. I need help to know why this isnt working, or at least an alternative way of doing a preloader that works

  • Help req in Seeburger SFTP adapter

    Hi All,             I have a scenario where i need to send a file to SFTP server. I used Seeburger SFTP adapter to connect to SFTP.           This is a pass through interface and i havent used any party at the confiuration( normal configuration witho

  • Query doesn't work.

    I am declarin a cursor like this inside a pl\sql editor in Oracle Forms 6i: cursor customer_services is select * from Customers inner join Services on Customers.ServiceID=Services.ServiceID. And when i compile the followin error appears: Error 103: E