Background color flickers using JCheckBox in JTable - java

On my JTable, I am setting the background colors of the rows using the prepareRenderer function. I also removed the focus border and later in the code I disable the row selection.
JTable table = new JTable() {
private static final long serialVersionUID = -2965586838134675413L;
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
c.setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248));
((JComponent) c).setBorder(null);
return c;
}
};
DefaultTableModel tableModel = new DefaultTableModel(sampleData, headers) {
private static final long serialVersionUID = 9186050244728809804L;
#Override
public Class<?> getColumnClass(int columnIndex) {
switch(columnIndex) {
case 0:
return Boolean.class;
default:
return String.class;
}
}
#Override
public boolean isCellEditable(int row, int column) {
switch(column) {
case 0:
return true;
default:
return false;
}
}
#SuppressWarnings("unchecked")
#Override
public void setValueAt(Object aValue, int row, int column) {
if (aValue instanceof Boolean && column == 0) {
//System.out.println(aValue);
Vector<Boolean> rowData = (Vector<Boolean>) getDataVector().get(row);
rowData.set(0, (boolean) aValue);
fireTableCellUpdated(row, column);
}
}
};
table.setModel(tableModel);
table.setRowSelectionAllowed(false);
The problem is when I click on the JCheckBoxes in the background color switches to white while the mouse button pressed down. Is there a way keep to keep the color from changing whilst clicking on the JCheckBox?
Note
Using the default boolean renderer to achieve this still has the same problem but it does not happen as often
private class BooleanRenderer extends JCheckBox implements TableCellRenderer
{
private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
public BooleanRenderer() {
super();
setHorizontalAlignment(JLabel.CENTER);
setBorderPainted(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelected((value != null && ((Boolean)value).booleanValue()));
setBorder(noFocusBorder);
return this;
}
}
table.setDefaultRenderer(Boolean.class, new BooleanRenderer())
Note 2
I also tried adding a MouseListener to the JCheckBox but that did not work as the mouse listener did not seem to attach to the JCheckBox
JTable fileList = new JTable() {
private static final long serialVersionUID = -2965586838134675413L;
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
c.setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248));
((JComponent) c).setBorder(null);
if(c instanceof JCheckBox) {
c.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248));
}
public void mousePressed(MouseEvent e) {
setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248));
}
public void mouseReleased(MouseEvent e) {
setBackground(row % 2 == 1 ? Color.WHITE : new Color(255, 245, 248));
}
});
}
return c;
}
};
Edit - SSSCE using first example of code
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class JTableTest extends JFrame {
private JTableTest() {
super("Button Layout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 1));
createPanel();
pack();
setVisible(true);
}
JPanel panel = new JPanel(new GridLayout(1, 1));
JScrollPane scroll = new JScrollPane();
private void createPanel() {
Object[] headers = {"Select", "Title", "Artist", "Length"};
Object[][] sampleData = {{true, "Bat Outta Hell", "Meat Loaf", "673"},
{false, "Spanish Train", "Chris De Burgh", "358"}};
JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
c.setBackground(row % 2 == 1 ? Color.WHITE : Color.RED);
((JComponent) c).setBorder(null); //Left in so it is easier to see problem
return c;
}
};
DefaultTableModel tableModel = new DefaultTableModel(sampleData, headers) {
#Override
public Class<?> getColumnClass(int columnIndex) {
switch(columnIndex) {
case 0:
return Boolean.class;
default:
return String.class;
}
}
};
table.setModel(tableModel);
table.setRowSelectionAllowed(false); //Left in so it is easier to see problem
scroll.getViewport().add(table);
panel.add(scroll);
getContentPane().add(panel);
}
public static void main(String[] args) {
new JTableTest();
}
}

