使用 JTextfield 文本重复搜索 JTable [英] Search a JTable repeatedly using JTextfield text

查看:19
本文介绍了使用 JTextfield 文本重复搜索 JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将从文本字段中获取文本并在 JTable 中搜索它.它只显示文本的第一次出现.我也需要连续出现.所以,请指导我如何实现这一目标.提前致谢.

The following code will get text from a textfield and searches it in a JTable. It shows only the first occurence of the text. I need successive occurence too. So, please guide me how to achieve this. Thanks in advance.

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

推荐答案

// 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 在他的回答中也展示了这种方法

This approach was also demonstrated by @Sujay in his answer

更新

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

这篇关于使用 JTextfield 文本重复搜索 JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