Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为jtable fixed column设置映像,当我运行时,它';s仅获取图像路径_Java_Swing_Jtable_Imageicon - Fatal编程技术网

Java 如何为jtable fixed column设置映像,当我运行时,它';s仅获取图像路径

Java 如何为jtable fixed column设置映像,当我运行时,它';s仅获取图像路径,java,swing,jtable,imageicon,Java,Swing,Jtable,Imageicon,我创建了一个程序,在jtable固定列中设置一个imageIcon,我创建了一个jtable并获取一个数据库记录,然后将第一列设置为固定列。我在固定列中设置了一个图像图标。当我编译这个程序时,我只得到imageicon的路径,而没有得到图像。我修复了项目包文件夹中的图像图标 This is the code i used public void Frm_FlxD_Database() { try{ TmpRow=0;

我创建了一个程序,在jtable固定列中设置一个imageIcon,我创建了一个jtable并获取一个数据库记录,然后将第一列设置为固定列。我在固定列中设置了一个图像图标。当我编译这个程序时,我只得到imageicon的路径,而没有得到图像。我修复了项目包文件夹中的图像图标

    This is the code i used

public void Frm_FlxD_Database() {
           try{
                TmpRow=0;
                TmpMainPrj.PRJ_DB_CONNECTION_ASSGN();
                TmpFlxMdl =(DefaultTableModel)FlxD.getModel();
                TmpFlxDRow = 0;

                TmpFlxSt=TmpGPrjVarDec.GContn.createStatement();
                TmpFlxDRs=TmpFlxSt.executeQuery("SELECT * from activitymaster");
                PRJ_FLX_DEFTL_ASSGN(FlxD, "BEGIN");
                TmpFlxDRs.first();
                do {
                    FlxD.setValueAt(TmpFlxDRs.getString("ACTVTYDESC"), TmpRow,1);
                    FlxD.setValueAt(TmpFlxDRs.getString("ACTVTYCODE"), TmpRow,2);
                    FlxD.setValueAt(TmpFlxDRs.getString("DISPSTATUS"), TmpRow,3);
                    FlxD.setValueAt(TmpFlxDRs.getString("ACTVTYID"), TmpRow,4);
                    TmpFlxMdl.addRow(new Object[]{""});
                    TmpRow = TmpRow + 1;
                }while(TmpFlxDRs.next());

                FRM_FLXD_PTR_DATA_ASSGN(TmpFlxDRow);
           }
           catch(Exception e){
                System.out.println(e);
           }
    }
private void FRM_FLXD_PTR_DATA_ASSGN(int PFlxRow) {
           TmpFlxDRow = PRJ_FLX_PTR_ASSGN(FlxD, PFlxRow, TmpFlxDRow);
   }

    private int PRJ_FLX_PTR_ASSGN(JTable PFlx, int PCurRow, int PPrvRow) {
            ImageIcon TmpIcon;

            System.out.println(PCurRow);
            System.out.println(PPrvRow);

            if (PCurRow != PPrvRow){
                TmpIcon = new ImageIcon(getClass().getResource("Blank.gif"));
                PFlx.setValueAt(TmpIcon,PPrvRow,0);
                System.out.println(TmpIcon);
            }
            TmpIcon = new ImageIcon(getClass().getResource("Pointer.gif"));
            PFlx.setValueAt(TmpIcon,PCurRow,0);
            System.out.println(TmpIcon);               
            return(PCurRow);
    }

不应在数据模型中添加图标。您应该添加数据(布尔指示符、字符串等),并为此列使用渲染器,该渲染器将根据该列的数据显示相应的图标

有关单元渲染器的信息和示例,请参见


请学习Java命名约定并遵守它们。你的代码不可读。请参见

不应在数据模型中添加图标。您应该添加数据(布尔指示符、字符串等),并为此列使用渲染器,该渲染器将根据该列的数据显示相应的图标

有关单元渲染器的信息和示例,请参见


请学习Java命名约定并遵守它们。你的代码不可读。请参阅

代码中没有太多内容,我猜这与tablemodel getColumnClass()方法有关。有很多教程介绍如何解决这个问题。目前,它可能由对象的表defaultrenderer呈现


好消息是,你不必混淆你的代码,它已经很难阅读,甚至更难理解。您可能需要阅读一些java代码指南来改进代码。

在不深入代码的情况下,我猜这与tablemodel getColumnClass()方法有关。有很多教程介绍如何解决这个问题。目前,它可能由对象的表defaultrenderer呈现

好消息是,你不必混淆你的代码,它已经很难阅读,甚至更难理解。您可能需要阅读一些java代码指南来改进代码。

知道,这是一个简单的示例

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);
        table = new JTable(model) {

            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.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);
        EventQueue.invokeLater(new Runnable() {

            public void run() {
            }
        });
        new Thread(this).start();
    }

    public void run() {
        while (runProcess) {
            try {
                Thread.sleep(750);
            } 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);
    }
}

知道,简单的例子

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);
        table = new JTable(model) {

            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.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);
        EventQueue.invokeLater(new Runnable() {

            public void run() {
            }
        });
        new Thread(this).start();
    }

    public void run() {
        while (runProcess) {
            try {
                Thread.sleep(750);
            } 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);
    }
}


我正在使用netbeans在jframe中工作,如何设置getColumnClass。请解释一下好吗?@javasi很抱歉,我从来没有使用过名为Java桌面应用程序的Swing框架,但必须存在一个代码编辑器,用于编辑和从数据库获取数据到Swing,请搜索ResultSetTableModel或TableFromDatabase我正在使用netbeans的jframe中工作,如何设置getColumnClass。请您解释一下好吗?@javasi很抱歉,我从未使用过名为Java桌面应用程序的Swing框架,但必须存在一个代码编辑器,用于将数据从数据库提取到Swing,请搜索ResultSetTableModel或TableFromDatabase