You are missing a prepareEditor override in your JTable instance, I added one in your sample. When you click on the checkbox, what is displayed is the editor component rather than the renderer component. You need to prepare that component as well to get the coloring you want.
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
#SuppressWarnings("serial")
public class JTableTest extends JFrame {
private JTableTest() {
super("Button Layout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1, 1));
createPanel();
pack();
setVisible(true);
}
JPanel panel = new JPanel(new GridLayout(1, 1));
JScrollPane scroll = new JScrollPane();
private void createPanel() {
Object[] headers = {"Select", "Title", "Artist", "Length"};
Object[][] sampleData = {{true, "Bat Outta Hell", "Meat Loaf", "673"},
{false, "Spanish Train", "Chris De Burgh", "358"}};
JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
c.setBackground(row % 2 == 1 ? Color.WHITE : Color.RED);
((JComponent) c).setBorder(null); //Left in so it is easier to see problem
return c;
}
#Override
public Component prepareEditor(TableCellEditor editor, int row, int column) {
Component c = super.prepareEditor(editor, row, column);
c.setBackground(row % 2 == 1 ? Color.WHITE : Color.RED);
((JComponent) c).setBorder(null); //Left in so it is easier to see problem
return c;
}
};
DefaultTableModel tableModel = new DefaultTableModel(sampleData, headers) {
#Override
public Class<?> getColumnClass(int columnIndex) {
switch(columnIndex) {
case 0:
return Boolean.class;
default:
return String.class;
}
}
};
table.setModel(tableModel);
table.setRowSelectionAllowed(false); //Left in so it is easier to see problem
scroll.getViewport().add(table);
panel.add(scroll);
getContentPane().add(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater (
new Runnable() {
#Override
public void run() {
new JTableTest();
}
}
);
}
}

Related

Multiple focus events from mouse click in JTable

When I click on a table cell, and then click on a second table cell, a number of mouse and focus events occur that I do not understand. For example, clicking of cell (1, 0) and then on cell (2, 1) and then the Done button to display the sequence of events causes the following events:
1) Mouse Pressed on cell (1,0)
2) Focus Gained on cell (1,0)
3) Mouse Pressed on cell (1,0) - why(?)
4) Mouse Pressed on cell (2,1)
5) Focus Lost on cell (1,0) - why(?)
6) Focus Lost on cell (2,1)
7) Focus Gained on cell (1,0) - why(?)
8) Focus Gained on cell (2,1) - why(?)
9) Focus Lost on cell (1,0) - why(?)
10) Focus lost on cell (2,1) - why(?)
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.*;
import static javax.swing.SwingConstants.CENTER;
import static javax.swing.SwingConstants.LEFT;
import javax.swing.SwingUtilities;
import javax.swing.table.*;
public class TestFocus {
public ArrayList<String> mylog;
public int number = 0;
public TestFocus() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = createPanel();
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public JPanel createPanel() {
mylog = new ArrayList<>();
JPanel panel = new JPanel();
TestTableModel tm = new TestTableModel();
JLabel title = new JLabel("Test Table");
JTable table = new JTable(tm);
TableColumnModel tcm = table.getColumnModel();
TestTableCellEditor editor = new TestTableCellEditor();
TestTableCellRenderer renderer = new TestTableCellRenderer();
for (int i = 0; i < tm.getColumnCount(); i++) {
TableColumn column = tcm.getColumn(i);
column.setCellEditor(editor);
column.setCellRenderer(renderer);
}
JScrollPane jsp = new JScrollPane(table);
JButton btn = new JButton("Done");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
for (String s : mylog) {
System.out.println(s);
}
}
});
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(jsp);
panel.add(btn);
return panel;
}
class TestTableModel extends AbstractTableModel {
private String[] columnNames = { "Firstname", "Lastname", "Age" };
private Object[][] data = {
{ "John", "Smith", 29},
{ "Mary", "Thomas", 63},
{ "Peter", "Jones", 48} };
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
public String getColumnClassName(int col) {
if (col == 2) {
return "Integer";
} else {
return "String";
}
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
public class TestTableCellEditor extends AbstractCellEditor
implements TableCellEditor {
JComponent component = new JTextField();
#Override
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
((JTextField)component).addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent me) {
mylog.add(++number + ") Mouse pressed: " +
value.toString() + ": r/c ("+row+"/"+column+")");
}
});
((JTextField)component).addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent fe) {
mylog.add(++number + ") Focus gained: " +
value.toString() + ": r/c ("+row+"/"+column+")");
}
#Override
public void focusLost(FocusEvent fe) {
mylog.add(++number + ") FocusLost: " +
value.toString() + ": r/c ("+row+"/"+column+")");
}
});
if (value != null) {
((JTextField)component).setText(value.toString());
} else {
((JTextField)component).setText("");
}
return (JTextField)component;
}
#Override
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
public class TestTableCellRenderer extends JLabel implements
TableCellRenderer {
public TestTableCellRenderer() {
this.setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, final int row, int column) {
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
Component c = renderer.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (hasFocus) {
c.setBackground(Color.yellow);
}
TestTableModel tm = (TestTableModel)table.getModel();
int col = table.convertColumnIndexToModel(column);
String colname = tm.getColumnName(col);
String type = tm.getColumnClassName(col);
if (type.equals("Integer") || type.equals("Int")) {
((JLabel)c).setHorizontalAlignment(CENTER);
} else { // add padding
((JLabel)c).setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
((JLabel)c).setHorizontalAlignment(LEFT);
}
if (type.equals("String")) {
String text = ((JLabel)c).getText();
((JLabel)c).setToolTipText(text);
}
return c;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestFocus();
}
});
}
}
getTableCellEditorComponent method will be called very time the JTable gets rendered - a lot of times. Inside it you textfield.addMouseListener(). That means the listener will be added a lot of times. That's why you get many events instead of one (all these listeners are notified). In order to solve it, add the listeners only one time. You can add the listeners in constructor of this class.
For example:
public class TestTableCellEditor extends AbstractCellEditor implements TableCellEditor {
private JTextField component;
public TestTableCellEditor() {
component = new JTextField();
component.addMouseListener(mouseListener);
component.addFocusListener(focusListener)
}
#Override
public Object getCellEditorValue() {
return component.getText();
}
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
component.setText(value == null ? "" : String.valueOf(value));
return component;
}
}

