如何从JTable获取Icon

时间:2011-08-13 09:03:26

标签: java swing jtable imageicon

我已使用以下代码更改JTable中的单元格渲染以显示图片而非文字:

base_table.getColumnModel().getColumn(3).setCellRenderer(new TableCellRenderer() {

    @Override
    public Component getTableCellRendererComponent(JTable jtable, Object value,
            boolean bln, boolean bln1, int i, int i1) {
        JLabel lbl = new JLabel();
        lbl.setIcon((ImageIcon) value);
        return lbl;
    }
});

现在,我希望能够获取JTable中每行的图像,以便将其保存在数据库中。我怎么能这样做?

4 个答案:

答案 0 :(得分:4)

我无法抗拒那个

的榜样

enter image description here enter image description here

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

答案 1 :(得分:3)

JTable中存储的数据可以在TableModel中找到。但是因为它是你的代码,通常是构建这个TableModel(通常是从列表或数组中),你应该能够从这个列表或数组中获取图标。否则,只需使用table.getModel().getValueAt(row, column),然后将其投放到ImageIcon

答案 2 :(得分:2)

您应该已经拥有表模型中的所有图像。所以你只需要从模型中获取图像,然后将它们保存在数据库中。

在您的单元格渲染器中,您有Object value类型,然后使用(ImageIcon) value将其转换为ImageIcon中的lbl.setIcon((ImageIcon) value);

当您从模型中获取数据时,您可以执行相同的操作:

ImageIcon myIcon = 
         (ImageIcon) base_table.getModel().getValueAt(rowIndex, 3);

其中3是带有图像的列的columnIndex,而rowIndex是您想要的行。

答案 3 :(得分:0)

  下面的

是正确的图像渲染器类。

class SimpleCellRenderer extends DefaultTableCellRenderer{



    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 

    {
         Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
               column);
      ((JLabel)cell).setIcon((Icon)value);
      ((JLabel)cell).setText("");
      ((JLabel)cell).setHorizontalAlignment(JLabel.CENTER);

      if (isSelected) {
         cell.setBackground(Color.white);
      } else {
         cell.setBackground(null);
      }
      //((AbstractTableModel)table.getModel()).fireTableCellUpdated(row,column);

      return cell;
   }


    }
  

下面是自动填充所有内容的方法。      private void formWindowOpened(java.awt.event.WindowEvent evt)

{                                  
        // TODO add your handling code here:

        fillIcon();  
    } 

   public void fillIcon() {
        int i,j,rowValue,colValue;
         int cols= student.getColumnCount();
         int rows=student.getRowCount();

         for(i =0 ;i<rows ;i++)
         {
             for(j=3; j<cols;j++)
             {
                 rowValue = i;
                 colValue = j;
                 String value = (String)student.getValueAt(rowValue, colValue);

                 if(value.equals("h"))//here h is the value stored in your database which is used to set some icon in place of value h.
{

            ImageIcon icon = new ImageIcon(getClass().getResource("dash.png"));
            student.setValueAt(icon, rowValue, colValue);
            student.getColumnModel().getColumn(colValue).setCellRenderer(new SimpleCellRenderer());
        }