Java – set column headers in JTable

I have the following JTable 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 do I define my table name This is my code

This is the code of my table model. The framework creates an object from the table model and displays it in 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);

    }
}

Solution

I don't know how good this thing is, but you can use defaulttablemodel instead of abstracttablemodel, which extends abstracttablemodel

The following is code for example purposes:

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

}

This is the output:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>