JTable 单元格中的图像显示

发布于 2025-01-03 18:48:11 字数 1764 浏览 4 评论 0原文

我使用以下程序使用 java 类创建 JTable。如果我从选项窗格中获取 warnIcon,infoIcon 的图像,它会正确显示。但是,如果我从系统添加图像,它不会显示在表中。显示空白区域而不是我的图像。如何从该表中的文件(例如 A.jpg)中绘制图像?

package pointer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.plaf.OptionPaneUI;
import javax.swing.table.*;
import sun.swing.ImageIconUIResource;

public class TableIcon1 extends JFrame  {
    private JTable table;
    private int pHeight = 60;
       public TableIcon1() {
           ImageIcon testIcon = new ImageIcon("A.jpg");
          // ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
           ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
           ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");
           String[] columnNames = {"Picture", "Description"};
           Object[][] data = {{testIcon  , "About"}, {infoIcon, "Add"}, {warnIcon, "Copy"},};
           DefaultTableModel model = new DefaultTableModel(data, columnNames);
           table = new JTable(model) {
            @Override
            public Class getColumnClass(int column) {
                return getValueAt(2, column).getClass();
            }
          };
           table.setRowHeight(pHeight);
           table.setPreferredScrollableViewportSize(table.getPreferredSize());
           JScrollPane scrollPane = new JScrollPane(table);
           add(scrollPane, BorderLayout.CENTER);
         }

    public static void main(String[] args) {
        TableIcon1 frame = new TableIcon1();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.pack();
        frame.setVisible(true);
    }
}

I use the following program for creating JTable using java class. If I get the images for warnIcon,infoIcon from optionpane it displays properly. However, if I add image from my system it doesn't display in table. A blank space is displayed instead of my image. How can I draw an image from file (e.g. A.jpg) in that table?

package pointer;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.plaf.OptionPaneUI;
import javax.swing.table.*;
import sun.swing.ImageIconUIResource;

public class TableIcon1 extends JFrame  {
    private JTable table;
    private int pHeight = 60;
       public TableIcon1() {
           ImageIcon testIcon = new ImageIcon("A.jpg");
          // ImageIcon errorIcon = (ImageIcon) UIManager.getIcon("OptionPane.errorIcon");
           ImageIcon infoIcon = (ImageIcon) UIManager.getIcon("OptionPane.informationIcon");
           ImageIcon warnIcon = (ImageIcon) UIManager.getIcon("OptionPane.warningIcon");
           String[] columnNames = {"Picture", "Description"};
           Object[][] data = {{testIcon  , "About"}, {infoIcon, "Add"}, {warnIcon, "Copy"},};
           DefaultTableModel model = new DefaultTableModel(data, columnNames);
           table = new JTable(model) {
            @Override
            public Class getColumnClass(int column) {
                return getValueAt(2, column).getClass();
            }
          };
           table.setRowHeight(pHeight);
           table.setPreferredScrollableViewportSize(table.getPreferredSize());
           JScrollPane scrollPane = new JScrollPane(table);
           add(scrollPane, BorderLayout.CENTER);
         }

    public static void main(String[] args) {
        TableIcon1 frame = new TableIcon1();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setLocation(150, 150);
        frame.pack();
        frame.setVisible(true);
    }
}

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

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

发布评论

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

评论(1

清音悠歌 2025-01-10 18:48:11
ImageIcon testIcon = new ImageIcon("A.jpg"); is road to nowhere

图标很常见,永远不会因错误路径或空值而返回任何异常,您必须进行测试,

最好的方法是在您的 Java 项目中创建一个名为 icons 的新文件夹。 > 然后复制您的 A.jpg Icon

然后您只能调用

URL url = ClassLoader.getSystemClassLoader().getResource("icons/A.jpg");
ImageIcon testIcon = new ImageIcon(url);
ImageIcon testIcon = new ImageIcon("A.jpg"); is road to nowhere

Icon is common that never returns any exceptions for wrong path or null value, you have to test fro that

best of way would be create a new folder with name icons in your Java project and there copy your A.jpg Icon

then you can only call

URL url = ClassLoader.getSystemClassLoader().getResource("icons/A.jpg");
ImageIcon testIcon = new ImageIcon(url);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文