Java在表格单元格中添加JLayeredPane?

时间:2013-09-26 03:55:33

标签: java swing

是否可以在JLayeredPane()内添加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;
            }
        }; 

任何想法如何做到这一点?

1 个答案:

答案 0 :(得分:2)

你混淆了你的责任范围。 TableModel仅用于携带数据。由视图来决定如何呈现数据。

如果您需要提供某种自定义呈现,则需要提供TableCellRenderer

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

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

enter image description here enter image description here

我能够制作......

enter image description here

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;
        }
    }
}

有关详细信息,请参阅Using Custom Renderers