java swing中对于JTable的使用(一)

package com.robert.JTable;

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

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 11-11-13
 * Time: 上午9:43
 * To change this template use File | Settings | File Templates.
 */
public class PlanetTable {

    public static void main(String args[])
    {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new PlanetTableFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }

}

 
package com.robert.JTable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 11-11-13
 * Time: 上午9:47
 * To change this template use File | Settings | File Templates.
 */
public class PlanetTableFrame extends JFrame
{
    private static int DEFAULT_WIDTH = 400;
    private static int DEFAULT_HEIGHT = 300;
    private Object[][] cells = {{"Mercury",2440.0,0,false, Color.yellow},
            {"Venus",6052.0,false,Color.yellow},{"Earth",6378.0,false,Color.blue},
            {"Saturn",60268.0,18,true,Color.orange},
            {"Uranus",25559.0,17,true,Color.blue},{"Nepture",24766.0,8,true,Color.blue},
            {"Pluto",1137.0,1,false,Color.black}
    };

    private String[] columnNames = {"Planet", "Radius", "Moons", "Gaseous", "Color"};

    public PlanetTableFrame()
    {
        setTitle("PlanetTable");
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
        final JTable table = new JTable(cells,columnNames);
        table.setAutoCreateRowSorter(true);
        add(new JScrollPane(table),BorderLayout.NORTH);
        JButton printButton = new JButton("print");
        printButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                try
                {
                    table.print();
                }
                catch (PrinterException e)
                {
                    e.printStackTrace();
                }
            }
        });

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(printButton);
        add(buttonPanel, BorderLayout.CENTER);
        buttonPanel.updateUI();
    }

}

原文地址:https://www.cnblogs.com/mengjianzhou/p/5986892.html