简体   繁体   English

如何突出显示jtable中的多个单元格

[英]how to highlight multiple cells in jtable

I have to search text in jtable data. 我必须在jtable数据中搜索文本。 eg "ADMIN" text is appearing multiple places in jtable then how to highlight all the cells that contains specified value. 例如,“ ADMIN”文本在jtable中出现多个位置,然后如何突出显示包含指定值的所有单元格。

Does anyone have any idea ? 有人有什么主意吗 ?

In SwingX (biased me can't resist showing off :-) the solution boils down to installing a Highlighter and configure it with a SearchPredicate as needed: 在SwingX中(让我无法抗拒炫耀:-),该解决方案归结为安装荧光笔,并根据需要使用SearchPredicate对其进行配置:

// instantiate a background decorator
final ColorHighlighter hl = new ColorHighlighter(HighlightPredicate.NEVER, Color.YELLOW, null);
// register it with the table (of type JXTable)
table.addHighlighter(hl);
// set the predicate, f.i. highlight all cell that contain ADMIN
hl.setHighlightPredicate(new SearchPredicate("ADMIN"));

As @kleopatra suggested, use a custom CellRenderer (the following example is just a POC, add methods to change the search pattern, highlight color, etc.): 如@kleopatra所建议,请使用自定义CellRenderer (以下示例仅是POC,添加方法以更改搜索模式,突出显示颜色等):

import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class CellHighlighter {

    private static class CellHighlighterRenderer extends JLabel implements TableCellRenderer {

        public CellHighlighterRenderer() {
            setOpaque(true); // Or color won't be displayed!
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            String val = (String)value;
            Color c;
            if (val.matches(".*MIN.*")) // Add a method to configure the regexpr
                c = Color.YELLOW; // Add a method to configure color
            else
                c = UIManager.getColor("Table.background");
            setBackground(c);
            setText(val);
            return this;
        }
    }

    public static void main(String[] args) {
        String[] columnNames = {
            "Login", "Real name", "Age", "Birthday"
        };
        String[][] data = {
            {"toto", "Toto Mackwert", "73", "18/06/1940"},
            {"adm", "ADMINISTRATOR", "13", "01/01/2000"},
            {"AMINA", "Amina Farou", "3", "01/01/2010"},
        };
        JTable table = new JTable(data, columnNames);
        table.setDefaultRenderer(Object.class, new CellHighlighterRenderer());
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(scrollPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM