开发者

jTable and Sorting

开发者 https://www.devze.com 2023-01-26 23:27 出处:网络
How do i sort jtable column using radio button? my jtable is defaultTableModel not vectors. I have already achieve when user press开发者_开发百科 on column header, it will sort, now i have to implem

How do i sort jtable column using radio button?

my jtable is defaultTableModel not vectors.

I have already achieve when user press开发者_开发百科 on column header, it will sort, now i have to implement using radio button..

What would be the best way to achieve this?


To do a sort programmatically you add code like the following to your listener:

DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
ArrayList list = new ArrayList();
list.add( new RowSorter.SortKey(2, SortOrder.ASCENDING) );
sorter.setSortKeys(list);
sorter.sort();


Add an actionlistener to the radiobutton, sort and set the tableModel. The Vector argument is an input to defaultTableModel.

final JTable table = new JTable();
JRadioButton button = new JRadioButton();
button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    //sort your data here
    table.setModel(new DefaultTableModel(sortedDate));
    table.repaint();// maybe revalidate too
  }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消