This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have gone through several web sites and tried several solutions but none of them seem to work. I have a Jtable with one column and I want to put a title at the top of the column. One would think this would be the most simple thing in the world as compared with filling the Jtable with rows and then reading the text of the row when it is clicked but this has not been the case at all.
Part of my problem is that at this point I have been confused by TableModels, ColumnModels, and HeaderModels.
They all seem to do the same thing but I do not know which one to use. I have tried several and none have worked but this is most likely because of how the Jtable data has been refreshed.
I am using Netbeans to create a Swing application.
Can someone point me in the right direction to guide me out of my maze of confusion?
I have been confused by TableModels, ColumnModels, and HeaderModels.
Given that ColumnModels and HeaderModels don't exist I can understand your confusion. Don't make up class names when you have a question if you want us to understand what your are talking about.
I suggest you use a DefaultTableModel to keep it simple to start with. Since I guess you want an empty table to start with, try one of the constructos that accepts "column names" and "row count" as the parameters. So the basic code would be:
DefaultTableModel model = new DefaultTableModel(...);
JTable table = new JTable( model );
JScrollPane scrollPane = new JScrollPane( table );
frame.add( scrollPane );
If you need more help then post your SSCCE that shows what you have tried.
This is a global variable since it is referenced by several modules at various times:
The JTable in question is called tblSections. For some crazy reason it was created with four columns and I cannot change that. When the program is initialized if the table is empty I delete the three columns I do not need and here I attempted to specify a column title. This did not work.
Later in another module I tried some different approaches, these did not work either:
The code where I repaint, update, setVisible are my attempts to update the GUI.
This is confusing for me because I have a TABLE and that is really all that I care about but in order to do anything with the table I need to create a table model, column model, header model, and now some sort of scroll pane. I don't understand why this has to be so complicated. I am sure Java is configured to be a very powerful language that can do wonderful things but it is difficult for a newbie like me.
I see now that in these examples in two places I used "FirstColumn.setHeaderValue". That shows how confused I am. I have tried four different things now. I will do some research on that scrollpane thing as you suggested.
Why are you making it so complicated? Where in my answer did I mention the TableColumnModel or JTableHeader?
The JTable is not dynamically created in the code, it is defined in the GUI.
Don't use the IDE to generate the code. You are learning the IDE and NOT learning Java.
I will see about posting some code.
I didn't ask you to post "some" code. I asked for an SSCCE. The total program will be 5-15 lines (I would guess) to create and display a JTable in a JFrame. I already gave you 4 of them so there is not a lot more to do. I can't help without a SSCCE. Here is a basic structure for a simple SSCCE:
I am making this so complicated because I am looking at several web sites and trying different approaches but none of them are working. My project is 99.99% complete and it works great, I just have to add a title to one column in one JTable.
Per your suggestion I have started over again with a small test application. Once I figure out how to create a JTable and add a column header I can then put those steps into my project and I will be done with this.
I am attempting to dynamically create a JTable per your code. This does not seem to do anything at all when I click the button:
! am making this so complicated because I am looking at several web sites and trying different approaches but none of them are working.
That is why you need to forget the other websites and follow the simple advice given here.
Per your suggestion I have started over again with a small test application.
I suggested you post your SSCCE (and I even gave you 95% of the SSCCE code) showing what you are doing. You haven't posted a SSCCE. 4 lines of code that don't compile are NOT a SSCCE! Read the link again!
I am attempting to dynamically create a JTable per your code.
I suggested you learn to walk before you run. That is you need to learn how to create a static table with a single column that has a heading first before you can attempt to create a dynamic table.
This does not seem to do anything at all when I click the button:
Even if you follow Rob's advice about revalidate() and repaint() it still won't do anything, because you still haven't listened to my suggestion about how to use the DefaultTableModel which I have already repeated twice and am not going to repeat a third time.