File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTable inside JScrollPane - background coloring problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable inside JScrollPane - background coloring problem" Watch "JTable inside JScrollPane - background coloring problem" New topic
Author

JTable inside JScrollPane - background coloring problem

Dennis McNulty
Greenhorn

Joined: Apr 28, 2011
Posts: 3
I have a situation with a table that's narrower and shorter than the enclosing scroll pane's viewport, and column auto-resizing is turned off, so there's empty space below and to the right of the table. I'd like to fill the empty space with a single background color. I can fill in the bottom using scrollPane.setBackground(), and I can set most of the right side using scrollPane.getViewport().setBackground(). But I'm still left with an uncolored strip to the right of the table's column headers. I've tried using setBackground on the scroll pane's enclosing panel, and also tried using scrollPane.setColumnHeaderView() to force a new colored JPanel, but neither method has worked.

Is there any way to fill in that strip right of the table's column headers?

Below is the code I'm using, a slight variation of TableDemo.java from Oracle's Swing Example set.

Thanks in advance for any help. - Dennis


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2226
    
    8
165 lines of code is way too much code to post. What do the custom TableModel and all the debug code have to do with your problem? We don't have time to read the the code to determine what is and isn't relevant to the stated problem. That is your job. Please post a SSCCE the next time.

Here is an example of a SSCCE which happens to do what you want:

Dennis McNulty
Greenhorn

Joined: Apr 28, 2011
Posts: 3
I didn't know that Oracle demo programs didn't qualify as SSCCEs. I was hoping to avoid creating a lot of source code from scratch. Thank you for the example SSCCE. I'll give it a try in a little while. Is there any kind of repository of SSCCEs for new forum users like me?

Thanks - Dennis
Dennis McNulty
Greenhorn

Joined: Apr 28, 2011
Posts: 3
Rob -

(Problem resolved)

Your SSCEE had the answer to my problem. Setting the background in the table header filled in the empty space. I had thought that a JTableHeader object was merely a reorganization of data in the JTableColumnModel, and didn't have any displayable items of its own. In my large app, I am using custom table header renderers, declared as parts of TableColumns, and I had never needed to deal with table headers.

Your SCCEE also held another surprise for me. I had thought that it wasn't allowed to have a null JTableModel. About 100 lines of code in my first posting were devoted solely to building the table model.

Again, though, I'd like to know if there is some place for greenhorns like me to find existing SSCEEs. If I had known about this TableDemo2 one, I wouldn't have needed to bother anyone at CodeRanch about my problem.

Thanks - Dennis
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2226
    
    8
Again, though, I'd like to know if there is some place for greenhorns like me to find existing SSCEEs


You miss the point about a SSCCE. The idea is to start from scratch and just add in the code/components relevant to the problem. That way you isolate the problem easier. So there is only one "existing SSCCE" that you need. Its just a shell program that does nothing:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable inside JScrollPane - background coloring problem