Help on Editing a Cell in a JTable

Hi Folks,
I have a JTable with two Columns and Some number of rows. My Second Column is Editable i.e., I can change the contents of the cell in the same. My Problem is, When I Select the particular cell in Column2 for Editing it starts appending the text I have typed after the text already present in the Selected Cell.Instead I wanted it to overwrite the previous text present in that cell.(Just like the effect we get in MS Excel). Now What I have to do is press F2 and then press CTRL+A to select all the text in the selected Cell and do the editing. This is somewhat a pain in the Ass to do if you have to Edit a huge table. Is there a Solution for the above problem wherein as soon as I select the cell to edit, it should start overwriting on the previous text instead of appending to the same.There are duke dollars in the offing. Anybody interested in helping me out...???
Thanks in Advance...
Sachin Shanbhag

Hi,
I am using a JTable with cell editor using TableCellEditor.My table consists of two columns of which first one is using JTextField and the second column ( only the for the column of newly added row) using combobox as editors.My requirement is when a cell gets a focus the cell text should be selected automatically (only for the first column which is using JTextField)) with out the user explicitly going to the focused cell and select the text using mouse.
If anybody can put some idea on this then it would be fine.
Thanks
My code is like this.
public class PurposesTableEditor implements TableCellEditor
          private TableCellEditor editor;
          private TableCellEditor defaultEditor;
          * HashTable variable for storing editors.
          private Hashtable editors;
          * Constructor.
          public PurposesTableEditor()
               editors = new Hashtable();
               defaultEditor = new DefaultCellEditor(new JTextField());
          public void add(int row , TableCellEditor editor)
               editors.put(new Integer(row),editor);
          public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,int row,int col)
               editor = (TableCellEditor)editors.get(new Integer(row));
               if (editor == null)
                    editor = defaultEditor;
               return editor.getTableCellEditorComponent(table, value, isSelected, row, col);
          public boolean stopCellEditing()
               table.getCellRect(table.getSelectedRow(),0,false);
               table.getCellRect(table.getRowCount()-1, 0, false);
               return editor.stopCellEditing();
          public void cancelCellEditing()
               editor.cancelCellEditing();
          public boolean isCellEditable(EventObject anEvent)
               return true;
          public boolean shouldSelectCell(EventObject anEvent)
               return editor.shouldSelectCell(anEvent);
          public void addCellEditorListener(CellEditorListener l)
               editor.addCellEditorListener(l);
          public Object getCellEditorValue()
               return editor.getCellEditorValue();
          public void removeCellEditorListener(CellEditorListener l)
               editor.removeCellEditorListener(l);
     }

