简体   繁体   中英

Resize ImageIcon in JTable dynamically with column width

当用户更改image列的列宽时,如何让JTable自动调整绑定ImageIcons的大小?

"How can I have JTable automatically resize bound ImageIcons when a user changes the column width of the image column?"

Here is a helper class that I use to stretch images It's just an extends JPanel that draws the Image. You need to pass an Image to it

Basically what it does is draw to image to width and height of the panel. So when you use it later in your renderer, it will resize automatically when the display area is stretched

import java.awt.*;
import javax.swing.*;

public class ImageViewer extends JPanel {

    private java.awt.Image image;
    private int xCoordinate;
    private boolean stretched = true;
    private int yCoordinate;

    public ImageViewer() {
    }

    public ImageViewer(Image image) {
        this.image = image;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (image != null) {
            if (isStretched()) {
                g.drawImage(image, xCoordinate, yCoordinate,
                        getSize().width, getSize().height, this);
            } else {
                g.drawImage(image, xCoordinate, yCoordinate, this);
            }
        }
    }

    public java.awt.Image getImage() {
        return image;
    }

    public void setImage(java.awt.Image image) {
        this.image = image;
        repaint();
    }

    public boolean isStretched() {
        return stretched;
    }

    public void setStretched(boolean stretched) {
        this.stretched = stretched;
        repaint();
    }

    public int getXCoordinate() {
        return xCoordinate;
    }

    public void setXCoordinate(int xCoordinate) {
        this.xCoordinate = xCoordinate;
        repaint();
    }

    public int getYCoordinate() {
        return yCoordinate;
    }

    public void setYCoordinate(int yCoordinate) {
        this.yCoordinate = yCoordinate;
        repaint();
    }
}

Also I used this custom cell renderer in which I use the ImageViewer class

import bookclasses.ImageViewer;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class MyImageCellRenderer extends DefaultTableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean isFocused, int row, int column) {
        Image image = ((ImageIcon) value).getImage();
        ImageViewer imageViewer = new ImageViewer(image);
        return imageViewer;
    }
}

And here's a sample program where I use the renderer class

import java.awt.BorderLayout;
import java.util.GregorianCalendar;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class TestTableCellRendererDemo extends JApplet {

    private String[] columnNames
            = {"Title", "Copies Needed", "Publisher", "Date Published",
                "In-stock", "Book Photo"};

    private ImageIcon intro1eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png");
    private ImageIcon intro2eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png");
    private ImageIcon intro3eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png");

    private Object[][] rowData = {
        {"Introduction to Java Programming", 120,
            "Que Education & Training",
            new GregorianCalendar(1998, 1 - 1, 6).getTime(),
            false, intro1eImageIcon},
        {"Introduction to Java Programming, 2E", 220,
            "Que Education & Training",
            new GregorianCalendar(1999, 1 - 1, 6).getTime(),
            false, intro2eImageIcon},
        {"Introduction to Java Programming, 3E", 220,
            "Prentice Hall",
            new GregorianCalendar(2000, 12 - 1, 0).getTime(),
            true, intro3eImageIcon},};

    private MyTableModel tableModel = new MyTableModel(
            rowData, columnNames);


    private JTable jTable1 = new JTable(tableModel);

    public TestTableCellRendererDemo() {
        jTable1.setDefaultRenderer(jTable1.getColumnClass(5), 
                new MyImageCellRenderer());
        jTable1.setRowHeight(60);
        add(new JScrollPane(jTable1), BorderLayout.CENTER);
    }
}

The result is a cell with an image, when if you expand the frame, the image will expand with it

在此处输入图片说明在此处输入图片说明


So, Basically

  1. Set my custom cell renderer to the column you want

     jTable1.setDefaultRenderer(jTable1.getColumnClass(5), new MyImageCellRenderer()); jTable1.setRowHeight(60); 
  2. Make sure the column in the table is an ImageIcon

     private ImageIcon intro1eImageIcon = new ImageIcon("icons/stacko/stackoverflow5.png"); Object[] row = { "Introduction to Java Programming", 120, "Que Education & Training", new GregorianCalendar(1998, 1 - 1, 6).getTime(), false, intro1eImageIcon }; 
  3. The renderer already calls the ImageViewer , so you don't need to explicitly call it yourself anywhere. Just make sure the file is in the same location as the renderer class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM