Java JScrollPane中的JTable,如何设置背景?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18362150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:53:19  来源:igfitidea点击:

JTable in JScrollPane, how to set background?

javaswingbackgroundjtablejviewport

提问by brimborium

I am using a JScrollPaneto 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?

我正在使用 aJScrollPane包装 a JTable。根据配置,有一些空间没有被表占用。它绘制为灰色(看起来它是透明的,您只能看到背面的组件)。如何将此区域设置为某种颜色?

Here is a SSCCE to illustrate.

这是一个 SSCCE 来说明。

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.

在这里你可以看到我想要“着色”的区域。在 SSCCE 中,我尝试通过使用表和滚动窗格的setOpaque(boolean)和来做到这一点setBackgroundColor(Color),但没有成功。

enter image description here

在此处输入图片说明

Can you tell me, what I am doing wrong?

你能告诉我,我做错了什么吗?

采纳答案by Branislav Lazic

Instead of this:

取而代之的是:

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

call:

称呼:

pane.getViewport().setBackground(Color.WHITE);

回答by Kuhakuvi

Don't use Opaque, try this:

不要使用不透明,试试这个:

pane.getViewport().setBackground(Color.WHITE);