Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Making JTable Non-Editable

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Making JTable Non-Editable

    I want to make non-editable to table.
    I am taking data using DbUtils. I can take the data but I can not make non-editabale.
    try {
                String SQL = "SELECT * FROM tblmalzemeler ";
                preparedStatement = baglanti.prepareStatement(SQL);
                resultSet = preparedStatement.executeQuery();
                        tableMalzeme.setModel(DbUtils.resultSetToTableModel(resultSet)
     
                        );
     
            } catch (Exception e) {
            }


    Probably I should use isCellEditable method but I can not make it. How can I put following code in setModel method.

        boolean[] canEdit = new boolean [] {
            false, false, false, false, false, false, false, false
        };
    public boolean isCellEditable(int row, int col) {  // iscelleditable function  
        return false;
      }


  2. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Making JTable Non-Editable

    You can explicitly override a class that implements TableModel. Or you can use anonymous classes.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making JTable Non-Editable

    Quote Originally Posted by angstrem View Post
    You can explicitly override a class that implements TableModel. Or you can use anonymous classes.
    How can I do class that implemenst TableModel and apply. You can explain this?

  4. #4
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Making JTable Non-Editable

    class Foo implements TableModel {
    // Implement all the methods here
    }
    What's so difficult? Btw, I would recommend you to extend one of the default implementations of TableModel rather then inventing your own.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making JTable Non-Editable

    Quote Originally Posted by angstrem View Post
    class Foo implements TableModel {
    // Implement all the methods here
    }
    What's so difficult? Btw, I would recommend you to extend one of the default implementations of TableModel rather then inventing your own.
    Ok. I declerad the class. and I implement "isCelleditable" method .But I did not apply setmodel

  6. #6
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Making JTable Non-Editable

    And what is your question? How to invoke setmodel on a new model?

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making JTable Non-Editable

    If I don't use "DbUtils.resultSetToTableModel" I can make non-editable.
    How can I make non-editabale using " jTable2.setModel(DbUtils.resultSetToTableModel(res ultSet)"

    jTable2.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null}
        },
        new String [] {
            "Title 1", "Title 2", "Title 3", "Title 4"
        }
    ) {
        boolean[] canEdit = new boolean [] {
            false, false, false, false
        };
     
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });

    How can I declare "isCellEditable" in this method
    tableMalzeme.setModel(DbUtils.resultSetToTableModel(resultSet)
            );

  8. #8
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Making JTable Non-Editable

    Well, you can use, for example, this and this methods to convert your table model to other model. Perhaps, you might want to create a separate method for conversion.

Similar Threads

  1. [SOLVED] TextArea Editable value does not change
    By beer-in-box in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 21st, 2013, 02:17 PM
  2. [SOLVED] Error for JCombobox Editable
    By remedys in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 30th, 2013, 11:09 PM
  3. Editable JTable
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 2nd, 2012, 02:26 PM
  4. Non-Editable DateField in J2me
    By thiruv in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: December 22nd, 2011, 05:03 AM
  5. JTable - multiple lines in a cell AND cells not editable?
    By friday in forum AWT / Java Swing
    Replies: 3
    Last Post: January 10th, 2011, 08:44 AM