java 如何在JTable中添加WaterMark

发布于 2024-11-25 00:44:52 字数 1836 浏览 3 评论 0原文

我有一个 JTable,我试图在 JTable 后面插入一个图像作为水印

tblMainView= new JTable(dtModel){

        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) 
        {
        Component c = super.prepareRenderer( renderer, row, column);
        // We want renderer component to be transparent so background image is visible
        if( c instanceof JComponent )
        ((JComponent)c).setOpaque(true);
        return c;
        }
        ImageIcon image = new ImageIcon( "images/watermark.png" );

          public void paint( Graphics g )
        {
        // First draw the background image - tiled 
        Dimension d = getSize();
        for( int x = 0; x < d.width; x += image.getIconWidth() )
        for( int y = 0; y < d.height; y += image.getIconHeight() )
        g.drawImage( image.getImage(), x, y, null, null );
        // Now let the regular paint code do it's work
        super.paint(g);
        }


        public boolean isCellEditable(int rowIndex, int colIndex) {
          return false;
        }
        public Class getColumnClass(int col){
            if (col == 0)  
            {  
            return Icon.class;  
            }else if(col==7){
                return String.class;
            }
        else
            return String.class;  

        }   
        public boolean getScrollableTracksViewportWidth() {
            if (autoResizeMode != AUTO_RESIZE_OFF) {
                if (getParent() instanceof JViewport) {
                return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
                }
            } 
            return false;
            }

    };

以上是 c 我的 JTable 的颂歌,但水印不可见;让我补充一下,稍后我将此 JTable 放置在 JScrollPaneJSplitPane 中。

I have a JTable, and I am trying to insert an image behind the JTable as a Watermark

tblMainView= new JTable(dtModel){

        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) 
        {
        Component c = super.prepareRenderer( renderer, row, column);
        // We want renderer component to be transparent so background image is visible
        if( c instanceof JComponent )
        ((JComponent)c).setOpaque(true);
        return c;
        }
        ImageIcon image = new ImageIcon( "images/watermark.png" );

          public void paint( Graphics g )
        {
        // First draw the background image - tiled 
        Dimension d = getSize();
        for( int x = 0; x < d.width; x += image.getIconWidth() )
        for( int y = 0; y < d.height; y += image.getIconHeight() )
        g.drawImage( image.getImage(), x, y, null, null );
        // Now let the regular paint code do it's work
        super.paint(g);
        }


        public boolean isCellEditable(int rowIndex, int colIndex) {
          return false;
        }
        public Class getColumnClass(int col){
            if (col == 0)  
            {  
            return Icon.class;  
            }else if(col==7){
                return String.class;
            }
        else
            return String.class;  

        }   
        public boolean getScrollableTracksViewportWidth() {
            if (autoResizeMode != AUTO_RESIZE_OFF) {
                if (getParent() instanceof JViewport) {
                return (((JViewport)getParent()).getWidth() > getPreferredSize().width);
                }
            } 
            return false;
            }

    };

The above is the c
ode of my JTable, but the watermark in not visible; let me add that later I place this JTable in a JScrollPane and JSplitPane.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烂柯人 2024-12-02 00:44:52

两种可能的解决方案,但我不知道哪一种。 :DI认为第一种方法成功的机会最高。

第一种方法

重写 paintComponent(Graphics g) 方法:

public void paintComponent(Graphics g)
{
    //First super
    super.paintComponent(g);

    g.drawImage(0, 0, getWidth(), getHeight());
}

第二种方法

将 JTable 不透明设置为 false: table.setOpaque(false);
重写您的 paintComponent(Graphics g) 方法:

public void paintComponent(Graphics g)
{
    //First draw
    g.drawImage(0, 0, getWidth(), getHeight());

    super.paintComponent(g);
}

Two possible solutions, but I don't know which one. :D I think the first approach will have the highest chance of success.

First approach

Override your paintComponent(Graphics g) method:

public void paintComponent(Graphics g)
{
    //First super
    super.paintComponent(g);

    g.drawImage(0, 0, getWidth(), getHeight());
}

Second approach

Set your JTable opaque to false: table.setOpaque(false);
Override your paintComponent(Graphics g) method:

public void paintComponent(Graphics g)
{
    //First draw
    g.drawImage(0, 0, getWidth(), getHeight());

    super.paintComponent(g);
}
晌融 2024-12-02 00:44:52

有一些错误

确实不是个好主意,对于 Swing JComponents 使用 paint(Graphics g)paint() 用于 AWT 代码,对于 Swing 是paintComponent(Graphics g),在 Swing 中使用 paint(Graphics g) 会向 Swing GUI 显示意外的输出,

这确实不是一个好主意 paint(Graphics g)AWT 代码或paintComponent(Graphics g) 对于任何 Renderer 内的 Swing 代码,

您必须准备 JTable 的 BackGroung,如此处所示 TableWithGradientPaint

there are some mistakes

really not good idea use paint(Graphics g) for Swing JComponents, paint() is for AWT code, for Swing is there paintComponent(Graphics g), usage of paint(Graphics g) in Swing would have shows unexpected output to the Swing GUI,

really not good idea paint(Graphics g)for AWT code or paintComponent(Graphics g) for Swing code inside any of Renderer

you have to prepare JTable's BackGroung as is demonstrated here TableWithGradientPaint

清泪尽 2024-12-02 00:44:52

在绘制水印之前尝试调用 super.paint(g) ,JTable 可能正在您的图像上绘制。

Try calling super.paint(g) before you paint your watermark, the JTable is probably painting over your image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文