Results 1 to 6 of 6
- 04-30-2010, 04:06 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
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.
- 04-30-2010, 04:25 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 04-30-2010, 04:44 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
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?
-
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.
- 04-30-2010, 06:12 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 3
- Rep Power
- 0
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.
- 04-30-2010, 07:33 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
How to repaint.refresh the table (table model) with combo box selection envent
By man4ish in forum AWT / SwingReplies: 1Last Post: 01-08-2010, 06:19 AM -
Can't add columns in table viewer
By feiqu0325 in forum SWT / JFaceReplies: 1Last Post: 03-24-2009, 12:46 PM -
How to resize columns as SWT table resizes
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 03:08 PM -
Adding a progress bar to a table
By djc in forum NetBeansReplies: 3Last Post: 07-11-2008, 12:31 AM -
Getting names of table columns
By Java Tip in forum Java TipReplies: 0Last Post: 01-07-2008, 08:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks