Hello,
I would like to be able to add 'n' columns programmatically and SET THEIR TITLE as I add them. I have 2 questions:
1. How can I change the title? (not by seting a model to my table but just 'changing a property' - if possible)?
2. Is there an easiest way of adding columns than what I did below? (e.g.: in C# I find very easy methods of adding columns at the end of a listView or after a column by index).

//'txt_n' is a textfield defining the number of columns I want to add when I click 'jButton1'
//table is my JTable
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        for(int i=1;i<=Integer.parseInt(txt_n.getText());i++)
              table.addColumn(new TableColumn(table.getColumnCount()-i));
    }

Recommended Answers

All 4 Replies

Have you tried setHeaderValue("..."); on your new TableColumn?

Oh... I must admit I tried like this:

//doesn't work
table.addColumn(new TableColumn(table.getColumnCount()-i).setHeaderValue(Integer.toString(i)));

//this works and I tried it after your post :) 
 table.getColumnModel().getColumn(i-1).setHeaderValue(Integer.toString(i));

Thanks :D. What about the second question? Any more ideas or the same as mine?

yeah, the version in line 2 fails because setHeaderValue returns null. Wouldn't it be useful if it returned "this"?
As for your second Q, no, i've no immediate ideas. Mind you, I have no idea what columns you are adding or why ...

Yes you're right :).
... I use this for applications like: populating from a database and inserting values for some 'Statistics and probabilistic' calculations. ->Every event has a probability associated with it (that's two rows table with 'n' possible events). So, after setting that values I should make a graphical representation of it.... stuff like that.

Thanks for help! Thread solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.