Java 在表格单元格中添加 JLayeredPane? [英] Java add JLayeredPane in a table cell?

查看:26
本文介绍了Java 在表格单元格中添加 JLayeredPane?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 JTable 中添加 JLayeredPane() ?

Is it possible to add a JLayeredPane() inside a JTable?

我试过了:

     JLayeredPane agentPicturePane = new JLayeredPane();
     String agentPicture = "http://www.domain.com/uploads/thumb_23.jpg";

     try {
         final BufferedImage image = resize(new URL(agentPicture), new Dimension(60, 60));
         jLabel1.setIcon(new javax.swing.ImageIcon(image));
     } catch (Exception ex) {

     }

     jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/imageBg.png")));

     jLabel1.setBounds(13, 13, 60, 60);
     jLabel6.setBounds(0, 0, 86, 86);
     agentPicturePane.setPreferredSize(new Dimension(86, 86));
     agentPicturePane.add(jLabel6, new Integer(50));
     agentPicturePane.add(jLabel1, new Integer(100));

     visitorModel.addRow(new Object[] {
         agentPicturePane, visitorName, visitorClose
     });

使用模型:

        visitorModel = new DefaultTableModel(productss, colNamee) { 
            @Override
            public boolean isCellEditable(int row, int column) {
               //all cells false
               return false;
            }     
            public Class getColumnClass(int column) {
            //  return getValueAt(0, column).getClass();
                return (column == 0 || column == 2) ? Icon.class : Object.class;
            }
        }; 

知道如何做到这一点吗?

Any ideas how this could be done?

推荐答案

你混淆了你的职责范围.TableModel 只是假设携带数据.由视图决定如何呈现数据.

You confusing your areas of responsibility. The TableModel is only suppose to carry data. It's up to the view to decide how that data is to be rendered.

如果你需要提供某种自定义渲染,那么你需要提供你的TableCellRenderer

If you need to provide some kind of custom rendering, then you need to supply your TableCellRenderer

此示例使用复合 JLabelEmptyBorder 来提供叠加功能.

This example uses compound JLabels and a EmptyBorder to provide the overlay functionality.

所以使用下面的两张图片...

So using the two images below...

我能够生产...

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class LayeredCellRenderer {

    public static void main(String[] args) {
        new LayeredCellRenderer();
    }

    public LayeredCellRenderer() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }

                    BufferedImage pic = ImageIO.read(new File("Photo01.jpg"));

                    DefaultTableModel model = new DefaultTableModel(
                            new Object[][]{{pic, "Sophia", ""}},
                            new Object[]{"Picture", "Name", "Close"});

                    JTable table = new JTable(model);
                    table.setRowHeight(86);
                    table.getColumnModel().getColumn(0).setCellRenderer(new SecutiryBadgeTableCellRenderer());

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new JScrollPane(table));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public static class SecutiryBadgeTableCellRenderer extends DefaultTableCellRenderer {

        protected static BufferedImage background;
        private JLabel subImage;

        public SecutiryBadgeTableCellRenderer() {
            try {
                background = ImageIO.read(new File("Background.jpg"));
                setIcon(new ImageIcon(background));
                setLayout(new GridBagLayout());
                subImage = new JLabel();
                subImage.setHorizontalAlignment(JLabel.LEFT);
                subImage.setVerticalAlignment(JLabel.TOP);
                subImage.setBorder(new EmptyBorder(30, 10, 0, 0));
                add(subImage);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, null, isSelected, hasFocus, row, column);
            if (value instanceof Image) {
                Image img = (Image) value;
                subImage.setIcon(new ImageIcon((Image) value));
                subImage.setSize(subImage.getPreferredSize());
            } else {
                subImage.setIcon(null);
            }
            return this;
        }
    }
}

有关详细信息,请参阅使用自定义渲染器

这篇关于Java 在表格单元格中添加 JLayeredPane?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