Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By camickr

Thread: Stop editing jtable cells programatically

  1. #1
    krimen is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Stop editing jtable cells programatically

    Hi people, I'm developing a program that involves a lot of tables, of course I want to save in a file what the user does, so I save the tables as objects in a file.

    Problem:
    when the cell editor is not working, this is, when the table has no focus at all (and I mean at all because when the Jtables lost the focus, they're still editable, if I write something with the focus on other object, the text appears in the cell I left "open") I can perfectly save the tables as objects in a file.

    When I try to save with the cell editor unclosed, I get an exception (therefore the saving is aborted).

    I really don't know how to close the f***ing cell editor, and I've triyed everything that people say the use on the forums...

    Clear cell editor code (not working) :
    Java Code:
    public void ClearTablesSelection(){
            System.out.println("Stopping cell editors");
            
            LocationTable.getSelectionModel().clearSelection();  
            if (LocationTable.getCellEditor() != null) {
                LocationTable.getCellEditor().stopCellEditing();
            }
           //the same for the rest of the tables...
    }
    My saving code (working with the cell editor closed):
    Java Code:
     public void SaveProject(String Filename){
            // gather all the objects
            ClearTablesSelection();
            
            try{  // Catch errors in I/O if necessary.
                // Open a file to write to, named SavedObjects.sav.
                FileOutputStream saveFile=new FileOutputStream(Filename);
    
                // Create an ObjectOutputStream to put objects into save file.
                ObjectOutputStream save = new ObjectOutputStream(saveFile);
    
                // Now we do the save
                
                //EnableTables(false);           
                
                    save.writeObject(lcm.getModel());
                
                    save.writeObject(wrm.getModel());
                
                    save.writeObject(ws100m.getModel());
                
                    save.writeObject(wcm.getModel());
                
                    save.writeObject(agm.getModel());
                
                    save.writeObject(agp.getModel());
                
                    save.writeObject(rwm.getModel());
    
                // Close the file.
                save.close(); // This also closes saveFile.
                System.out.println("Project succesfully saved to: " + Filename);
            }   
            catch(Exception exc){
                exc.printStackTrace(); // If there was an error, print the info.
            }
        }
    Thanks in advance

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Stop editing jtable cells programatically

    pbrockway2 likes this.

  3. #3
    krimen is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Stop editing jtable cells programatically

    Is this just supposed to work magic?

    where is the procedure?
    if I create a property it should contain something, am I wrong?

    Java Code:
    JTable table = new JTable(...);
    table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

    this just doesn't work
    Java Code:
    if (table.isEditing())
        table.getCellEditor().stopCellEditing();
    To anybody reading this, create an empty frame with a JTable and a JButton, the JTable containing a model with some empty cells, run the program, edit one celll and do not press enter, the click the button to quit the focus from th JTable, then write something, and check if youre editing the cell you left open with the focus on the button or not. This is my problem.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Stop editing jtable cells programatically

    The code from the above link works fine or I wouldn't go to all the trouble of creating the posting.

    To anybody reading this, create an empty frame with a JTable and a JButton, the JTable containing a model with some empty cells, run the program, edit one celll and do not press enter, the click the button to quit the focus from th JTable, then write something, and check if youre editing the cell you left open with the focus on the button or not. This is my problem.
    That is your job. You post the code you wrote to test the problem. Its callled a SSCCE. Short, Self Contained, Correct Example

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Stop editing jtable cells programatically

    The client property and other suggestions in the blog seemed good when I tested them as you described (button both focusable and nonfocusable, since you talked about writing something). So a SSCCE sounds like a very good idea.

Similar Threads

  1. Add datas to cells of JTable ???
    By bilgohan in forum AWT / Swing
    Replies: 5
    Last Post: 02-22-2010, 10:56 AM
  2. Coloring JTable cells
    By ProgrammingPup in forum Advanced Java
    Replies: 2
    Last Post: 11-04-2009, 11:57 PM
  3. How to merge cells of JTable
    By nehaa in forum AWT / Swing
    Replies: 1
    Last Post: 05-19-2009, 02:07 PM
  4. how to merge cells /colspan in Jtable
    By ravrajesh.ap in forum AWT / Swing
    Replies: 0
    Last Post: 01-03-2009, 05:25 PM
  5. Getting objects from JTable Cells.
    By girigl in forum AWT / Swing
    Replies: 3
    Last Post: 12-24-2008, 11:46 AM

Tags for this Thread

Posting Permissions

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