How to highlight the text of a jtable cell?
I have a table that contains some data. When I preview the text in the text box, the table appearance, changes and text boxes disappear. I don’t know why, I don’t know if I am doing it right.
here is my whole code ..
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<TableModel> 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>(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.getColumnCount();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;
}
};
}
}
source to share
- You are using a search text box as a means of displaying table cells. This makes all cells look like text boxes and the search box disappears from its original location.
- You are installing an ActionListener to render a cell inside an ActionListener. This means that # 1 will happen after you try to find something.
source to share
I would suggest a slightly different approach:
Instead of getRender () returning an anonymous TableCellRender (), extend your code a little more to extend the JLable to return so you can freely change properties like setBackground (), setForeground (), etc. with much more freedom.
You will find more information here: http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableCellRenderer.html
public class DefaultTableCellRenderer extends JLabel implements TableCellRenderer {
}
Here is a complete JList example that applies the same to a JTable, moving on to the appropriate interfaces: (focus on the last two if-else)
public class ThumbnailCellRenderer extends JLabel implements ListCellRenderer {
private static final Color HIGHLIGHT_COLOR = new Color(0,0,128);
public ThumbnailCellRenderer() {
this.setOpaque(true);
this.setIconTextGap(12);
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
// COMPLETE BODY
Photo photo = (Photo)value;
ImageIcon thumbnail = photo.getThumbnail();
if(thumbnail != null) {
this.setToolTipText("Double Click for Slideshow - "+photo.getName());
this.setIcon(thumbnail);
this.setText(photo.getCaption()+" - "+Utilities.getReadableDateAndTime(photo.getTime()));
} else {
this.setIcon(Icons.IMAGEREMOVED_ICON.get());
this.setText(photo.getCaption()+" - "+Utilities.getReadableDateAndTime(photo.getTime())+" - Physical Image Removed");
}
if(isSelected) {
setBackground(HIGHLIGHT_COLOR);
setForeground(Color.WHITE);
} else {
setBackground(Color.WHITE);
setForeground(Color.black);
}
return this;
}
}
Finally, you just added the render as you do in your MainFrame code.
I hope this helps.
source to share