FileDocCategorySizeDatePackage
TestSortableTableModel.javaAPI DocExample5163Mon Jan 09 11:01:58 GMT 2006None

TestSortableTableModel

public class TestSortableTableModel extends JPanel implements ActionListener

Fields Summary
DefaultTableModel
myModel
SortableTableModel
mySortableModel
JButton
sort1
JButton
sort2
JButton
sort3
JButton
bonus
static Object[]
headers
static Object[]
data
static Object[]
bonusData
Constructors Summary
public TestSortableTableModel(DefaultTableModel m)


        
        super (new BorderLayout());
        myModel = m;
        mySortableModel = new SortableTableModel (myModel);
        mySortableModel.setComparatorForColumn (new MyColorComparator(), 2);
        JTable table = new JTable (mySortableModel);
        table.setDefaultRenderer (java.awt.Color.class, new ColorRenderer());
        JScrollPane scroller =
            new JScrollPane (table,
                             ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                             ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        table.setPreferredScrollableViewportSize (new Dimension (400, 200));
        setLayout(new BorderLayout());
        add (scroller, BorderLayout.CENTER);
        // add sort buttons
        JPanel buttonPanel = new JPanel();
        sort1 = new JButton ("Sort 1");
        buttonPanel.add(sort1);
        sort1.addActionListener(this);
        sort2 = new JButton ("Sort 2");
        buttonPanel.add(sort2);
        sort2.addActionListener(this);
        sort3 = new JButton ("Sort 3");
        buttonPanel.add(sort3);
        sort3.addActionListener(this);
        bonus = new JButton ("More data");
        buttonPanel.add(bonus);
        bonus.addActionListener(this);
        add (buttonPanel, BorderLayout.SOUTH);

    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        if (e.getSource() == sort1) {
            mySortableModel.setSortColumn (0);
        } else if (e.getSource() == sort2) {
            mySortableModel.setSortColumn (1);
        } else if (e.getSource() == sort3) {
            mySortableModel.setSortColumn (2);
        } else if (e.getSource() == bonus) {
            myModel.addRow (bonusData);
        }


    
public static voidmain(java.lang.String[] args)

        DefaultTableModel aModel =
            new DefaultTableModel(data, headers) ;
        JFrame frame = new JFrame ("Sortable Table");
        frame.getContentPane().add (new TestSortableTableModel(aModel),
                                    BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);