Results 1 to 8 of 8
  1. #1
    adra is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default Using JTable without extending or overriding

    Hello all,
    I am writing a small program in Matlab. My program has a JTable in it which i have built from pure java classes. Here is a sample Matlab code (you will see that code is same with Java);

    Java Code:
    frame=javax.swing.JFrame('table');
    model=javax.swing.table.DefaultTableModel({1,'a';2,'b'},{'int','str'});
    table=javax.swing.JTable(model);
    sorter=javax.swing.table.TableRowSorter(model);
    table.setRowSorter(sorter);
    pane=javax.swing.JScrollPane(table);
    frame.add(pane,java.awt.BorderLayout.CENTER);
    frame.setSize(300,250);
    frame.setVisible(true);

    As you can see java objects can be used within Matlab. Now my problem is i couldnt make my table uneditable. I know that it is fairly easy doing it in Java by overriding isCellEditable method so that it returns false. However it is not possible to extend or override a Java class in Matlab (as far as i know). So i need to find another solution. Is there any other way of doing it right without overriding or extending? The only work around solution i could find is making uneditable the textfields of every columns like this;

    Java Code:
    textfield=javax.swing.JTextField;
    textfield.setEditable(0);
    jtable.getColumnModel.getColumn(0).setCellEditor(javax.swing.DefaultCellEditor(textfield));
    That forbids editing but when any keyboard key is pressed, cell is entered everytime (even though it cant be edited). There is a method in cellEditor named setClickCountToStart, i gave a big number so cell cannot be entered in with mouse click but i also need to disable editing with key press now. Is it possible?


    Thanks,
    Arda

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why not create your own Java classes in Java, and then have MatLab call objects of your own classes? Then you can subclass JTable without problem.

    Edit: this article tells how to integrate your custom java classes with MatLab: Integrating MatLab with Perl and Java
    Last edited by Fubarable; 08-24-2011 at 10:10 PM.

  3. #3
    adra is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    I have no clue about how to that. In fact i dont know Java at all and that is why i am trying to do it in Matlab.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by adra View Post
    I have no clue about how to that. In fact i dont know Java at all and that is why i am trying to do it in Matlab.
    Well your first question is impossible to solve without knowing Java, and so the solution is obvious: if you want to use Java to its full capacity, your best option is to learn Java. The Java tutorials can help you with this.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Here is a sample Matlab code (you will see that code is same with Java);
    No, I don't see that at all. The syntax is quite different.

    db

  6. #6
    adra is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Well your first question is impossible to solve without knowing Java, and so the solution is obvious: if you want to use Java to its full capacity, your best option is to learn Java. The Java tutorials can help you with this.

    @Fubarable, thank you for your help and advice. However learning java is a long term shot (which i am confident to do). Today i need to save the day. I know it wont be easy but if you could tell me how to do (or better give some piece of code), i would at least try.

    I dont know java yes, but with the help of tutorials and some example applications i have already completed other objects in my gui like menubar, splitpane, frame, etc. I have also customized my jtable except the two things; implementing a checkbox column and making my table uneditable. Using a checkbox column is a thing that i can sacrifice (if you have a solution i would be very glad though) but i have to make my table uneditable.

    I even found a workaround solution to my problem as i described in my first post; setting ClickCountToStart method to 100 (or any other large number) for celleditors of every column disables editing for mouse in practice. But when i type a key it still tries to edit the cell. All i need to do is to forbid editing with keyboard. I have tried the following and it worked but now Matlab throws error every time i push a key..
    Java Code:
    table.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
    So i am not comfortable with that solution, there has to be another way of doing it..

  7. #7
    adra is offline Member
    Join Date
    Aug 2011
    Posts
    4
    Rep Power
    0

    Default

    Does anybody know how to disable editing for a column without overriding the getColumnClass method or extending the AbstractTableModel?

    I would be really appreciated for some help..

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by adra View Post
    Does anybody know how to disable editing for a column without overriding the getColumnClass method or extending the AbstractTableModel?
    No.

    I would extend JTable or DefaultTableModel and override isCellEditable.
    Last edited by Fubarable; 08-29-2011 at 04:25 AM.

Similar Threads

  1. Confused about Overriding - Please Help
    By jazzermonty in forum New To Java
    Replies: 15
    Last Post: 01-24-2011, 05:20 PM
  2. Extending Classes and What is Necessary
    By GavinCash in forum New To Java
    Replies: 10
    Last Post: 10-11-2010, 07:07 AM
  3. Overriding
    By prasanna1157 in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 07:47 AM
  4. Overriding
    By renuka_renukut in forum Advanced Java
    Replies: 3
    Last Post: 05-21-2010, 08:45 AM
  5. Implementing and Extending together
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 09:49 AM

Posting Permissions

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