New line in jTable cell

This is how I am adding rows to my jTable, as you can see I tried using \n in zainteresowania but it is being ignored. How can I simple add new line after every record from my List?
for (Osoba a : osoby){
List zainteresowania1 = a.getZainteresowania();
for (Iterator iterator2 = zainteresowania1.iterator(); iterator2.hasNext();){
Zainteresowania nazwa = (Zainteresowania) iterator2.next();
zainteresowania = zainteresowania + (nazwa.getZainteresowanie() + ". " + "\n");
}
model.addRow(new Object[]{a.getImie(), a.getNazwisko(), a.getEmail(), a.getTelefon(), zainteresowania});
zainteresowania = "";
}
Refer Example
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.UIManager;
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;
public class TestJTableMultiline extends JFrame {
public TestJTableMultiline() {
super("Multi-Line Cell Example");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
DefaultTableModel dm = new DefaultTableModel() {
public Class<String> getColumnClass(int columnIndex) {
return String.class;
}
public boolean isCellEditable(int row, int column) {
return false;
}
};
dm.setDataVector(
new Object[][]{
{"A0, Line1\nA0, Line2\nA0, Line3",
"B0, Line1\nB0, Line2",
"C0, Line1"},
{"A1, Line1",
"B1, Line1\nB1, Line2",
"C1, Line1"},
{"A2, Line1",
"B2, Line1",
"C2, Line1"}
},
new Object[] {"A", "B", "C"});
JTable table = new JTable(dm);
table.setDefaultRenderer(String.class, new MultiLineTableCellRenderer());
TableRowSorter<? extends TableModel> sort = new TableRowSorter<DefaultTableModel>(dm);
table.setRowSorter(sort);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add(scroll);
setLocationByPlatform(true);
setSize(400, 430);
setVisible(true);
}
public static void main(String[] args) {
TestJTableMultiline frame = new TestJTableMultiline();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
public class MultiLineTableCellRenderer extends JTextArea
implements TableCellRenderer {
private List<List<Integer>> rowColHeight = new ArrayList<List<Integer>>();
public MultiLineTableCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
setOpaque(true);
}
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setFont(table.getFont());
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
if (table.isCellEditable(row, column)) {
setForeground(UIManager.getColor("Table.focusCellForeground"));
setBackground(UIManager.getColor("Table.focusCellBackground"));
}
} else {
setBorder(new EmptyBorder(1, 2, 1, 2));
}
if (value != null) {
setText(value.toString());
} else {
setText("");
}
adjustRowHeight(table, row, column);
return this;
}
/**
* Calculate the new preferred height for a given row, and sets the height on the table.
*/
private void adjustRowHeight(JTable table, int row, int column) {
//The trick to get this to work properly is to set the width of the column to the
//textarea. The reason for this is that getPreferredSize(), without a width tries
//to place all the text in one line. By setting the size with the with of the column,
//getPreferredSize() returnes the proper height which the row should have in
//order to make room for the text.
int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth();
setSize(new Dimension(cWidth, 1000));
int prefH = getPreferredSize().height;
while (rowColHeight.size() <= row) {
rowColHeight.add(new ArrayList<Integer>(column));
}
List<Integer> colHeights = rowColHeight.get(row);
while (colHeights.size() <= column) {
colHeights.add(0);
}
colHeights.set(column, prefH);
int maxH = prefH;
for (Integer colHeight : colHeights) {
if (colHeight > maxH) {
maxH = colHeight;
}
}
if (table.getRowHeight(row) != maxH) {
table.setRowHeight(row, maxH);
}
}
}

