14

I am using a JScrollPane to wrap a JTable. Depending on the configuration, there is some space that is not occupied by the table. It is drawn gray (it looks like it is transparent and you can just see the component in the back). How can I set this area to be a certain color?

Here is a SSCCE to illustrate.

import java.awt.Color;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class DialogDemo extends JDialog {
    public static void main(final String[] args) {
        final DialogDemo diag = new DialogDemo();
        diag.setVisible(true);
    }

    public DialogDemo() {
        super();
        setTitle("SSCCE");

        final Vector<Vector<String>> rowData = new Vector<Vector<String>>();
        final Vector<String> columnNames = new VectorBuilder<String>().addCont("Property").addCont("Value");
        rowData.addElement(new VectorBuilder<String>().addCont("lorem").addCont("ipsum"));
        rowData.addElement(new VectorBuilder<String>().addCont("dolor").addCont("sit amet"));
        rowData.addElement(new VectorBuilder<String>().addCont("consectetur").addCont("adipiscing elit."));
        rowData.addElement(new VectorBuilder<String>().addCont("Praesent").addCont("posuere..."));

        final JTable table = new JTable(rowData, columnNames);
        JScrollPane pane = new JScrollPane(table);

        // ************* make that stuff white! *******************
        table.setBackground(Color.white);
        table.setOpaque(true);
        pane.setBackground(Color.white);
        pane.setOpaque(true);
        // ************* make that stuff white! *******************

        add(pane);
        pack();

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }

    class VectorBuilder<T> extends Vector<T> {
        public VectorBuilder<T> addCont(final T elem) {
            addElement(elem);
            return this;
        }
    }
}

And here you can see the area, which I want to "colorize". In the SSCCE, I try to do that by using setOpaque(boolean) and setBackgroundColor(Color) of the table and scroll pane, with no success.

enter image description here

Can you tell me, what I am doing wrong?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
brimborium
  • 8,880
  • 9
  • 46
  • 73

2 Answers2

27

Instead of this:

table.setBackground(Color.white);
table.setOpaque(true);
pane.setBackground(Color.white);
pane.setOpaque(true);

call:

pane.getViewport().setBackground(Color.WHITE);
Branislav Lazic
  • 13,007
  • 8
  • 52
  • 77
  • 1
    JScrollPane can't be opaque – mKorbel Aug 21 '13 at 16:19
  • @mKorbel Interesty...Then why does that method even exists? – Branislav Lazic Aug 21 '13 at 16:22
  • [see here](http://stackoverflow.com/a/18275877/714968) for JTable, this insteresting question were asked her a few times, I don't know answer – mKorbel Aug 21 '13 at 16:23
  • Works perfectly. Thanks a lot for the answer. @mKorbel Yeah, I figured that those `setOpaque()` methods did not do anything. Which was additionally confusing. ;) – brimborium Aug 22 '13 at 07:45
  • @mKorbel .. huch? Every component can be opaque (or not) – kleopatra Aug 22 '13 at 08:02
  • @kleopatra as far as i understan from OPs picture and description ---> `Depending on the configuration, there is some space that is not occupied by the table. It is drawn gray (it looks like it is transparent and you can just see the component in the back). How can I set this area to be a certain color?`, or am I wrong – mKorbel Aug 22 '13 at 08:06
  • @kleopatra and then there could be another logical requirement and nice issue with to set opacity for JTableHeader too and propertly – mKorbel Aug 22 '13 at 08:08
  • @mKorbel the only error of the OP was to color the wrong component (scrollPane vs. viewport), opacity has nothing to do with it: both are opaque by default. And the header is ... elsewhere ;-) – kleopatra Aug 22 '13 at 08:12
  • @kleopatra aaach now I see where your comment goes, sure you can to convert that to the answer, code be reference, important point for another readers here – mKorbel Aug 22 '13 at 08:18
  • @mKorbel no need for yet another answer, brano's already did it :-) – kleopatra Aug 22 '13 at 08:25
-2

Don't use Opaque, try this:

pane.getViewport().setBackground(Color.WHITE);
Adeel
  • 2,657
  • 7
  • 21
  • 31