如何在默认的jTable java Netbeans中添加状态图像

时间:2014-04-23 14:16:59

标签: java swing jtable

我的jtable功能正常。该表列出了任务以及您关联状态的每个任务。

我希望每个状态都与图像相关联。

因此图像将显示在表格中,如下所示。

------ jTable代码----------

  DefaultTableModel tmSubTask = new DefaultTableModel(null, new String[]{"Status", "Priority", "Task", "SubTask", "Desc", "Prevision Begin", "Time", "Prevision Duration", "Prevision hour", "Begin", "Hour Begin","End Date" ,"End Hour", "Duration"});
List<SubTask> subTask;
ListSelectionModel lsmSubTask;

Connection conexao = null;
PreparedStatement pst= null;
ResultSet rs = null;

 private void showSubTask(List<SubTask> subTask) {
    while (tmSubTask.getRowCount() > 0) {
        tmSubTask.removeRow(0);
    }
    if (subTask.size() == 0) {
        JOptionPane.showMessageDialog(null, "showSubTask");
    } else {
        String[] line = new String[]{null, null, null};
        for (int i = 0; i < SubTask.size(); i++) {
         tmSubTask.addRow(linha);
         tmSubTask.setValueAt(subTask.get(i).getStatus(), i, 0);
         tmSubTask.setValueAt(subTask.get(i).getIdPriority(), i, 1);
         tmSubTask.setValueAt(subTask.get(i).getIdTask(), i, 2);
         tmSubTask.setValueAt(subTask.get(i).getIdSubTask(), i, 3);
         tmSubTask.setValueAt(subTask.get(i).getDescSubTask(), i, 4);
         tmSubTask.setValueAt(subTask.get(i).getDateBegin(), i, 5);
         tmSubTask.setValueAt(subTask.get(i).getTerm(), i, 6);
         tmSubTask.setValueAt(subTask.get(i).getDuration(), i, 7);
         tmSubTask.setValueAt(subTask.get(i).gethourBeginP(), i, 8);
         tmSubTask.setValueAt(subTask.get(i).getDateBegin(), i, 9);
         tmSubTask.setValueAt(subTask.get(i).getHourBegin(), i, 10);
         tmSubTask.setValueAt(subTask.get(i).getDateEnd(), i, 11);
         tmSubTask.setValueAt(subTask.get(i).getHourEnd(), i, 12);
         tmSubTask.setValueAt(subTask.get(i).getDuration(), i, 13);

        }
    }
}

---------------的JTable --------------

enter image description here

---------------我想要什么---------

enter image description here

想象一下,如果像jTable中的延迟状态显示图像为红色。

老实说,我甚至不知道从哪里开始。

如果我不清楚,我道歉。

感谢你们给我的任何帮助。

问候。

我会尽力解释任何问题。

1 个答案:

答案 0 :(得分:0)

将ImageIcon添加到表中并覆盖JTable的getColumnClass(...)方法以返回Icon.class,表格将使用适当的渲染器来显示Icon。类似的东西:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableIcon extends JFrame
{
    public TableIcon()
    {
        Icon aboutIcon = new ImageIcon("about16.gif");
        Icon addIcon = new ImageIcon("add16.gif");
        Icon copyIcon = new ImageIcon("copy16.gif");

        String[] columnNames = {"Picture", "Description"};
        Object[][] data =
        {
            {aboutIcon, "About"},
            {addIcon, "Add"},
            {copyIcon, "Copy"},
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable( model )
        {
            //  Returning the Class of each column will allow different
            //  renderers to be used based on Class
            public Class getColumnClass(int column)
            {
                return getValueAt(0, column).getClass();
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );

        new TableRowResizer(table);

        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumnAdjuster tca = new TableColumnAdjuster(table);
        tca.adjustColumns();
    }

    public static void main(String[] args)
    {
        TableIcon frame = new TableIcon();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }

}