是否可以在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;
            }
        };


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

最佳答案

您混淆了自己的责任范围。 TableModel仅假定携带数据。由视图决定如何呈现该数据。

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

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

因此,使用下面的两个图片...



我能够生产...



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

关于java - Java在表单元格中添加JLayeredPane?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19019078/

10-10 04:10