创建一个透明的JTable

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建一个透明的JTable相关的知识,希望对你有一定的参考价值。

我想创建一个带有自定义单元格渲染的透明JTable。基本上我创建了一个不透明的JPanel(红色)和一个不透明的Jscroller和JTable。所以我期望看到的是接近红色的东西,但我看到桌子的细胞是白色的。你能解释一下为什么吗?这是我的例子:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Example extends JPanel {

    private static final long serialVersionUID = 1L;

    int boardHeight = 200;
    int boardWidth = 1;

    JTable table;
    Random random = new Random();

    public Example() {
        // setOpaque(true);
        setBackground(Color.RED);
        setLayout(new BorderLayout()); // !!
        DefaultTableModel model = new DefaultTableModel(boardHeight,
                boardWidth) {
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                return String.class;
            }
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        // !! table = new JTable(this.boardHeight, this.boardWidth);
        table = new JTable(model);
        for (int row = 0; row < model.getRowCount(); row++) {
            for (int col = 0; col < model.getColumnCount(); col++) {
                String s = random.nextBoolean() ? "1" : "2";
                model.setValueAt(s, row, col);
            }
        }
        table.setDefaultRenderer(String.class,
                new BoardTableCellRendererExample());

        table.setFocusable(true);
        table.setShowGrid(false);
        table.setRowMargin(0);
        table.setIntercellSpacing(new Dimension(0, 0));
        table.setRowSelectionAllowed(true);
        table.setVisible(true);
        table.getTableHeader().setUI(null);
        JScrollPane scroll = new JScrollPane(table);
        table.setOpaque(false);
        scroll.setOpaque(false);
        scroll.setBackground(new Color(0, 0, 0, 0F));
        table.setBackground(new Color(0, 0, 0, 0F));
        this.add(scroll, BorderLayout.CENTER);

        this.setPreferredSize(new Dimension(table.getPreferredSize().width,
                (table.getPreferredSize().height + 85)));

    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Example");
        frame.getContentPane().add(new Example());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setResizable(false);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }

}

class BoardTableCellRendererExample extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int col) {

        Component c = new CellComponentExample(table, value, isSelected,
                hasFocus, row, col);

        return c;
    }
}

class CellComponentExample extends JComponent {

    boolean isSelected;
    boolean hasFocus;
    int row;
    int col;
    public CellComponentExample() {
        super();
        setOpaque(false);
    }

    public CellComponentExample(JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int col) {
        this.isSelected = isSelected;
        this.hasFocus = hasFocus;
        this.row = row;
        this.col = col;

    }
    private static final long serialVersionUID = 1L;

    @Override
    protected void paintComponent(Graphics g) {

        g.setColor(new Color(0, 0, 0, 0F));

        g.fillRect(0, 0, 100, 50);
        g.setColor(Color.BLACK);

        g.drawRect(0, 0, 100, 50);
    }

}
答案

好的,我发现问题基本上你必须在ViewPort中将opaque设置为false:

scroll.getViewport().setOpaque(false);

我希望这可以帮助别人......

以上是关于创建一个透明的JTable的主要内容,如果未能解决你的问题,请参考以下文章

根据俄罗斯方块游戏创建的数组为 JTable 单元着色

无法使自定义 DialogFragment 在 Fragment 上透明

无法在 jquery 和 jtable 中使用 datepicker 更新和创建

Jtable 作为 Jtree 节点

在android活动中设置片段的形状和透明度

Swing Jtable 网格线设置