1

次のコードは、テキストフィールドからテキストを取得し、JTableで検索します。これは、テキストの最初の出現のみを示しています。私も連続して発生する必要があります。だから、これを達成する方法を教えてください。前もって感謝します。

private void search8()
{
    String target8 = sear8.getText();
    for(int row = 0; row < table8.getRowCount(); row++)
        for(int col = 0; col < table8.getColumnCount(); col++)
        {
            String next8 = (String)table8.getValueAt(row, col);
            if(next8.equals(target8))
            {
                showSearchResults(row, col);
                return;
            }
        }
}

更新しました:

private void showSearchResults(int row, int col)
{
    CustomRenderer renderer = (CustomRenderer)table8.getCellRenderer(row, col);
    renderer.setTargetCell(row, col);
    Rectangle r8 = table8.getCellRect(row, col, false);
    table8.scrollRectToVisible(r8);
    table8.repaint();
}

class CustomRenderer implements TableCellRenderer
{
    public CustomRenderer()
    {
       label = new JLabel();
       label.setHorizontalAlignment(JLabel.CENTER);
       label.setOpaque(true);
       targetRow = -1;
       targetCol = -1;
    }

    public Component getTableCellRendererComponent(JTable table,
    Object value,boolean isSelected,boolean hasFocus,int row, int column)
    {
       if(isSelected)
       {
           label.setBackground(table.getSelectionBackground());
           label.setForeground(table.getSelectionForeground());
       }
       else
       {
           label.setBackground(table.getBackground());
           label.setForeground(table.getForeground());
       }
       if(row == targetRow && column == targetCol)
       {
           label.setBackground(new Color(176,196,222));
           //label.setBorder(BorderFactory.createLineBorder(Color.red));
           label.setFont(table.getFont().deriveFont(Font.BOLD));
       }
       else
       {
           label.setBorder(null);
           label.setFont(table.getFont());
       }
       label.setText((String)value);
       return label;
    }

    public void setTargetCell(int row, int col)
    {
       targetRow = row;
       targetCol = col;
    }
} 
4

3 に答える 3

3

これを実現するには、次の 2 つの方法があります。

  • Reimeusが提案したように、単一の解析を実行して特定の検索キーワードのすべての出現を見つけ、それらの位置をリストに保存します。そして、あなたを呼び出しshowSearchResults()て、リストを繰り返します。

  • 2 番目のオプションは、最後にスキャンした場所をどこかに保存することです。次に、ユーザーが [次へ] を押すと、(0,0) ではなく、この位置から検索を開始します。

このテーブルを繰り返しスキャンする必要がないため、個人的には最初のオプションを好みます。また、このリストは、「前」および「次」機能などを非常に簡単に実装するのに役立ちます

編集:これは、これを実現する方法の 1 つです (要件に基づいてカスタマイズする必要があることに注意してください。これは、開始を支援するためのものです)。

private void search8() {
    ArrayList<String> resultList = new ArrayList<String>();

    String target8 = sear8.getText();
    for (int row = 0; row < table8.getRowCount(); row++) {
        for (int col = 0; col < table8.getColumnCount(); col++) {
            String next8 = (String) table8.getValueAt(row, col);
            if (next8.contains(target8)) {
                resultList.add(row + "," + col);
            }
        }
    }

    System.out.println(sarr8.length);

    String[] tokens;
    for (String result : resultList) {
        try {
            tokens = result.split("[,]");
            showSearchResults(tokens[0], tokens[1]);
        } finally {
            tokens = null;
        }
    }

    /**
     * Your remaining part
     */
}
于 2012-07-27T14:16:53.753 に答える
3

target8 の最初の一致が見つかった直後に戻ります。

一致する文字列のリストを作成してメソッドに渡す方がよいでしょうshowSearchResults

于 2012-07-27T13:25:14.230 に答える
1
// I'd, personally, make this protected as you may wish to change the how the search 
// is performed in the future.
protected void search8() {

    // You've assumed that there are only ever 40 elements
    // while you've allowed for a variable number of search positions
    // You would need (at least) (rowCount * colCount) * 2 elements to be
    // safe.  This is a little ridiculous considering that there might
    // only be 1 reuslt in the table
    // int[] sarr8 = new int[40]; <-- Don't really want to do this

    // Instead, we should use a dynamic array instead
    // The ArrayList is a Collection implementation backed by an array
    // but it has the means to grow (and shrink) to meet the capacity requirements
    List<Point> slist8 = new ArrayList<Point>(25); // <-- you could change the initial value as you see fit
    int i = 0;
    String target8 = sear8.getText();
    for (int row = 0; row < table8.getRowCount(); row++) {
        for (int col = 0; col < table8.getColumnCount(); col++) {
            String next8 = (String) table8.getValueAt(row, col);
            if (next8.contains(target8)) {
                // Okay, this kinda cheating, but we want to store col/row or x/y
                // cell coordinates.  You could make your own class "Cell" class,
                // but for what we want, this is exactly the same
                Point cell = new Point(col. row);
                //sarr8[i] = row;
                //sarr8[i + 1] = col;
                //i = i + 2;
                slist8.add(cell);
            }
        }
    }

    //System.out.println(sarr8.length);
    System.out.println(slist8.size());

    //for (int j = 0; j < sarr8.length; j += 2) {
    //    showSearchResults(sarr8[j], sarr8[j + 1]);
    //    return;
    //}

    // Now, personally, I'd pass in the whole result set to the "showSearchResults"
    // method, because, IMHO, that's the methods domain of responsibility, ours was
    // to simply find the results.

    showSearchResults(slist8);

    // At this point, the showSearchResults method can determine how it wants to display
    // the search results

}

このアプローチは、@Sujay の回答でも示されています。

更新しました

for (Point p : slist8) {
    showSearchResults(p.x, p.y);
}

そうしないと

private void showSearchResults(List<Point> results)
{

    for (Point p : results) 
    {
        int col = p.x;
        int row = p.y;
        CustomRenderer renderer = (CustomRenderer)table8.getCellRenderer(row, col);
        renderer.setTargetCell(row, col);
        Rectangle r8 = table8.getCellRect(row, col, false);
        table8.scrollRectToVisible(r8);
    }
    table8.repaint();
}
于 2012-07-27T22:09:18.403 に答える