This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Jtable Column Heading Text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jtable Column Heading Text" Watch "Jtable Column Heading Text" New topic
Author

Jtable Column Heading Text

Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

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?


Phil
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2204
    
    7
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.
Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

Thank you for your reply but I cannot see in your code where I would specify the column title.

I have been working with the following:

JTableHeader
TableColumnModel
DefaultTableModel

yes, these things do exist, I have gotten code from several web sites but none of them have allowed me to put a title on the column.

I will see about posting some code. The JTable is not dynamically created in the code, it is defined in the GUI.
Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

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.
Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

I added the following code as a beginning per your suggestion:



However, I am getting the following error:

frame has private access in org.jdesktop.application.FrameView

Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

I have added the following code but since I cannot add the scrollpane to the frame the JTable now appears to be empty

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2204
    
    7
I cannot see in your code where I would specify the column title.


I suggested you use the appropriate constructor from the DefaultTableModel.

I have been working with the following:


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:

Philip Persson
Greenhorn

Joined: Jan 30, 2011
Posts: 20

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:



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

Whenever you modify the GUI hierarchy when it's already been made active you need to revalidate() and repaint() the GUI, or at least the parent component of component you just added / removed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2204
    
    7
! 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.
 
Don't get me started about those stupid light bulbs.
 
subject: Jtable Column Heading Text