如何从JTable中获取图标

发布于 2024-11-29 07:04:13 字数 499 浏览 1 评论 0原文

我已使用以下代码更改了 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 中每一行的图像code> 以便将其保存在数据库中。我怎么能这么做呢?

I have changed the cell render in JTable to show image instead of text using the following code:

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

Now, I'd like to be able to get the image for each row in the JTable in order to save it in database. How could I do that?

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

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

发布评论

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

评论(4

叹倦 2024-12-06 07:04:13

我无法抗拒这个

在此处输入图像描述 在此处输入图像描述

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

I can't resist just example for that

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);
    }
}
莫多说 2024-12-06 07:04:13

存储在 JTable 中的数据可以在其 TableModel 中找到。但由于通常是您的代码构建此 TableModel(通常从列表或数组),因此您应该能够从此列表或数组中获取图标。否则,只需使用table.getModel().getValueAt(row, column),并将其转换为ImageIcon

The data stored in a JTable can be found in its TableModel. But since it's your code, normally, that builds this TableModel (from a list or an array, typically), you should be able to get the icon from this list or array. Else, just use table.getModel().getValueAt(row, column), and cast it to an ImageIcon.

撕心裂肺的伤痛 2024-12-06 07:04:13

您的表格模型中应该已经包含了所有图像。因此,您只需从模型中获取图像,然后将它们保存在数据库中。

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

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

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

其中 3 是包含图像的列的 columnIndex,rowIndex 是排你 想。

You should already have all the images in your table model. So you just have to get the images from the model, and then save them in your database.

In your cell renderer you have the type Object value, then you use (ImageIcon) value to cast it to an ImageIcon in lbl.setIcon((ImageIcon) value);

You can do the exaclty the same when you get the data from your model:

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

where 3 is your columnIndex for the column with images, and rowIndex is the row you want.

梅窗月明清似水 2024-12-06 07:04:13

下面是正确的图像渲染器类。

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


    }

下面是自动填充所有内容的方法。
私有无效 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());
        }

below is the correct image renderer class.

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


    }

below is the method from where everything gets filled automtically.
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());
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文