html表格突出显示,java-如何突出显示jtable单元格的文本?

这篇博客介绍了如何使用Java Swing创建一个表格,并实现文本搜索功能。当在文本字段中输入搜索文本时,表格会动态更新,高亮显示匹配的行。代码示例展示了如何设置表格模型、使用`TableRowSorter`进行过滤以及自定义单元格渲染器以突出显示匹配文本。
摘要由CSDN通过智能技术生成

我有一个包含一些数据的表.当我在textField中搜索文本时,表格的外观发生变化,并且textFields消失了.我不知道为什么,我不知道我是否做对了

这是我的整个代码.

package test;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Component;

import java.awt.EventQueue;

import javax.swing.*;

import javax.swing.border.EmptyBorder;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.TableCellRenderer;

import javax.swing.table.TableModel;

import javax.swing.table.TableRowSorter;

import javax.swing.text.BadLocationException;

import javax.swing.JTextField;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JTable;

import javax.swing.JScrollPane;

import javax.swing.RowFilter;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class test3 extends JFrame {

private JPanel contentPane;

private JTable table;

private TableModel tableModel;

private JTextField textField;

private String textForSearch;

private TableRowSorter sorter;

/**

* Launch the application.

*/

public static void main(String[] args) {

/**

* Create the frame.

*/

public test3() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 346);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JScrollPane scrollPane = new JScrollPane();

scrollPane.setBounds(63, 52, 305, 191);

contentPane.add(scrollPane);

String columns [] = {

"First Name", "Last Name", "Middle Name"

};

String data[][] = new String [3][3];

data [0][0] = "denise";

data [0][1] = "alyson";

data [0][2] = "berania";

data [1][0] = "denden";

data [1][1] = "pelesco";

data [1][2] = "pogi";

data [2][0] = "ryan";

data [2][1] = "ewan";

data [2][2] = "santos";

tableModel = new DefaultTableModel(data, columns);

table = new JTable(tableModel);

scrollPane.setViewportView(table);

sorter = new TableRowSorter(tableModel);

table.setRowSorter(sorter);

textField = new JTextField();

textField.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

textForSearch = textField.getText();

if(textForSearch.length()==0){

sorter.setRowFilter(null);

}else{

sorter.setRowFilter(RowFilter.regexFilter("(?i)" + textForSearch));

}

for(int i =0;i

table.getColumnModel().getColumn(i).setCellRenderer(getRenderer());

}

}

});

textField.setBounds(262, 21, 86, 20);

contentPane.add(textField);

textField.setColumns(10);

}

private TableCellRenderer getRenderer() {

return new TableCellRenderer() {

@Override

public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {

if(arg1 != null){

textField.setText(arg1.toString());

String string = arg1.toString();

if(string.contains(textForSearch)){

int indexOf = string.indexOf(textForSearch);

try {

textField.getHighlighter().addHighlight(indexOf,indexOf+textForSearch.length(),new javax.swing.text.DefaultHighlighter.DefaultHighlightPainter(Color.RED));

} catch (BadLocationException e) {

e.printStackTrace();

}

}

} else {

textField.setText("");

textField.getHighlighter().removeAllHighlights();

}

return textField;

}

};

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值