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

问题描述

我有一个RowSorterListener.我想知道哪个列触发了RowSorterEvent.但是,当我尝试获取该列时,没有得到我想要的输出.

I have a RowSorterListener. I would like to be able to tell which column fires the RowSorterEvent. However when I attempt to get the column, I do not get the output I desire.

public class CustomRowSorterListener implements RowSorterListener {
    JTable table;
    public CustomRowSorterListener(JTable table) {
        this.table = table;
    }

    @Override
    public void sorterChanged(RowSorterEvent e)
    {
        //Attempt 1
        System.out.println(e.getSource()); //Returns RowSorter and not a column

        //Attempt 2
        System.out.println(e.getSource().getColumn()); //Caused error in code

        //Attempt 3
        System.out.println(table.getColumn()) //Didn't work because no arguments were 
                                              //provided. However, I was unsure if it could
                                              //be done this way.

        //System.out.println(table.getColumn(e.getSource())) also doesn't work
    }
}

对于获得有效解决方案的帮助,我将不胜感激.

I would appreciate any help in getting a solution which works.

推荐答案

SortKey开始.它可以返回getColumn.例如:

Start with the SortKey. It can return getColumn. For example:

import java.awt.EventQueue;
import java.util.List;

import javax.swing.*;
import javax.swing.RowSorter.SortKey;
import javax.swing.event.*;
import javax.swing.table.*;

public class SortTest {

    private JFrame frame = new JFrame(getClass().getSimpleName());

    private DefaultTableModel model = new DefaultTableModel(10, 2) {
        private static final long serialVersionUID = 1L;

        @Override
        public Class<?> getColumnClass(int column) {
            return column == 1 ? Integer.class : Object.class;
        }
    };
    private JTable table = new JTable(model);

    public SortTest() {
        for (int row = model.getRowCount(); --row >= 0;) {
            int i = 20 + row % 20;
            model.setValueAt(row + " " + i, row, 0);
            model.setValueAt(i + row, row, 1);
        }
        table.setAutoCreateRowSorter(true);
        TableRowSorter<?> sorter = (TableRowSorter<?>) table.getRowSorter();
        sorter.setSortsOnUpdates(true);
        sorter.addRowSorterListener(new RowSorterListener() {

            @Override
            public void sorterChanged(RowSorterEvent rse) {
                if (rse.getType() == RowSorterEvent.Type.SORT_ORDER_CHANGED) {
                    List<SortKey> keys = rse.getSource().getSortKeys();
                    for (SortKey key : keys) {
                        System.out.println("Column NO. - " + key.getColumn() + " is sorted");
                        if (key.getColumn() == 0) {
                            break;
                        } else {
                            break;
                        }
                    }
                }
            }
        });
        frame.add(new JScrollPane(table));
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            new SortTest();
        });
    }
}

这篇关于获取触发RowSorterEvent的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 17:34