How to color rows of JTable according to particular value of attribute

I have JTable, and I want the result in the table to have rows colored according to particular value of attribute; comp is null here:
private JNI18NTable _issueIncidentTable = new JNI18NTable(I18N_ID, COLUMNS, "issue.table.") {
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component comp = super.prepareRenderer(renderer, row, col);
int RangeIndex = Utilities.rowIndexToRangeIndex(_resultIter, row);
Row theRow = _resultIter.getRowAtRangeIndex(RangeIndex);
System.out.println("the row is" + theRow.getAttribute("Type"));
boolean markRow = theRow != null && theRow.getAttribute("Type").toString().equals("c");
boolean isSelected = isRowSelected(row);
System.out.println("the comp" + comp);
if (markRow) {
comp.setForeground(Color.white);
comp.setBackground(isRowSelected(row) ? Color.red.darker() : Color.red);
} else {
comp.setForeground(isSelected ? getSelectionForeground() : getForeground());
comp.setBackground(isSelected ? getSelectionBackground() : getBackground());
}
return comp;
}
};
Extrapolating an sscce, your implementation of prepareRenderer() seems to work as shown below. You might look elsewhere in your code for the presumed problem. In particular, the implementation of Utilities.rowIndexToRangeIndex() should probably use the table's own convert method. Moreover, use the value of isSelected consistently.
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
/**
* #see http://stackoverflow.com/a/20684058/230513
*/
public class Test {
private void display() {
JFrame f = new JFrame("Test");
JTable table = new JTable(new DefaultTableModel(10, 1) {
#Override
public Object getValueAt(int row, int col) {
return "Row " + row;
}
}) {
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component comp = super.prepareRenderer(renderer, row, col);
boolean isSelected = isRowSelected(row);
if (row % 2 == 0) {
comp.setForeground(Color.white);
comp.setBackground(isSelected ? Color.red.darker() : Color.red);
} else {
comp.setForeground(isSelected ? getSelectionForeground() : getForeground());
comp.setBackground(isSelected ? getSelectionBackground() : getBackground());
}
return comp;
}
};
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(table));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Test().display();
}
});
}
}

JTable cell renderer skips Boolean column

Below is a picture of my JTable. My problem is that the blue background every other row isn't present in the 4th column. My table model and cell renderer are also below.
Table model:
public class MyTableModel extends DefaultTableModel {
public int getColumnCount() {
return columnNames.length;
}
public String getColumnName(int column) {
return columnNames[column];
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int row, int column) {
return data[row][column];
}
public Class getColumnClass(int column) {
return (getValueAt(0, column).getClass());
}
public void setValueAt(Object value, int row, int column) {
data[row][column] = value;
}
public boolean isCellEditable(int row, int column) {
return (column == 3);
}
}
Cell Renderer:
private class CalendarRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focused, int row, int column) {
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
setBackground(new Color(0xFFFFFF));
if (row % 2 == 1) {
setBackground(new Color(0xE8F2FE)); //light blue
}
setBorder(null);
setForeground(Color.black);
return this;
}
}
The "default" cell renderer for Boolean is a check box. If you want to change the way that this cell gets rendered, you're going to have to supply your own TableCellRenderer that implements your own logic.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
public class TestTableCellRenderer {
public static void main(String[] args) {
new TestTableCellRenderer();
}
public TestTableCellRenderer() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTable table = new JTable();
table.setModel(new TableModel());
table.setDefaultRenderer(Boolean.class, new BooleanCellRenderer());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TableModel extends AbstractTableModel {
private List<Boolean> values = new ArrayList<>(25);
public TableModel() {
for (int index = 0; index < 25; index++) {
values.add((((int) Math.round(Math.random() * 1)) == 0 ? false : true));
}
}
#Override
public int getRowCount() {
return values.size();
}
#Override
public int getColumnCount() {
return 1;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return values.get(rowIndex);
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return Boolean.class;
}
}
public static class BooleanCellRenderer extends JCheckBox implements TableCellRenderer {
private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
public BooleanCellRenderer() {
setLayout(new GridBagLayout());
setMargin(new Insets(0, 0, 0, 0));
setHorizontalAlignment(JLabel.CENTER);
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Boolean) {
setSelected((Boolean) value);
}
if (!isSelected) {
setBackground(new Color(0xFFFFFF));
if (row % 2 == 1) {
setBackground(new Color(0xE8F2FE)); //light blue
}
setForeground(Color.black);
} else {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
setBorder(noFocusBorder);
}
return this;
}
}
}
See Table Row Rendering for a solution that doesn't require you to keep creating custom renderers every time you have columns with different data.
JTable table = new JTable( model )
{
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
// Alternate row color
if (!isRowSelected(row))
c.setBackground(row % 2 == 0 ? getBackground() : Color.LIGHT_GRAY);
return c;
}
};

