2016-05-31 88 views
2

我有一个表的行分类器 - >setAutoCreateRowSorter(true);和ListSelectionModel我附加了监听器。如果我排序(通过点击标题)。Java排序表监听器

tableProducts=new JTable(table_modelProducts); 
tableProducts.setAutoCreateRowSorter(true); 
cellSelectionModelProducts = tableProducts.getSelectionModel(); 
cellSelectionModelProducts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 

的问题是,如果排序,然后点击一个细胞,它返回位于该位置(这是有表排序前的)原始值:

if(arg0.getValueIsAdjusting()){ 
    System.out.println(String.valueOf(tableProducts.getModel().getValueAt(tableProducts.getSelectedRow(), 1))); 
} 

回答

0

如果你读java代码,getSelectedRow()在选择模型上,而getModel()返回数据模型。数据模型和选择模型是2种不同的模型,这就是为什么你不能得到正确的索引。

正确的解决方案是调用JTable.getRowSorter()。convertRowIndexToModel()来获取数据模型索引。分拣机就是将底层数据模型转换为视图模型的分拣机。从逻辑上讲,它必须是维持2个模型之间映射关系的人。所以,你的代码应该是

System.out.println(String.valueOf(tableProducts.getModel() 
    .getValueAt(tableProducts.getRowSorter().convertRowIndexToModel(
    tableProducts.getSelectedRow()), 1))); 

从Java教程以JTable的例子为基地,看到的System.out.println行(“权的价值=”)如何调用上面的方法,完整的代码这里。它是可执行的,您可以通过启动应用程序来测试它,单击姓氏列标题,然后单击每个姓氏,观察控制台输出。

import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

/* 
* SimpleTableDemo.java requires no other files. 
*/ 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.ListSelectionModel; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

public class SimpleTableDemo extends JPanel { 
    private boolean DEBUG = false; 

    public SimpleTableDemo() { 
     super(new GridLayout(1,0)); 

     String[] columnNames = {"First Name", 
           "Last Name", 
           "Sport", 
           "# of Years", 
           "Vegetarian"}; 

     Object[][] data = { 
     {"Kathy", "Smith", 
     "Snowboarding", new Integer(5), new Boolean(false)}, 
     {"John", "Doe", 
     "Rowing", new Integer(3), new Boolean(true)}, 
     {"Sue", "Black", 
     "Knitting", new Integer(2), new Boolean(false)}, 
     {"Jane", "White", 
     "Speed reading", new Integer(20), new Boolean(true)}, 
     {"Joe", "Brown", 
     "Pool", new Integer(10), new Boolean(false)} 
     }; 

     final JTable table = new JTable(data, columnNames); 
     table.setPreferredScrollableViewportSize(new Dimension(500, 70)); 
     table.setAutoCreateRowSorter(true); 
     table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
     table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { 

      @Override 
      public void valueChanged(ListSelectionEvent e) { 
       if(e.getValueIsAdjusting()) { 
        System.out.println("Wrong Value = " + String.valueOf(table.getModel().getValueAt(table.getSelectedRow(), 1))); 
        System.out.println("Right Value = " + String.valueOf(table.getModel().getValueAt(table.getRowSorter().convertRowIndexToModel(table.getSelectedRow()), 1))); 
       } 
      } 

     }); 
     table.setFillsViewportHeight(true); 

     if (DEBUG) { 
      table.addMouseListener(new MouseAdapter() { 
       public void mouseClicked(MouseEvent e) { 
        printDebugData(table); 
       } 
      }); 
     } 

     //Create the scroll pane and add the table to it. 
     JScrollPane scrollPane = new JScrollPane(table); 

     //Add the scroll pane to this panel. 
     add(scrollPane); 
    } 

    private void printDebugData(JTable table) { 
     int numRows = table.getRowCount(); 
     int numCols = table.getColumnCount(); 
     javax.swing.table.TableModel model = table.getModel(); 

     System.out.println("Value of data: "); 
     for (int i=0; i < numRows; i++) { 
      System.out.print(" row " + i + ":"); 
      for (int j=0; j < numCols; j++) { 
       System.out.print(" " + model.getValueAt(i, j)); 
      } 
      System.out.println(); 
     } 
     System.out.println("--------------------------"); 
    } 

    /** 
    * Create the GUI and show it. For thread safety, 
    * this method should be invoked from the 
    * event-dispatching thread. 
    */ 
    private static void createAndShowGUI() { 
     //Create and set up the window. 
     JFrame frame = new JFrame("SimpleTableDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Create and set up the content pane. 
     SimpleTableDemo newContentPane = new SimpleTableDemo(); 
     newContentPane.setOpaque(true); //content panes must be opaque 
     frame.setContentPane(newContentPane); 

     //Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+0

请使用我的示例。我无法使它工作 – Ben

+0

用示例代码更新了我的答案。看到打印右值的行。 –

+0

@Ben,你能使它工作吗? –