将视图设置为特定的单元格

发布于 2025-02-04 15:50:57 字数 211 浏览 4 评论 0原文

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

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

我在jtable < /code>和defaultTableModel,但我看不到任何有用的方法。

Do you know how to set the view to a specific cell in a JTable?
Because I'm working on searching for specific content in cells (Such Ctrl + F)

I have a specific cell such as row 39 and column 5, but I don't know how to view it

I have looked in JTable and DefaultTableModel but I don't see any useful methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2025-02-11 15:50:57

如果要选择(突出显示)特定的jtable 单元格,那么这可能是一种方法:

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);

If you want to Select (highlight) a specific JTable Cell then this could be one way you can do it:

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);
}

How you might use this method:

selectJTableCell(jTable1, 39, 5);

Consequently, if you want to select an entire JTable Row, then this could be one way you can do it:

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);
    
}

How you might use this method:

selectJTableRow(jTable1, 39);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文