Results 1 to 6 of 6
  1. #1
    joker.med is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default After adding new columns to my Table

    Hello everybody,
    After I created a JTable with:
    JTable MaTable =new JTable();
    MaTable.setModel(new DefaultTableModel(new Object [N][1], new String [1]));

    I wanted to add new columns to it, so I did this:
    TableColumn Colonne = new TableColumn();
    MaTable.addColumn(Colonne);

    The operation did worked, even the new columns displayed correctly, but, when I set a value for one of the new cells, all the others (newly created) cells are modified!
    It's like all new cells have the same index.
    Did I miss something in my code that should correct the index? (if the problem is with the index).
    I'm waiting for your precious help.

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

    Default

    First of all, use proper Java naming conventions. Variable name should NOT start with a capital letter. "N", "Colonne", "MaTable" are all wrong.

    Yes the problem is with the index. You did not tell the TableColumn which column in the model it refers to, so it defaults to 0.

    The easier solution is to use DefaultTableModel.addColumn(...) method and you don't have to worry about this.

  3. #3
    joker.med is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank you camickr for your quick respond, surely I'm a newbie in java programming.
    When I used DefaultTableModel.addColumn(colonne) like you've suggested, I got an error "non-static method addCollumn cannot be referenced from a static context".
    And how can I tell the TableColumn wich column I'm referring?

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

    Default

    You're calling addColumn on the class not on an object of the class. You may wish to read the Sun tutorial on classes and objects and how to use them.

  5. #5
    joker.med is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Smile Problem Solved

    Here's the solution if you're interested.
    Like I suspected, it was an Index problem. And just like camickr said, I realized that I didn't tell TableColumn where to refer the new columns, so it defaults to 0 (or something common for all new cells). I should-ed type TableColumn colonne = new TableColumn(diagramme.getColumnCount());
    So TableColumn refer every new column at the end of the table (that makes sens).
    So, that was the major problem, but I did modified the code in order to work properly:
    diagramme.setAutoCreateColumnsFromModel(false);
    DefaultTableModel model = DefaultTableModel)diagramme.getModel();
    TableColumn colonne = new TableColumn(diagramme.getColumnCount());
    diagramme.addColumn(colonne);
    model.addColumn(colonne);

    Thank you all for your precious help.

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

    Default

    Or you can do it in one like of code like I suggested:

    Java Code:
    model.addColumn("Added Column");

Similar Threads

  1. Replies: 1
    Last Post: 01-08-2010, 06:19 AM
  2. Can't add columns in table viewer
    By feiqu0325 in forum SWT / JFace
    Replies: 1
    Last Post: 03-24-2009, 12:46 PM
  3. Replies: 0
    Last Post: 07-11-2008, 03:08 PM
  4. Adding a progress bar to a table
    By djc in forum NetBeans
    Replies: 3
    Last Post: 07-11-2008, 12:31 AM
  5. Getting names of table columns
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:39 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
  •