在JTable中设置列标题

在JTable中设置列标题

本文介绍了在JTable中设置列标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用表模型的JTable:

http://s17.postimage.org/7zfh3l4lr/Screen_Shot_2012_03_10_at_15_11_31

如何使用A,B,C,D等代替我自己定义的表名.这是我的代码

这是我的表模型的代码,框架从该表模型创建一个对象,并将其显示在JFrame中.

package uk.ac.kcl.inf._4css1pra.spreadsheet;

import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

/**
 * @author imdad
 *
 */
public class Spreadsheet extends AbstractTableModel{

    private Map data = new HashMap();

    public int getColumnCount()
    {
        return 7;
    }

    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getRowCount()
     */
    public int getRowCount()
    {
        return 250;
    }

    public Object getValueAt(int row, int col)
    {
        return data.get(new Dimension(row, col));
    }

    public void setValueAt(Object data, int row, int col)
    {
        Dimension coord = new Dimension(row, col);
        this.data.put(coord, data);
        fireTableCellUpdated(row, col);

    }
}

不确定这件事有多好,但是您可以使用 DefaultTableModel 代替用于扩展AbstractTableModel的AbstractTableModel.

以下是示例代码:

package jtable;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;


public class TableIcon extends JFrame
{
    public TableIcon()
    {
        ImageIcon backIcon = getImage("/images/bac.png");
        ImageIcon exitIcon = getImage("/images/exit.png");
        ImageIcon forwardIcon = getImage("/images/forward.png");

        String[] columnNames = {"Picture", "Description"};
        Object[][] data =
        {
            {backIcon, "BACK"},
            {exitIcon, "EXIT"},
            {forwardIcon, "FORWARD"},
        };

        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();
            }
        };
        ImageIcon icon = new ImageIcon(getClass().getResource("/images/appIcon.png"));
        //model.addRow(new Object[]{icon, "Text"});
        //model.addRow(data[0]);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

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

    private ImageIcon getImage(String path)
    {
        java.net.URL url = getClass().getResource(path);
        if (url != null)
            return (new ImageIcon(url));
        else
        {
            System.out.println(url);
            return null;
        }
    }

    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        TableIcon frame = new TableIcon();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }

}

以下是输出:

I have the following JTable which uses a table model:

http://s17.postimage.org/7zfh3l4lr/Screen_Shot_2012_03_10_at_15_11_31.png

Instead of using, A,B,C,D etc. how can I define my own table names. This is my code

Here is the code for my table model, the frame creates an object from this table model and displays it in a JFrame.

package uk.ac.kcl.inf._4css1pra.spreadsheet;

import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

/**
 * @author imdad
 *
 */
public class Spreadsheet extends AbstractTableModel{

    private Map data = new HashMap();

    public int getColumnCount()
    {
        return 7;
    }

    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getRowCount()
     */
    public int getRowCount()
    {
        return 250;
    }

    public Object getValueAt(int row, int col)
    {
        return data.get(new Dimension(row, col));
    }

    public void setValueAt(Object data, int row, int col)
    {
        Dimension coord = new Dimension(row, col);
        this.data.put(coord, data);
        fireTableCellUpdated(row, col);

    }
}
解决方案

Not sure how good this thing is but you can use DefaultTableModel instead of AbstractTableModel, which extends AbstractTableModel.

Here is the code for example purpose :

package jtable;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;


public class TableIcon extends JFrame
{
    public TableIcon()
    {
        ImageIcon backIcon = getImage("/images/bac.png");
        ImageIcon exitIcon = getImage("/images/exit.png");
        ImageIcon forwardIcon = getImage("/images/forward.png");

        String[] columnNames = {"Picture", "Description"};
        Object[][] data =
        {
            {backIcon, "BACK"},
            {exitIcon, "EXIT"},
            {forwardIcon, "FORWARD"},
        };

        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();
            }
        };
        ImageIcon icon = new ImageIcon(getClass().getResource("/images/appIcon.png"));
        //model.addRow(new Object[]{icon, "Text"});
        //model.addRow(data[0]);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

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

    private ImageIcon getImage(String path)
    {
        java.net.URL url = getClass().getResource(path);
        if (url != null)
            return (new ImageIcon(url));
        else
        {
            System.out.println(url);
            return null;
        }
    }

    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        TableIcon frame = new TableIcon();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }

}

Here is the output :

这篇关于在JTable中设置列标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:07