【问题标题】:Set the view to a specific cell将视图设置为特定单元格
【发布时间】:2022-06-29 12:26:59
【问题描述】:

您知道如何将视图设置为JTable 中的特定单元格吗? 因为我正在搜索单元格中的特定内容(例如 Ctrl + F)

我有一个特定的单元格,例如第 39 行和第 5 列,但我不知道如何查看它

我查看了JTableDefaultTableModel,但没有看到任何有用的方法。

【问题讨论】:

    标签: java swing jframe jtable


    【解决方案1】:

    如果您想选择(突出显示)特定的 JTable Cell,那么这可能是您可以做到的一种方式:

    public static void selectJTableCell(javax.swing.JTable theTable, 
                             int literalCellRowNumber, int literalCellColumnNumber) {
        /* Set the Selection mode...   */
        theTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        /* Make sure ColumnSelectionAllowed is enabled 
          so that just the cell is selected.        */
        theTable.setColumnSelectionAllowed(true);
        /* Make sure RowSelectionAllowed is enabled. 
           (It should be by default anyways).     */
        theTable.setRowSelectionAllowed(true);
        /* Select the desired cell. We subtract 1 from 
           the supplied LITERAL Cell Row Number and the
           LITERAL Cell Column Number values supplied 
           since we're asking for the literal row/column
           numbers rather than index numbers. If you would 
           rather use an index value then remove the -1's.  */
        theTable.changeSelection(literalCellRowNumber - 1, literalCellColumnNumber - 1, false, false);
    }
    

    如何使用此方法:

    selectJTableCell(jTable1, 39, 5);
    

    因此,如果您想选择整个 JTable ,那么这可能是您可以做到的一种方式:

    public static void selectJTableRow(javax.swing.JTable theTable, int literalRowNumber) {
        /* Subtract 1 from the supplied LITERAL Row 
           Number value supplied since we're asking 
           for the literal row number rather than the 
           index number. If you would rather use an 
           index value then remove this code line.  */
        literalRowNumber = literalRowNumber - 1;
        /* Disable ColumnSelectionAllowed otherwise the 
           row will not be highlighted.        */
        theTable.setColumnSelectionAllowed(false);
        /* Make RowSelectionAllowed is enabled.*/  
        theTable.setRowSelectionAllowed(true);
        /* Select the first cell in the desired row to 
           ensure the table will scroll to the row 
           selection so that it will be visible within 
           the viewport.                */
        theTable.changeSelection(literalRowNumber, 0, false, false);
        // Now, Select the row.
        theTable.setRowSelectionInterval(literalRowNumber, literalRowNumber);
        
    }
    

    如何使用此方法:

    selectJTableRow(jTable1, 39);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多