Similar Messages

  • Help with editing a cell in a JTable. DESPERATE

    Hi! Is there someone out there that could help with some source code for editing a cell in my JTable when I run it as an applet??
    It works fine when I run it as an application.
    I manage to select the row and write a number in it, but I can't get out of the cell (it seems that the program stops??). I want to enter an integer in a cell and when I click enter or with the mouse in an other cell the value that I enter should update some other cell( multiply the entered value with some fixed number and update a cell in a second coloumn)
    I am really desperate now..... I have thought about using a MouseListener....
    I am using a tablemodel that is from AbstractTableModel.

    Are you using some cell editors?
    While converting inside them, you might be getting some exceptions (like parseexception)which is stopping you from proceeding further.
    Are you using your own tablemodel with custom datatypes?
    Hope this helps,
    Ramkumar

  • How? double click to edit a cell in a JTable (Custom Editor/TableModel)

    I have a JTable with a custom table model that when you click anything in the first column a custom editor appears. I would like to know how to make the custom editor appear after a double click on any cell in the first column. It can probably be done with a MouseListener but is there any easier way to do this?
    Thanks.

    this works for me.
    public class MyJcustomEditor extends DefaultCellEditor {
    public MyJcustomEditor(JTextField tField) {
    super(tField);
    setClickCountToStart(2);
    }

  • Edit table cells

    i have edited the setValueAt method of the TableModel interface to enable editing of cells in my JTable - in-situ. This works fine. However, i would like it so that when any change is made to the table data, it is automatically sorted depending on how it was sorted before the change was made. i have:
    public void setValueAt(Object aValue, int row, int col) {
            if (col == 0) {
                bookList[row].setBookNumber(Integer.parseInt((String)aValue)); // Populates cell with new book number
                if (lastSort == "number") {
                    listByNumber();
            } else {
                if (col == 1) {
                    bookList[row].setTitle((String)aValue); // Populates cell with new book title
                } else {
                    if (col == 2) {
                        bookList[row].setAuthorPrename((String)aValue); // Populates cell with new author prename
                    } else {
                        bookList[row].setAuthorSurname((String)aValue); // Populates cell with new author surname
        }i would put more if statements in later for different sort methods but at the moment ill just stick with one. My problem is that the array is being sorted correctly, but the table doesn't update automatically to show the change. what DOES happen - is when you click on each row of the table, it changes until it is in the correct order. this leads me to think that the sorting works fine, but somehow my table model isnt reflecting the changes? can any help me update the table in real time?

    no luck :( i tried:
       public void setValueAt(Object aValue, int row, int col) {
            if (col == 0) {
                bookList[row].setBookNumber(Integer.parseInt((String)aValue)); // Populates cell with new book number  
                listByNumber();
                fireTableDataChanged();and extended AstractTableModel but it still didnt work. is there anything else i would need to do?

  • Programmatically Editing a Cell Not Working

    Hi Guys,
    I'm trying to automatically begin editing a cell within a JTable as soon as the table is displayed.
    I've tried using the table.editCellAt(int row, int col) method but it just seems to have no effect.
    I've set the cell editable within my custom table class (and I know it is because if I double click on the cell or type anything then the editor is displayed).
    FYI I'm using Java 1.5
    Anyone got any ideas?

    Does nobody know how to automatically engage the editor component of a JTable programatically i.e. without having to click on the cell?? The code I gave you above works fine for me in JDK1.4.2. Why don't you post the 10 line demo program you are using to test my suggestion. That way people can determine whether you test code is wrong or 1.5 isn't working.

  • Default behaviour of the Escape key while editing a cell in JTable??

    Hi all,
    i have a Jtable which get its data from an own model object which extends DefaultTableModel.
    If i overwrite the isCellEditable(row, col) method within the tablemodel object and set a specific column to editable, then i notice while editing a cell in that column that the default behaviour of the Escape key is that independet from what you have entered before in to the cell the editing stops and the cell gets the value it had before editing.
    This is the case for me even if i apply a custom editor to a column (one that extends DefaultCellEditor). It is just a JTextField that limits the number of digits a user can enter. Everything works fine. If the user edits the cell and presses ENTER or the "down arrow key" i can check what he has entered with the help of the getCellEditorValue() method. But if the user hits the ESC key after editing a cell this method is not invoked!!!
    My question is :
    is there any way to detect that the user cancels editing with the ESC-key.
    this is very important for me because if the user goes editing the cell i lock the related record in the database, if i cannot detect this it is locked till the application terminates.
    Thanks for any help in advance

    I try override the JTable editingCanceled() ==> does not work.
    I try the addCellEditorListener( CellEditorListener l ) ==> does not work.
    Finally, I try the addKeyListener ==> it works.
    Here is a quick demo. program:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    public class Test {
    public static void main(String[] args){
    JFrame f = new JFrame();
    String[] colName = {"a", "b"};
    String[][] rowData = {{"1", "2"}, {"3", "4"}};
    JTable table = new JTable(rowData, colName);
    JTextField t = new JTextField(10);
    t.setBackground(Color.red);
    t.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
    // do what ever you want ex. un-lock table
    System.out.println("ESCAPE");
    DefaultCellEditor editor = new DefaultCellEditor(t);
    TableColumnModel colModel = table.getColumnModel();
    for (int i = colModel.getColumnCount()-1; i >= 0; i--) {
    colModel.getColumn(i).setCellEditor(editor);
    f.setContentPane(new JScrollPane(table));
    f.pack();
    f.setVisible(true);

  • How do I update a JTable immediately after editing a cell?

    I have a JTable where I use a JComboBox to select a value in a cell. When a value is selected this updates another cell in the JTable.
    This works like I want to except for that I have to deselect the JComboBox (ie. click in another cell) before the other cell is updated.
    I want the cell to be updated when the JComboBox changes without having to deselect the JComboBox first.
    How can I do this?
    Thanks in advance for any help...
    Geir

    As an example I just copied the TableRenderDemo from the Swing-tutorial (http://java.sun.com/docs/books/tutorial/uiswing/components/table.html).
    The only change I have made to the TableRenderDemo is to replace the DefaultCellEditor with my own editor (ComboBoxEditor extends DefaultCellEditor).
    The only change I have made to the program itself is therefore to replace
    sportColumn.setCellEditor(new DefaultCellEditor(comboBox));*
    with
    sportColumn.setCellEditor(new ComboBoxEditor());*
    (And add the class ComboBoxEditor, of course.)
    When you run the original program, the default editor closes itself when a value is selected from the combobox. My editor doesen't close itself when a value is selected, but remais open until you click on another tablecell. I want my editor to close itself in the same way the DefaultCellEditor does.
    The code for TableRenderDemo is here: http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TableRenderDemoProject/src/components/TableRenderDemo.java
    Again, to test what I mean just replace
    sportColumn.setCellEditor(new DefaultCellEditor(comboBox));*
    with
    sportColumn.setCellEditor(new ComboBoxEditor());*
    (And here is the code for my own celleditor:)
    private class ComboBoxEditor extends DefaultCellEditor {
    private JComboBox comboBox = null;
    public ComboBoxEditor(){
    super(new JComboBox());
    @Override public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,int row, int column){
    comboBox=new JComboBox();
    String name=(String)table.getValueAt(row,0);
    if(name.equals("Mary")){
    comboBox.addItem("Marys sport");
    comboBox.addItem("Marys cooking");
    else{
    comboBox.addItem("Other sport");
    comboBox.addItem("Other cooking");
    return comboBox;
    //else return super.getTableCellEditorComponent(table,value,isSelected,row,column);
    public void setCellEditorValue(Object newData){
    comboBox = (JComboBox) newData;
    @Override public Object getCellEditorValue(){
    return comboBox;
    }//end class

  • JTable edits wrong cell on mouseclick

    I am using an editable JComboBox as my cell editor. When the user is editing a cell and clicks on another cell with the mouse, the value of the cell they were editing is inserted into the cell that they clicked on. Can anyone please help me. Thanks.

    The Swing tutorial has a working example:
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#combobox

  • How to make a cell in a JTable to be uneditable and handle events

    I tried many things but failed,How do you make a cell in a JTable to be uneditable and also be able to handle events>Anyone who knows this please help.Thanx

    Hello Klaas2001
    You can add KeyListener ,MouseListener
    Suppose you have set the value of cell using setValueAt()
    table.addKeyListener(this);
    public void keyTyped(KeyEvent src)
    String val="";
    int r= table.getEditingRow();
    int c= table.getEditingColumn();
    val=table.getValueAt(r,c).toString();
    if (r!=-1 && c!=-1)
    TableCellEditor tableCellEditor = table.getCellEditor(r, c);
    tableCellEditor.stopCellEditing();
    table.clearSelection();
    table.setValueAt(val,r,c);
    public void keyReleased(KeyEvent src)
    public void keyPressed(KeyEvent src)
    table.addMouseListener(this);
    public void mouseClicked(MouseEvent e)
    public void mouseEntered(MouseEvent e)
    public void mouseExited(MouseEvent e)
    public void mousePressed(MouseEvent e)
    public void mouseReleased(MouseEvent e)
    if(e.getClickCount()>=2)//Double Click
    table.clearSelection();
    int r= table.getEditingRow();
    int c= table.getEditingColumn();
    if (r!=-1 && c!=-1)
    TableCellEditor tableCellEditor = table.getCellEditor (
    r,c);
    tableCellEditor.stopCellEditing();
    table.clearSelection();
    table.setValueAt("",r,c);
    }//Mouse Released
    You can remove keyListener and Mouse Listener whenever You want to edit
    then add it later.
    Regarding handling events implement javax.swing.event.TableModelListener
    table.getModel().addTableModelListener(this);
    public void tableChanged(javax.swing.event.TableModelEvent source)
    TableModel tabMod = (TableModel)source.getSource();
         switch (source.getType())
    case TableModelEvent.UPDATE:
         break;
         }//Table Changed Method
    //This method gets fired after table cell value is changed.

  • Editing the values of a JTable

    I am trying to allow the user to edit the values of a JTable. I have captured the event of editing the table, but I can't find out how to know what the new value of cell is... it always gets reverted to the old value.
    I can't find any method or way how to know what the new value is.
    Any help?

    Problem solved ...
    setValueAt() was not overridden ...

  • Problem of JComboBox as cell editor in JTable

    Hi,
    I use JComboBox as cell editor in JTable. If the drop-down menu of the JComboBox out of the JTable area (as the editable cell is near the bottom of the JTable), the item in JComboBox can not be selected with mouse, in this situation there is no MouseEvent to be received in JTable. But it works when I use the keyboard to choose an item in JComboBox.

    Works fine for me.
    If you need further help then you will need to provide [url http://www.physci.org/codes/sscce.jsp]Simple Demo Code that demonstrates the problem.

  • How do I change the colour of a selected cell in a jTable?

    I have a Jtable that displays URL names in one column. There are several problems I'm having. The effect I'm trying to achieve is this:
    When the user runs the mouse over the URL name the cursor should change into a hand (similar to what happens in an HTML hyperlink). I'm aware that the Cursor class can set the cursor graphic so i figure that i need a listener of some sort on each cell (so the cursor can change from an arrow to a hand) and also one to indicate when the cursor is not on a cell (so that it can change from a hand back into an arrow). Is this the right track?
    Also, I've looked at the DefaultTableCellRenderer class (which, as i understand it, is responsible for how each cell in the jtable is displayed) for a method that will allow me to set the background of a selected cell (or row or column). I require this because each time i select a cell (or row) it becomes highlighted in blue. I would rather it just remained white and changed the cursor to a hand. I know there exists a method for setting the background for an unselected cell but none for a selected cell. Again, I'm not sure if I'm going down the right track with this approach.
    Lastly, if the cell has been selected (by a mouse click) the font of the writing in the cell (i.e. The name of the URL) should change. This shouldn't be too much of a problem I think.
    I do not expect anyone to provide code to do all of this but some general pointers would be extremely helpful as I do not know if I'm thinking on the right track for any of this. Having some (limited) experience with Swing I doubt there is a simple way to do this but I can only hope!
    Thanks.
    Chris

    http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
    there you can find some examples with CellRenderer's and so on ...
    have fun

  • How to get the coordinate of a cell in the jtable?

    How to get the coordinate of a cell in the jtable?
    How to get the point of a cell in the jtable?
    Thanks for help!

    getCellRect(...);

  • How to enable JScrollPane in a cell of the JTable

    Hi,
    I am able to place the components into the cell of the JTable. But I am unable to interact with the components like JTable or JComboBox after inseting them into a cell of the JTable.
    This was the one of the Scenario:
    Step 1: I created One JTable named as "insertTable"and adding to JScrollPane
    Step 2: I'm able to inserted the newly created JTable ("insertTable") in to a cell of another JTable(like table inserting a table) using TableCellRenderer.
    here was the problem. I am able to insert the newly created table into the cell. the "insertTable" size is greater the Cell size. So, the "insertTable" is appering with Horizantal and Vertical Scrollbars because the "insertTable" is added to JScrollBar. but I am unable to move the scrollbars.
    please any one help me for this.

    you still didn't try to learn the difference between cellEditor vs cellRenderer - as you were advised to do more than once in recent posts.
    If you do, the answer will be obvious (to you :-). As long as you don't there's nothing to help
    Cheers
    Jeanette

  • JComboBox as a cell editor in JTable

    Hi,
    I've set one of my columns in my table to have a JComboBox as a cell editor. This is fine but my problem is that each row has different values in the JComboBox but when I update each row the column with the JComboBox they all end up with the same values.
    All help appreciated,
    Thanks,
    Lou
    // column with JComboBox
    TableColumn myCol = table.getColumnModel().getColumn(3);
    Vector comboValues = new Vector();
    JComboBox myCombo = null;
    for (int row = 0; row < tableModel.getRowCount(); row++)
    comboValues = new Vector();
    if (row == 0)
    // code here to add values to comboValues
    myCombo = (JComboBox) (((DefaultCellEditor) (table.getCellEditor(row, 3, .getComponent());
    myCombo = new JComboBox(comboValues);
    myCol.setCellEditor(new DefaultCellEditor(myCombo);
    tableModel.fireTableStructureChanged();
    tableModel.fireTableDataChanged();
    else
    // get differnent values for comboValues and update comboValues Vector
    myCombo = (JComboBox) (((DefaultCellEditor) (table.getCellEditor(row, 3, .getComponent());
    myCombo = new JComboBox(comboValues);
    myCol.setCellEditor(new DefaultCellEditor(myCombo);
    tableModel.fireTableStructureChanged();
    tableModel.fireTableDataChanged();

    Remember that you Table API specifies that each cell will use one instance of the specified editor...
    Think about that... the same editor is being used for each cell in that column... therefore, setting the
    data in the combobox et all will set it for that instance bveing used to edit the cell....
    Check out [url http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender]How to use Tables if in doubt...

Maybe you are looking for

  • PDSN bandwidth monitoring

    I am facing and issues with a PDSN 7204 Router Basicly we are very closed to our Bandwidth limit with our Internet Service provider We think we should be around 5 to 8Mbit/s but we are at a steady 35Mbit/s We have seen a significant increase in bandw

  • Stacked format in chart using graphics:chart

    I Implemented a graphics:chart element and is working fine, I know how to set Pie format, bars, line in 2D or 3D, but I cant find info about how to set the stacked format. I read the PDF that comes with the SAP chart Designer. In the section “Blocks”

  • Slideshows (pics) in iDVD, can i put them to fit the screen

    when im doing a video project in iDVD, if id like to do a slideshow, is it possible for the photos to fit to the screen and do the same effects (dissolve, page flip) as iPhoto?.. Im mostly concered for the pictures to fit the screen (when im going to

  • Read the entire content of XML Form progamatically

    Hi, I want to write a program which can read the entire content of XML Form. In some of the xml form, some entity like Title etc are coming from Metadata property. But i want to read everything through the java code. I can read the xml file, but it d

  • Version of Business Connector Supported

    Hi , What version of Business Connector supported in xMII? Is it possible to connect SAP ECC6.0 with the BC 4.7 using xMII? Thanks, Bala