custom cell renderer for particular row and column

puuuuuuf, I'm starting to like swing :) I'm trying to write a cellRenderer to customy render all cells besides those which in first row and column. So I wrote the following:
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
if(row >0&&column>0){
cell.setBackground(Color.GREEN);
}
return cell;
}
}
and set the renderer as following:
scheduleTable.setDefaultRenderer(Object.class, new CustomTableCellRenderer());
but for some reason such an approach applies renderer to all the cell. So all of them are Green. If I'm doing something wrong, could you help me with that please?
Thanks in advance!
ADDITION
scheduleTable = new JTable() {
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component comp = super.prepareRenderer(renderer, row, column);
int modelRow = convertRowIndexToModel(row);
int modelColumn = convertColumnIndexToModel(column);
if (modelColumn != 0 && modelRow != 0) {
comp.setBackground(Color.GREEN);
}
return comp;
}
};
this code makes all the table green as well.
This code:
scheduleTable = new JTable(tableModel) {
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component comp = super.prepareRenderer(renderer, row, column);
int modelRow = convertRowIndexToModel(row);
int modelColumn = convertColumnIndexToModel(column);
if (modelRow != 0 && modelColumn != 0) {
setBackground(Color.GREEN);
} else {
setBackground(Color.WHITE);
}
return comp;
}
};
gives me the following result ;(
The following situations I have with differents n in expression row != 0 && column != 0:
you can use prepareRenderer, is easiest and more confortable that XxxCellRenderer
great code example is Table Row Rendering by #camickr
EDIT:
if (modelColumn != 0 || modelRow != 0) {
and with if (modelColumn != 0 && modelRow != 0) {
from code
import java.awt.*;
import java.awt.font.TextAttribute;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.*;
public class TablePrepareRenderer extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
public TablePrepareRenderer() {
Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
Object[][] data = {
{"Buy", "IBM", new Integer(1000), new Double(80.50), false},
{"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
{"Sell", "Apple", new Integer(3000), new Double(7.35), true},
{"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table = new JTable(model) {
private static final long serialVersionUID = 1L;
private Border outside = new MatteBorder(1, 0, 1, 0, Color.red);
private Border inside = new EmptyBorder(0, 1, 0, 1);
private Border highlight = new CompoundBorder(outside, inside);
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
int modelRow = convertRowIndexToModel(row);
int modelColumn = convertColumnIndexToModel(column);
if (!isRowSelected(modelRow)) {
if (modelColumn != 0 || modelRow != 0) {
comp.setBackground(Color.GREEN);
} else {
comp.setBackground(table.getBackground());
}
}
return comp;
/*Component comp = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent) comp;
Map attributes = (new Font("Serif", Font.PLAIN, 12)).getAttributes();
//attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
if (!isRowSelected(row)) {
comp.setForeground(Color.black);
comp.setBackground(row % 2 == 0 ? Color.white : Color.orange);
int modelRow = convertRowIndexToModel(row);
String type = (String) getModel().getValueAt(modelRow, 0);
if (type.equals("Sell")) {
comp.setFont(new Font(attributes));
comp.setForeground(Color.red);
} else {
comp.setFont(new Font("Serif", Font.BOLD, 12));
}
} else {
comp.setFont(table.getFont());
}
jc.setBorder(BorderFactory.createCompoundBorder(jc.getBorder(), BorderFactory.createEmptyBorder(0, 0, 0, 5)));
return comp;*/
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TablePrepareRenderer frame = new TablePrepareRenderer();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Use the row and column value without converting into model

Categories