Jtable 按数值排序

package tablesort;

import java.awt.;
import java.awt.event.ActionListener;
import java.util.Comparator;
import javax.swing.
;
import javax.swing.table.*;

public class SortByInt {
public static void main(String[] args) {
String[][] data = new String[][] { { “49”, “a”, “Test1” },
{ “24”, “b”, “Test2” }, { “9”, “c”, “Test3” },
{ “5”, “d”, “Test4” },{ “201”, “a”, “Test1” }};

	String[] title = new String[] { "a1", "b1", "c1" };

	DefaultTableModel model = new DefaultTableModel(data, title);
	JTable table = new JTable(model);
	String[] rowValues = { "1", "f", "TestAdd" };
	model.addRow(rowValues);

	final TableRowSorter sorter = new TableRowSorter(
			model);

	final JTextField field = new JTextField(20);
	final JComboBox list = new JComboBox(title);
	JButton button = new JButton("Find");

	table.setRowSorter(sorter);

	sorter.setComparator(0, new Comparator() {
		public int compare(Object arg0, Object arg1) {
			try {
				int a = Integer.parseInt(arg0.toString());
				int b = Integer.parseInt(arg1.toString());
				return a - b;
			} catch (NumberFormatException e) {
				return 0;
			}
		}
	});

	button.addActionListener(new ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			int index = list.getSelectedIndex();
			sorter.setRowFilter(RowFilter.regexFilter(field.getText(),
					index));
		}
	});

	JFrame frame = new JFrame("TableSort");
	JPanel contentPanel = new JPanel(new BorderLayout());
	JPanel searchPanel = new JPanel();
	searchPanel.add(list);
	searchPanel.add(field);
	searchPanel.add(button);
	contentPanel.add(searchPanel, BorderLayout.NORTH);
	contentPanel.add(new JScrollPane(table), BorderLayout.CENTER);
	frame.getContentPane().add(contentPanel);
	frame.pack();
	frame.setLocationRelativeTo(null);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
}
 
  

}

你可能感兴趣的:(java)