I want to make a search functionality for JTable object.
I have a JTextFiled where i put my text to search.
I want to change colors rows and cells which contains this text.
Now i'm stuck because i have no idea how to change color dynamically.
public class TableSearchCellRenderer extends DefaultTableCellRenderer {
String search = "";
public void setSearch(String search) {
this.search = search;
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (table.getValueAt(row, column).equals(search)) {
setBackground(Color.green);
}
return this;
}
}
I've tried with something like this but it doesn't work.
I tried to call rerender table like this way
String selectedTitle = tabbed.getTitleAt(tabbed.getSelectedIndex());
JTable table = tabels.get(selectedTitle);
((TableSearchCellRenderer)table.getDefaultRenderer(String.class)).setSearch(searchField.getText());
table.repaint();
((AbstractTableModel) table.getModel()).fireTableDataChanged();
but with no success :(
Could you help me?
Here is a working version of your search feature based on the TableCellRenderer. One of the issue you may have hit, is that, although your TableModel contains String, if you use the DefaultTableModel, it always returns Object.class for all data and the JTable will therefore use the DefaultTableCellRenderer instead of your TableSearchCellRenderer.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.Random;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class TestTable2 {
private String search;
public String getSearch() {
return search;
}
public void setSearch(String search) {
this.search = search;
}
private class TableSearchRenderer extends DefaultTableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(null);
Component tableCellRendererComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (getSearch() != null && getSearch().length() > 0 && value.toString().contains(getSearch())) {
setBackground(Color.RED);
}
return tableCellRendererComponent;
}
}
protected void initUI() {
DefaultTableModel model = new DefaultTableModel();
for (int i = 0; i < 5; i++) {
model.addColumn("Column " + (i + 1));
}
Random random = new Random();
for (int i = 0; i < 200; i++) {
Vector<Object> row = new Vector<Object>();
for (int j = 0; j < 40; j++) {
row.add(WORDS[random.nextInt(WORDS.length)]);
}
model.addRow(row);
}
table = new JTable(model);
TableSearchRenderer renderer = new TableSearchRenderer();
table.setDefaultRenderer(Object.class, renderer);
textField = new JTextField(30);
textField.getDocument().addDocumentListener(new DocumentListener() {
#Override
public void removeUpdate(DocumentEvent e) {
updateSearch();
}
#Override
public void insertUpdate(DocumentEvent e) {
updateSearch();
}
#Override
public void changedUpdate(DocumentEvent e) {
updateSearch();
}
});
JFrame frame = new JFrame(TestTable2.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add(scrollpane, BorderLayout.CENTER);
frame.add(textField, BorderLayout.NORTH);
frame.setSize(1000, 800);
frame.setVisible(true);
}
protected void updateSearch() {
setSearch(textField.getText());
table.repaint();
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestTable2().initUI();
}
});
}
private static final String[] WORDS = { "art", "australia", "baby", "beach", "birthday", "blue", "bw", "california", "canada", "canon",
"cat", "chicago", "china", "christmas", "city", "dog", "england", "europe", "family", "festival", "flower", "flowers", "food",
"france", "friends", "fun", "germany", "holiday", "india", "italy", "japan", "london", "me", "mexico", "music", "nature",
"new", "newyork", "night", "nikon", "nyc", "paris", "park", "party", "people", "portrait", "sanfrancisco", "sky", "snow",
"spain", "summer", "sunset", "taiwan", "tokyo", "travel", "trip", "uk", "usa", "vacation", "water", "wedding" };
private JTable table;
private JTextField textField;
}
See Table Row Rendering. The example does row level highlighting on a fixed word. You would need to modify the code to do the highlighting on your search word.
Another approach is to filter the table to see only the rows that contain the text you are searching for. See Sorting and Filtering for a working example.
It works. I change prepare renderer.
JTable table = new JTable() {
private static final long serialVersionUID = 1L;
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
**if (column == 0) {
for (int i = 0; i < getModel().getColumnCount(); i++) {
if (((TableSearchRenderer) getDefaultRenderer(String.class)).getSearch().length() > 0 && ((TableSearchRenderer) getDefaultRenderer(String.class)).getSearch().toLowerCase().equals(getModel().getValueAt(row, i).toString().toLowerCase()))
((TableSearchRenderer) getDefaultRenderer(String.class)).getRows().add(row);
}
}**
if (((TableSearchRenderer) getDefaultRenderer(String.class)).getRows().contains(row) && c.getBackground() != Color.RED) {
c.setBackground(Color.GREEN);
}
return c;
}
};
Related
I have a JTable with numbers 1,2,3 as 1st column and the number as text in the 2nd column which can be chosen with a JCombobox. For example 1 can be represented as "1", ONE, FIRST or ONCE in the 2nd column. When I make a choice, all the comboboxes of the rows below have to be updated in cascade with the text of the same nature. So if I choose ONCE, the comboboxes of the rows below should be updated to TWICE, THRICE. If I choose FIRST, the comboboxes of the rows below should be updated to SECOND, THIRD. And so on..
At first it looks like it's working but whenever I click somewhere else on the JTable, the combobox is updated with the value of the last row. For example, if I choose ONCE in the 1st row, at first it will update the other rows to TWICE and THRICE. Then if I click on any row, the combobox selection will be updated to THRICE on the 1st row. Next time I click, the 2nd row is updated to THRICE.
What am I doing wrong here?
The combobox cell editor:
public class NumberChoiceEditor extends DefaultCellEditor {
private static final long serialVersionUID = 1L;
private String numberAsText;
private static final String[][] NUMBERS = { { "1", "ONE", "FIRST", "ONCE" }, { "2", "TWO", "SECOND", "TWICE" }, { "3", "THREE", "THIRD", "THRICE" } };
public NumberChoiceEditor() {
super(new JComboBox<>());
}
#Override
public Object getCellEditorValue() {
return numberAsText;
}
#Override
public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected, final int row, final int column) {
if (value instanceof String) {
numberAsText = (String) value;
}
JComboBox<String> numberTextChoice = new JComboBox<>(NUMBERS[row]);
numberTextChoice.setSelectedItem(numberAsText);
numberTextChoice.addActionListener(e -> {
numberAsText = (String) numberTextChoice.getSelectedItem();
int nextRow = row + 1;
if (nextRow < NUMBERS.length) {
String numberText = (String) table.getValueAt(nextRow, 1);
JComboBox<String> nextRowChoices = (JComboBox<String>) getTableCellEditorComponent(table, numberText, isSelected, nextRow, column);
nextRowChoices.setSelectedIndex(numberTextChoice.getSelectedIndex());
table.setValueAt(nextRowChoices.getSelectedItem(), nextRow, 1);
}
});
return numberTextChoice;
}
}
The main class with the frame:
public class NumberTable {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JTable table = new JTable(new Object[][] { { 1, "1" }, { 2, "2" }, { 3, "3" } }, new Object[] { "Number", "Number Text" });
table.getColumn("Number Text").setCellEditor(new NumberChoiceEditor());
table.setRowHeight(25);
JFrame frame = new JFrame("Number Table");
frame.add(new JScrollPane(table));
frame.setLocationRelativeTo(null);
frame.setSize(600, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
I would use a TableModelListener. When the value is changed via the JComboBox editor, I would adjust the TableModel accordingly.
(Notes after the code.)
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class NumTable implements Runnable, TableModelListener {
private boolean adjusting;
private JFrame frame;
private JTable table;
#Override
public void run() {
createGui();
}
#Override
public void tableChanged(TableModelEvent event) {
if (!adjusting) {
adjusting = true;
int col = event.getColumn();
if (col == 1) {
NumChoiceEd editor = (NumChoiceEd) table.getColumnModel().getColumn(1).getCellEditor();
int row = event.getFirstRow();
JComboBox<String> combo = editor.getCombo(row);
if (combo != null) {
int ndx = combo.getSelectedIndex();
if (ndx >= 0) {
int rows = table.getRowCount();
for (int i = 0; i < rows; i++) {
combo = editor.getCombo(i);
String val = combo.getModel().getElementAt(ndx);
table.setValueAt(val, i, col);
}
}
}
}
adjusting = false;
}
}
private void createGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTable(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JScrollPane createTable() {
Object[][] data = new Object[][]{{1, "1"}, {2, "2"}, {3, "3"}};
Object[] columnNames = new Object[]{"Number", "Number Text"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
model.addTableModelListener(this);
table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumnModel tcm = table.getColumnModel();
TableColumn column = tcm.getColumn(1);
column.setCellEditor(new NumChoiceEd());
JScrollPane scrollPane = new JScrollPane(table);
return scrollPane;
}
public static void main(String[] args) {
EventQueue.invokeLater(new NumTable());
}
}
class NumChoiceEd extends DefaultCellEditor {
private static final long serialVersionUID = 1L;
private JComboBox<String> oneCombo;
private JComboBox<String> twoCombo;
private JComboBox<String> threeCombo;
public NumChoiceEd() {
super(new JComboBox<>());
oneCombo = new JComboBox<>(new String[]{"1", "ONE", "FIRST", "ONCE"});
twoCombo = new JComboBox<>(new String[]{"2", "TWO", "SECOND", "TWICE"});
threeCombo = new JComboBox<>(new String[]{"3", "THREE", "THIRD", "THRICE"});
}
public JComboBox<String> getCombo(int row) {
switch (row) {
case 0:
return oneCombo;
case 1:
return twoCombo;
case 2:
return threeCombo;
default:
return null;
}
}
#Override
public Component getTableCellEditorComponent(final JTable table,
final Object value,
final boolean isSelected,
final int row,
final int column) {
if (column == 1) {
switch (row) {
case 0:
return oneCombo;
case 1:
return twoCombo;
case 2:
return threeCombo;
default:
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
else {
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
}
Rather than create a new JComboBox each time method getTableCellEditorComponent is called, I initially create all three JComboBoxes and return the relevant one.
You don't need to override method getCellEditorValue because the superclass method (in class DefaultCellEditor) will return the correct value.
Once the user changes the value (and closes the table cell editor), method tableChanged is invoked. In that method, I get the index of the value that was selected from the JComboBox and then I go through all the rows in the JTable and get the value at that index in the JComboBox for each row and set the JTable value to that value.
Because I change the TableModel in method tableChanged, that will cause the method to be called again. In order to prevent the recursive call, I use the adjusting flag.
In the below screen capture, I have selected a value from the JComboBox but I have not yet closed the editor. If I navigate to a different cell in the JTable that will close the editor and then all the displayed data will change. Note that if you navigate to column Number Text in a different row, you may not see the change since that will immediately open the JComboBox editor for the cell that you navigated to.
After I close the editor, the table looks as in the below screen capture.
Note that there is a lot of blank space in the above screen captures since the default dimensions of JTable are quite large but the space required to display the data (in this case) is small. One way to make the JTable smaller (in this case) would be to change the preferred size of the JScrollPane.
EDIT
In response to the question you asked in your comment, namely
is it possible to update on the combobox value change
Yes, it is possible. You add an ActionListener to each JComboBox that simply calls method stopCellEditing. Here is the above code, modified to include the ActionListener. The only changes are in class NumChoiceEd.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class NumTable implements Runnable, TableModelListener {
private boolean adjusting;
private JFrame frame;
private JTable table;
#Override
public void run() {
createGui();
}
#Override
public void tableChanged(TableModelEvent event) {
if (!adjusting) {
adjusting = true;
int col = event.getColumn();
if (col == 1) {
NumChoiceEd editor = (NumChoiceEd) table.getColumnModel().getColumn(1).getCellEditor();
int row = event.getFirstRow();
JComboBox<String> combo = editor.getCombo(row);
if (combo != null) {
int ndx = combo.getSelectedIndex();
if (ndx >= 0) {
int rows = table.getRowCount();
for (int i = 0; i < rows; i++) {
combo = editor.getCombo(i);
String val = combo.getModel().getElementAt(ndx);
table.setValueAt(val, i, col);
}
}
}
}
adjusting = false;
}
}
private void createGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTable(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JScrollPane createTable() {
Object[][] data = new Object[][]{{1, "1"}, {2, "2"}, {3, "3"}};
Object[] columnNames = new Object[]{"Number", "Number Text"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
model.addTableModelListener(this);
table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumnModel tcm = table.getColumnModel();
TableColumn column = tcm.getColumn(1);
column.setCellEditor(new NumChoiceEd());
JScrollPane scrollPane = new JScrollPane(table);
return scrollPane;
}
public static void main(String[] args) {
EventQueue.invokeLater(new NumTable());
}
}
class NumChoiceEd extends DefaultCellEditor {
private static final long serialVersionUID = 1L;
private JComboBox<String> oneCombo;
private JComboBox<String> twoCombo;
private JComboBox<String> threeCombo;
public NumChoiceEd() {
super(new JComboBox<>());
ActionListener al = event -> NumChoiceEd.this.stopCellEditing(); // ADDED THIS LINE
oneCombo = new JComboBox<>(new String[]{"1", "ONE", "FIRST", "ONCE"});
oneCombo.addActionListener(al); // ADDED THIS LINE
twoCombo = new JComboBox<>(new String[]{"2", "TWO", "SECOND", "TWICE"});
twoCombo.addActionListener(al); // ADDED THIS LINE
threeCombo = new JComboBox<>(new String[]{"3", "THREE", "THIRD", "THRICE"});
threeCombo.addActionListener(al); // ADDED THIS LINE
}
public JComboBox<String> getCombo(int row) {
switch (row) {
case 0:
return oneCombo;
case 1:
return twoCombo;
case 2:
return threeCombo;
default:
return null;
}
}
#Override
public Component getTableCellEditorComponent(final JTable table,
final Object value,
final boolean isSelected,
final int row,
final int column) {
if (column == 1) {
switch (row) {
case 0:
return oneCombo;
case 1:
return twoCombo;
case 2:
return threeCombo;
default:
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
else {
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
}
I want to sort a JTable like this:
That is: (EDIT)
If I click the "Name" column to sort, "Tom", "Polazzo" and "Anna" must be sorted alphabetically, and rows with the same names must stay together("grouped" by the name), and each name should be shown only once, the rest cells must be blank.
If I click the "Duration" or "Book #" column, I want all rows sorted ascending/descending by values of duration/book number, but same as in point 1), rows with the same "Name" must stay together, that is, stay grouped, and only the first row in every group is shown, and the rest "Name" stay blank.
The data in the table model's vector are collected from parsing a XML file. The rows with same "Name" are under the same node in the hierarchy tree.
I think there're two ways to do this:
a) When collecting the data and construct the rows, under the same "Name" node, give the cell at column 0 the "Name" value, and leave the rest of rows "" in the same column. But, I don't know how to construct the comparator of column "Name", to ensure the first row always being the top in sorting. (It cannot be the biggest and the smallest when we override compare() method, can it?)
b) Every time we click the table header to sort, make the renderer repaint the table the way we want: comparing the value in the first line of each group, and if it's the same as the last line's column 0's value, don't paint this cell, until we reach another different value. In that way, we don't mess with comparators/sorters, and it turns into a renderer problem. That's what I kind of achieved in the SSCCE below, but I am half way there and I need some tips.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.List;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Comparator;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter;
import javax.swing.RowSorter.SortKey;
import javax.swing.event.RowSorterEvent;
import javax.swing.event.RowSorterListener;
import javax.swing.SortOrder;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import com.WindThunderStudio.JHeaderToolTip.JHeaderToolTip;
import net.miginfocom.swing.MigLayout;
public class RowGroupInTable extends JFrame {
public RowGroupInTable() {
begin();
}
private void begin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
DefaultTableModel model = new DefaultTableModel();
Object[][] data = new Object[][] {{"Tom", "17", "Book1"},
{"Tom", 23, "Book2"},
{"Tom", 25, "Book3"},
{"Polazzo", 41, "Book1"},
{"Polazzo", 45, "Book2"},
{"Polazzo", 12, "Book3"},
{"Anna", 1, "Book3"},
{"Anna", 33, "Book5"}};
String[] titles = new String[] {"Name", "Last job duration", "Book #"};
JTable table = new JTable(data, titles);
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(false);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
ArrayList<SortKey> sortKeys = new ArrayList<RowSorter.SortKey>();
sortKeys.add(new SortKey(2, SortOrder.ASCENDING));
sortKeys.add(new SortKey(1, SortOrder.ASCENDING));
// sorter.setSortKeys(sortKeys);
sorter.setSortable(0, true);
sorter.setSortable(1, false);
sorter.setSortable(2, true);
table.setRowSorter(sorter);
table.setDefaultRenderer(Object.class, new MyRenderer(table.getDefaultRenderer(Object.class)));
JTableHeader header = table.getTableHeader();
header.addMouseListener(new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseClicked(MouseEvent e) {
int col = ((JTableHeader)(e.getComponent())).getColumnModel().getColumnIndexAtX(e.getX());
}
});
JScrollPane sp = new JScrollPane(table);
sp.setBounds(0, 0, 200, 200);
add(sp, BorderLayout.CENTER);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
RowGroupInTable frame = new RowGroupInTable();
}
});
}
private class MyRenderer implements TableCellRenderer {
TableCellRenderer def;
public MyRenderer() {
// TODO Auto-generated constructor stub
}
public MyRenderer(TableCellRenderer defaultRend) {
this();
this.def = defaultRend;
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
int rowCount = table.getModel().getRowCount();
Component orig = (def).getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (column == 0) {
if (row == 0) {
return orig;
} else if (row > 0 && row < rowCount) {
if (table.getModel().getValueAt(row-1, column).equals(value)) {
return new JLabel("");
} else {
return orig;
}
}
}
return orig;
}
}
}
each name should be shown only once, the rest cells must be blank.
If I understand your requirement you might be able to use table.getValueAt(...) instead of table.getModel().getValueAt(...):
import java.awt.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;
public class RowGroupInTableTest {
private JComponent makeUI() {
String[] titles = new String[] {"Name", "Last job duration", "Book #"};
DefaultTableModel model = new DefaultTableModel(null, titles) {
#Override public Class<?> getColumnClass(int column) {
return MyData.class;
}
};
addMyData(model, new MyData("Tom", 17, "Book1"));
addMyData(model, new MyData("Tom", 23, "Book2"));
addMyData(model, new MyData("Tom", 25, "Book3"));
addMyData(model, new MyData("Polazzo", 41, "Book1"));
addMyData(model, new MyData("Polazzo", 45, "Book2"));
addMyData(model, new MyData("Polazzo", 12, "Book3"));
addMyData(model, new MyData("Anna", 1, "Book3"));
addMyData(model, new MyData("Anna", 33, "Book5"));
JTable table = new JTable(model);
table.setFillsViewportHeight(true);
table.setDefaultRenderer(MyData.class, new MyRenderer());
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
Comparator<MyData> c = Comparator.comparing(MyData::getName);
sorter.setComparator(0, c);
sorter.setComparator(1, c.thenComparing(Comparator.comparingInt(MyData::getDuration)));
sorter.setComparator(2, c.thenComparing(Comparator.comparing(MyData::getBook)));
table.setRowSorter(sorter);
return new JScrollPane(table);
}
private static void addMyData(DefaultTableModel model, MyData data) {
//Omission work...
model.addRow(Collections.nCopies(3, data).toArray());
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new RowGroupInTableTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class MyData {
private final String name;
private final int duration;
private final String book;
protected MyData(String name, int duration, String book) {
this.name = name;
this.duration = duration;
this.book = book;
}
public String getName() {
return name;
}
public int getDuration() {
return duration;
}
public String getBook() {
return book;
}
}
class MyRenderer implements TableCellRenderer {
TableCellRenderer def = new DefaultTableCellRenderer();
#Override public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
JLabel orig = (JLabel) def.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
orig.setHorizontalAlignment(SwingConstants.LEFT);
MyData data = (MyData) value;
switch (table.convertColumnIndexToModel(column)) {
case 0:
String str = data.getName();
if (row > 0) {
//if (table.getModel().getValueAt(row-1, column).equals(value)) {
//Since it compares with the value of the previous line on the display,
//table.getModel() is not needed
MyData prev = (MyData) table.getValueAt(row - 1, column);
if (Objects.equals(prev.getName(), str)) {
str = " ";
}
}
orig.setText(str);
break;
case 1:
orig.setHorizontalAlignment(SwingConstants.RIGHT);
orig.setText("" + data.getDuration());
break;
case 2:
orig.setText(data.getBook());
break;
default:
break;
}
return orig;
}
}
edit
Now if I only use Java 7, is there some "old" way to do this? Just setting the comparators in the Java 7 way?
You would need to impliment Comparator:
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
//Comparator<MyData> c = Comparator.comparing(MyData::getName);
//sorter.setComparator(0, c);
//sorter.setComparator(1, c.thenComparing(Comparator.comparingInt(MyData::getDuration)));
//sorter.setComparator(2, c.thenComparing(Comparator.comparing(MyData::getBook)));
sorter.setComparator(0, new MyDataGroupComparator(0));
sorter.setComparator(1, new MyDataGroupComparator(1));
sorter.setComparator(2, new MyDataGroupComparator(2));
table.setRowSorter(sorter);
class MyDataGroupComparator implements Comparator<MyData> {
private final int column;
protected MyDataGroupComparator(int column) {
this.column = column;
}
#Override public int compare(MyData a, MyData b) {
if (a == null && b == null) {
return 0;
} else if (a != null && b == null) {
return -1;
} else if (a == null && b != null) {
return 1;
} else {
int v = a.getName().compareTo(b.getName());
if (v == 0) {
switch (column) {
case 2:
return a.getBook().compareTo(b.getBook());
case 1:
return a.getDuration() - b.getDuration();
case 0:
default:
return v;
}
}
return v;
}
}
}
when I change table.getModel().getValueAt() to table.getValueAt() I cannot get my original example to work. Why?
Works fine for me(only the cell under Anna is blank):
hi i am new in java jtable cellrendered. I am looking for a way that works in my program but i dont have any luck finding it.
Here is my Jtable
Employee ID | Name | Status | Position
00565651 Roger Active Manager
00565652 Gina Active Crew
00565652 Alex Inactive Crew
00565652 Seph Active Manager
the data came from ms access database but i want to change the background/foreground of the rows which has a value of "inactive" in status column. I found many examples in the internet but all of it is not possible in my program. Can someone help me?
This is my model
String[] columnNames = {"Employee ID","Name", "Status", "Position"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
and this is the way to create my table and how i am fetching data from database
public MyList(){//my constructor
frame();
loadListFromDB();
}
public void frame(){//
//codes for frame setsize,titles etc...
tblList = new JTable();
tblList.getTableHeader().setPreferredSize(new Dimension(100, 40));
tblList.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 25));
tblList.setAutoCreateRowSorter(true);
tblList.setModel(model);
scrollPane.setViewportView(tblList);
loadListFromDB();
}
public void loadListFromDB(){
String sql = "SELECT emp_id,lname,fname,positional_status from tblEmployee";
try{
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()){
Vector row = new Vector();
for (int i = 1; i <= 4; i++){
row.addElement( rs.getObject(i) );
}
model.addRow(row);
}
}catch(Exception err){
//for error code
}
}
How am i suppose to add the tableredered in this way?Can anyone give simple example to change the color of row? Thanks in advance.. My program stop in this problem.
"i want to change the background/foreground of the rows which has a value of "inactive" in status column"
It's really just a matter of getting the value from table/model. if the status for that row is inactive, then set the background/foreground for every every cell in that row. Since the renderer is rendered for every cell, then basically you need to get the value of the [row][statusColumn], and that will be the status value for each row. Something like
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
#Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int col) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
String status = (String)table.getModel().getValueAt(row, STATUS_COL);
if ("active".equals(status)) {
setBackground(Color.BLACK);
setForeground(Color.WHITE);
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
});
Here's a simple example
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class TableRowDemo {
private static final int STATUS_COL = 1;
private static JTable getTable() {
final String[] cols = {"col 1", "status", "col 3"};
final String[][] data = {
{"data", "active", "data"},
{"data", "inactive", "data"},
{"data", "inactive", "data"},
{"data", "active", "data"}
};
DefaultTableModel model = new DefaultTableModel(data, cols);
return new JTable(model) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(350, 150);
}
};
}
private static JTable getNewRenderedTable(final JTable table) {
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer(){
#Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row, int col) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
String status = (String)table.getModel().getValueAt(row, STATUS_COL);
if ("active".equals(status)) {
setBackground(Color.BLACK);
setForeground(Color.WHITE);
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
return this;
}
});
return table;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JOptionPane.showMessageDialog(null, new JScrollPane(getNewRenderedTable(getTable())));
}
});
}
}
Another option is to #Override prepareRenderer of the table. It will give you the same result.
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class TableRowDemo {
private static final int STATUS_COL = 1;
private static JTable getTable() {
final String[] cols = {"col 1", "status", "col 3"};
final String[][] data = {
{"data", "active", "data"},
{"data", "inactive", "data"},
{"data", "inactive", "data"},
{"data", "active", "data"}
};
DefaultTableModel model = new DefaultTableModel(data, cols);
return new JTable(model) {
#Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(350, 150);
}
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component c = super.prepareRenderer(renderer, row, col);
String status = (String)getValueAt(row, STATUS_COL);
if ("active".equals(status)) {
c.setBackground(Color.BLACK);
c.setForeground(Color.WHITE);
} else {
c.setBackground(super.getBackground());
c.setForeground(super.getForeground());
}
return c;
}
};
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
JOptionPane.showMessageDialog(null, new JScrollPane(getTable()));
}
});
}
}
Imagine I'm building an IRC client with Java and I'd like rich text in the chat view to show IRC colors and colored nicks. I'd like to build this with a JTable. I can do that, but the text is then not selectable. Making the table editable doesn't make sense.
I've also investigated:
TextArea - no rich text formatting
JEditPane - can't append, only replace which is bad performance wise
JList - can't select text
So I got a table working I just need the text to be selectable without making it editable. I'd also would only like the text contents, and none of the HTML to be copied into the clipboard upon copying the text selection.
I have tried various iterations of setRowSelectionAllowed(), setColumnSelectionEnabled() and setCellSelectionEnabled() and setSelectionMode the table model returns false for isCellEditable(). Nothing has made the text selectable.
EDIT: as per answer 1 I was wrong about text editor panes so I'm trying those solutions.
I don't know why you don't want to use a JTextPane or JEditorPane. You insert text by its document. Examples here --> How to use Editor Panes and Text Panes.
But for your purpose you can for example do something like this. I override changeSelection to selectAll text when is clicking, the cells are editable but its cellEditors are not editable.
public class JTableTest {
private final DefaultCellEditor cellEditor;
private final JTextField textfield;
private JPanel panel;
private MyTableModel tableModel = new MyTableModel();
private JTable table = new JTable() {
#Override
public TableCellEditor getCellEditor(int row, int column) {
return JTableTest.this.cellEditor;
}
#Override
public void changeSelection(
final int row, final int column, final boolean toggle, final boolean extend) {
super.changeSelection(row, column, toggle, extend);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
if ((getCellEditor(row, column) != null && !editCellAt(row, column))) {
JTextField textfield=(JTextField)JTableTest.this.cellEditor.getComponent();
textfield.selectAll();
}
}
});
}
};
public JTableTest() {
JScrollPane scroll = new JScrollPane(table);
table.setModel(tableModel);
panel = new JPanel(new BorderLayout());
panel.add(scroll, BorderLayout.CENTER);
textfield = new JTextField();
textfield.setEditable(Boolean.FALSE);
textfield.setBorder(null);
cellEditor = new DefaultCellEditor(textfield);
tableModel.insertValue(new ItemRow("nonEditable", "Editable"));
}
private class ItemRow {
private String column1;
private String column2;
public ItemRow(String column1, String column2) {
this.column1 = column1;
this.column2 = column2;
}
public String getColumn1() {
return column1;
}
public void setColumn1(String column1) {
this.column1 = column1;
}
public String getColumn2() {
return column2;
}
public void setColumn2(String column2) {
this.column2 = column2;
}
}
private class MyTableModel extends AbstractTableModel {
public static final int COLUMN1_INDEX = 0;
public static final int COLUMN2_INDEX = 1;
private final List<ItemRow> data = new ArrayList<>();
private final String[] columnsNames = {
"Column1",
"Column2",};
private final Class<?>[] columnsTypes = {
String.class,
String.class
};
public MyTableModel() {
super();
}
#Override
public Object getValueAt(int inRow, int inCol) {
ItemRow row = data.get(inRow);
Object outReturn = null;
switch (inCol) {
case COLUMN1_INDEX:
outReturn = row.getColumn1();
break;
case COLUMN2_INDEX:
outReturn = row.getColumn2();
break;
default:
throw new RuntimeException("invalid column");
}
return outReturn;
}
#Override
public void setValueAt(Object inValue, int inRow, int inCol) {
System.out.println("Gets called ");
if (inRow < 0 || inCol < 0 || inRow >= data.size()) {
return;
}
ItemRow row = data.get(inRow);
switch (inCol) {
case COLUMN1_INDEX:
row.setColumn1(inValue.toString());
break;
case COLUMN2_INDEX:
row.setColumn2(inValue.toString());
break;
}
fireTableCellUpdated(inRow, inCol);
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public int getColumnCount() {
return columnsTypes.length;
}
#Override
public String getColumnName(int inCol) {
return this.columnsNames[inCol];
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return this.columnsTypes[columnIndex];
}
/**
*
* #param row
*/
public void insertValue(ItemRow row) {
data.add(row);
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
}
private static void createAndShowGUI(final Container container, final String title) {
//Create and set up the window.
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(Boolean.TRUE);
frame.add(container);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI(new JTableTest().panel, "Test");
}
});
}
}
I accomplished this by enabling the editing and then making the component responsible for the edition ignore any changes. For this I created a TableCellEditor and intercepted the key types to the JTextField, the component used for editing.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
public class TableCellSelectionTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new TableCellSelectionTest().initUI();
}
});
}
public void initUI()
{
JFrame frame = new JFrame();
int N = 5;
int M = 3;
Object[][] data = new Object[N][M];
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < M; ++j)
{
data[i][j] = "This is the cell (" + i + ", " + j +")";
}
}
String[] columnNames = { "Column 1", "Column 2", "Column 3" };
DefaultTableModel model = new DefaultTableModel(data, columnNames);
final MyTableCellEditor editor = new MyTableCellEditor();
JTable table = new JTable(model) {
#Override
public TableCellEditor getCellEditor(int row, int column)
{
return editor;
}
};
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
class MyTableCellEditor extends AbstractCellEditor implements
TableCellEditor
{
Object _value;
#Override
public Object getCellEditorValue()
{
return _value;
}
#Override
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column)
{
_value = value;
JTextField textField = new JTextField(_value.toString());
textField.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e) {
e.consume(); //ignores the key
}
#Override
public void keyPressed(KeyEvent e)
{
e.consume();
}});
textField.setEditable(false); //this is functionally irrelevent, makes slight visual changes
return textField;
}
}
}
I tried both the answers here... but one problem at least is that you can tell when you've entered the "editing" mode.
This might be of interest... uses a combination of Editor magic and cheeky rendering to make it look like no editing is going on: editor's click-count-to-start is set to 1, and the component (JTextPane) delivered by the editor's method does setEditable( false ).
If this tickles your fancy, you might be interested at looking at my implementation of a JTable which adjusts (perfectly, harnessing the JTextPane's powerful wrapping power) the row height to the text, for individual rows, including when you change the columns: How to wrap lines in a jtable cell?
public class SelectableNonEditableTableTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame main_frame = new JFrame();
main_frame.setPreferredSize(new Dimension(1200, 300));
main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ArrayList<String> nonsense = new ArrayList<String>(
Arrays.asList(
"Lorem ipsum dolor sit amet, sed dolore vivendum ut",
"pri an soleat causae doctus.",
"Alienum abhorreant mea ea",
"cum malorum diceret ei. Pri oratio invidunt consequat ne.",
"Ius tritani detraxit scribentur et",
"has detraxit legendos intellegat at",
"quo oporteat constituam ex"));
JTable example_table = new JTable(10, 4);
example_table.setRowHeight( example_table.getRowHeight() * 2 );
DefaultCellEditor cell_editor = new SelectableNonEditableCellEditor(
new JTextField());
cell_editor.setClickCountToStart(1);
example_table.setDefaultEditor(Object.class, cell_editor);
TableCellRenderer renderer = new SelectableNonEditableTableRenderer();
example_table.setDefaultRenderer(Object.class, renderer);
for (int i = 0; i < 10; i++) {
example_table.setValueAt(nonsense.get(i % nonsense.size()),
i, i % 4);
}
main_frame.getContentPane().add(new JScrollPane(example_table));
main_frame.pack();
main_frame.setVisible(true);
}
});
}
}
class SelectableNonEditableCellEditor extends DefaultCellEditor {
public SelectableNonEditableCellEditor(JTextField textField) {
super(textField);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int col) {
Component comp = super.getTableCellEditorComponent(table, value,
isSelected, row, col);
if (value instanceof java.lang.String) {
DefaultStyledDocument sty_doc = new DefaultStyledDocument();
try {
sty_doc.insertString(0, (String) value, null);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JTextPane jtp_comp = new JTextPane(sty_doc);
jtp_comp.setEditable(false);
return jtp_comp;
}
return comp;
}
}
class SelectableNonEditableTableRenderer extends JTextPane implements
TableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof DefaultStyledDocument) {
setDocument((DefaultStyledDocument) value);
} else {
setText((String) value);
}
return this;
}
}
Maybe you can implement your own TableCellRenderer that extends JTextField in your table.
I have a question for JTable.
When I select a cell and then there are same value cell in JTable which I chose, that cells highlight background red color.
I don't know what to do....
P.S: I am sorry, I do not know how to enter the code in here...;;
You can implement ListSelectionListener to track selection changes in a table. Then implement TableCellRenderer that would change background of a cell with the same value of a selected object. Check out How to Use Tables for more details on JTable, renderers and selection.
Here is a very simple example that demonstrates this idea:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.Component;
public class TableDemo {
private static void createAndShowGUI() {
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPanel = new JPanel();
String[] columnNames = { "Column1", "Column2" };
Object[][] data = { { "1", "3" }, { "2", "5" }, { "7", "1" },
{ "5", "3" } };
JTable table = new JTable();
MyModel model = new MyModel(Color.RED, table.getBackground());
model.setDataVector(data, columnNames);
table.setModel(model);
table.setColumnSelectionAllowed(true);
table.setDefaultRenderer(Object.class, new TestCellRenderer());
SelectionListener listener = new SelectionListener(table);
table.getSelectionModel().addListSelectionListener(listener);
table.getColumnModel().getSelectionModel()
.addListSelectionListener(listener);
JScrollPane scrollPane = new JScrollPane(table);
contentPanel.add(scrollPane);
contentPanel.setOpaque(true);
frame.add(contentPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
static class TestCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
MyModel model = (MyModel) table.getModel();
c.setBackground(model.getCellColor(row, column));
return c;
}
}
static class MyModel extends DefaultTableModel {
private Object selectedObject;
private Color selectedColor;
private Color normalColor;
public MyModel(Color selectedColor, Color normalColor) {
this.selectedColor = selectedColor;
this.normalColor = normalColor;
}
public Color getCellColor(int row, int column) {
if (getValueAt(row, column).equals(selectedObject))
return selectedColor;
return normalColor;
}
public void setSelectedObject(Object selectedObject) {
this.selectedObject = selectedObject;
fireTableRowsUpdated(0, getRowCount());
}
}
static class SelectionListener implements ListSelectionListener {
private JTable table;
SelectionListener(JTable table) {
this.table = table;
}
public void valueChanged(ListSelectionEvent e) {
int rowIndex = table.getSelectedRow();
int colIndex = table.getSelectedColumn();
if (!e.getValueIsAdjusting() && colIndex != -1 && rowIndex != -1) {
((MyModel) table.getModel()).setSelectedObject(table
.getValueAt(rowIndex, colIndex));
}
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Here is a result:
EDIT: solution using renderer only, without table model
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.Component;
public class TableDemo {
private static void createAndShowGUI() {
JFrame frame = new JFrame("TableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPanel = new JPanel();
String[] columnNames = { "Column1", "Column2" };
Object[][] data = { { "1", "3" }, { "2", "5" }, { "7", "1" },
{ "5", "3" } };
JTable table = new JTable(new DefaultTableModel(data, columnNames));
table.setColumnSelectionAllowed(true);
TestCellRenderer renderer = new TestCellRenderer();
table.setDefaultRenderer(Object.class, renderer);
SelectionListener listener = new SelectionListener(table);
table.getSelectionModel().addListSelectionListener(listener);
table.getColumnModel().getSelectionModel()
.addListSelectionListener(listener);
JScrollPane scrollPane = new JScrollPane(table);
contentPanel.add(scrollPane);
contentPanel.setOpaque(true);
frame.add(contentPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
static class TestCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
int columnIndex = table.getSelectedColumn();
int rowIndex = table.getSelectedRow();
if (columnIndex != -1 && rowIndex != -1){
Object selectedValue = table.getValueAt(rowIndex, columnIndex);
if (value.equals(selectedValue)) {
c.setBackground(Color.RED);
} else {
c.setBackground(table.getBackground());
}
}
return c;
}
}
static class SelectionListener implements ListSelectionListener {
private JTable table;
SelectionListener(JTable table) {
this.table = table;
}
public void valueChanged(ListSelectionEvent e) {
int rowIndex = table.getSelectedRow();
int colIndex = table.getSelectedColumn();
if (!e.getValueIsAdjusting() && colIndex != -1 && rowIndex != -1){
table.repaint();
}
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}