如何从JTable获取图标

如何从JTable获取图标,第1张

如何从JTable获取图标

我无法抗拒这样的例子

import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.EventQueue;import javax.swing.*;import javax.swing.table.*;public class TableIcon extends Jframe implements Runnable {    private static final long serialVersionUID = 1L;    private JTable table;    private JLabel myLabel = new JLabel("waiting");    private int pHeight = 40;    private boolean runProcess = true;    private int count = 0;    public TableIcon() {        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 = {{errorIcon, "about"}, {infoIcon, "Add"}, {warnIcon, "Copy"},};        DefaultTableModel model = new DefaultTableModel(data, columnNames) { private static final long serialVersionUID = 1L; //  Returning the Class of each column will allow different //  renderers to be used based on Class @Override public Class getColumnClass(int column) {     return getValueAt(0, column).getClass(); }        };        table = new JTable(model);        table.setRowHeight(pHeight);        table.setPreferredScrollableViewportSize(table.getPreferredSize());        JScrollPane scrollPane = new JScrollPane(table);        add(scrollPane, BorderLayout.CENTER);        myLabel.setPreferredSize(new Dimension(200, pHeight));        myLabel.setHorizontalAlignment(SwingConstants.CENTER);        add(myLabel, BorderLayout.SOUTH);        new Thread(this).start();    }    public void run() {        while (runProcess) { try {     Thread.sleep(1250); } catch (Exception e) {     e.printStackTrace(); } SwingUtilities.invokeLater(new Runnable() {     @Override     public void run() {         ImageIcon myIcon = (ImageIcon) table.getModel().getValueAt(count, 0);         String lbl = "JTable Row at :  " + count;         myLabel.setIcon(myIcon);         myLabel.setText(lbl);         count++;         if (count > 2) {  count = 0;         }     } });        }    }    public static void main(String[] args) {        TableIcon frame = new TableIcon();        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);        frame.setLocation(150, 150);        frame.pack();        frame.setVisible(true);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4921893.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存