how to set an image for jtable fixed column, when i am running, it's getting an image path only

好久不见. 提交于 2019-12-19 11:05:20

问题


I created a program to set an imageIcon in jtable fixed column, i created a jtable and getting a database records, then set a first column as fixed column. i set an image icon in fixed column. when i am compiling this program, i am getting only a path of the imageicon not getting an image. I fixed an imageIcon in project package folder.

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

回答1:


JTable knows Icon/ImageIcon, simple example

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



回答2:


You should not add an icon in your data model. You should add data (a boolean indicator, a String, whatever), and use a renderer for this column that will display the appropriate icon based on the data of the column.

See http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender for information and examples about cell renderers.

And please, learn the Java naming conventions and stick to them. Your code is unreadable. See http://www.oracle.com/technetwork/java/codeconv-138413.html




回答3:


Without getting to much into your code my guess is it has something to do with your tablemodel getColumnClass() method. There are plenty of tutorials how to fix that. Currently its probably rendered by tables defaultrenderer for object.

This thread should be helpful to you.

Good news is, you dont have to obfuscate your code, its already really hard to read and even harder to understand. You might want to read some java code guidelines to improve your code.



来源:https://stackoverflow.com/questions/9093641/how-to-set-an-image-for-jtable-fixed-column-when-i-am-running-its-getting-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!