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
Related
I know there are previous topics for a similar problem but even with it I wasn't able to resolve my problem.
So the thing is, when I add a new empty row to my JTable I want to highlight it in gray. So it works for the first one but when I try to add a second row, the second will be white...
I tried to debbug but without success, I didn't find my mistake.
Here is the class to highlight a row :
class GrayWhiteRenderer extends DefaultTableCellRenderer {
private int rowToColored;
GrayWhiteRenderer(int rowToColored) {
this.rowToColored = rowToColored;
Color color = UIManager.getColor ( "table.row" );
}
#Override
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);
if (UIManager.getColor ( "table.row" )==Color.GRAY) {
c.setBackground(Color.GRAY.brighter());
}
else if(row == rowToColored) {
c.setBackground(Color.GRAY.brighter());
} else {
c.setBackground(Color.WHITE);
}
return c;
}
}
I store the index of all new row in rowToAdd at the begining of it (for exemple : [19, 20, 21, -1, -1, ...]. -1 is to know when I have to stop looking if I have to highlight more) :
while(rowToAdd[k]!=-1) {
System.out.println("rowToAdd[k] : "+rowToAdd[k]);
dataTable.setDefaultRenderer(Object.class, new GrayWhiteRenderer(rowToAdd[k]));
k++;
}
And here is a class to test it :
package Graphic;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
class Test implements Runnable, ActionListener {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Test());
}
JTable table;
#Override
public void run() {
JFrame frame = new JFrame("Custom Cell Renderer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
table = new JTable(new DefaultTableModel(0, 2) {
#Override
public Class<?> getColumnClass(int c) {
return Object.class;
}
});
class GrayWhiteRenderer extends DefaultTableCellRenderer {
private int rowToColored;
GrayWhiteRenderer(int rowToColored) {
this.rowToColored = rowToColored;
}
#Override
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);
if (UIManager.getColor ( "table.row" )==Color.GRAY) {
c.setBackground(Color.GRAY.brighter());
}
else if(row == rowToColored) {
c.setBackground(Color.GRAY.brighter());
} else {
c.setBackground(Color.WHITE);
}
return c;
}
}
table.setDefaultRenderer(Object.class, new GrayWhiteRenderer(table.getRowCount()));
table.setTableHeader(null);
JButton btn = new JButton("Add Row");
btn.addActionListener(this);
JToolBar bar = new JToolBar();
bar.setFloatable(false);
bar.add(btn);
JPanel content = new JPanel(new BorderLayout());
content.add(bar, BorderLayout.NORTH);
content.add(new JScrollPane(table), BorderLayout.CENTER);
frame.setContentPane(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent ae) {
int nextRow = table.getRowCount();
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.addRow(new Object[] { "" + nextRow, "" + nextRow });
for(int i=0; i<model.getColumnCount(); i++) {
table.setDefaultRenderer(Object.class, new GrayWhiteRenderer(i));
}
}
}
So, I've fixed your example. Here is your code with my comments
#Override
public void actionPerformed(ActionEvent ae) {
int nextRow = table.getRowCount();
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[] {"" + nextRow, "" + nextRow});
// the correct row is: nextRow. No loop required here.
table.setDefaultRenderer(Object.class, new GrayWhiteRenderer(nextRow));
}
I would also correct your renderer to provide selection highlight
class GrayWhiteRenderer extends DefaultTableCellRenderer {
private int rowToColored;
GrayWhiteRenderer(int rowToColored) {
this.rowToColored = rowToColored;
}
#Override
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);
// As I know there is no value is registered for "table.row" in UIManager
// so I've skipped the first condition
if (row == rowToColored) {
c.setBackground(Color.GRAY.brighter());
} else {
// use correct color depended on whether the cell is selected or not!
c.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
}
return c;
}
}
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();
}
}
);
}
}
My next annoying problem is to set Space between cells so that content of cells doesn't get erased.
I tried already all Internet solutions but they don't help because as I encrease border then content of cell is erased.
Here is what I tried:
public class DateFormatDemo extends JFrame
{
private Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
private JTable dataSearchResultTable;
public DateFormatDemo()
{
JPanel panel = new JPanel(new GridLayout(2, 1, 5, 10));
panel.setPreferredSize(new Dimension(500, 300));
panel.add(new JScrollPane(initDataSearchResultTable()));
super.getContentPane().add(panel);
super.pack();
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
super.setVisible(true);
}
private JTable initDataSearchResultTable()
{
dataSearchResultTable = new JTable(new MyTableModel()) {
#Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
JComponent component = (JComponent) super.prepareRenderer(renderer, row, column);
// component.setBorder(emptyBorder);
return component;
}
};
dataSearchResultTable.setIntercellSpacing(new Dimension(5, 10));
dataSearchResultTable.setSelectionBackground(new Color(0xaaaaff));
dataSearchResultTable.setFillsViewportHeight(true);
dataSearchResultTable.setRowSelectionAllowed(true);
dataSearchResultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
dataSearchResultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
dataSearchResultTable.setDefaultRenderer(String.class, new DefaultTableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
JComponent component = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
component.setBorder(emptyBorder);
return component;
}
});
return dataSearchResultTable;
}
class MyTableModel extends AbstractTableModel
{
private String[] columnNames = { "First Name", "Last Name", "Timestamp", "Number", "Vegetarian" };
private Object[][] data ;
MyTableModel()
{
data = new Object[][] {
{ "Vova", "KipokKipokKipokKipok", Timestamp.valueOf("2013-04-12 11:20:41"), new Integer(5),
new Boolean(true) },
{ "Olia", "Duo", Timestamp.valueOf("2010-01-11 11:11:41"), new Integer(3), new Boolean(false) },
{ "Oksana", "Stack", Timestamp.valueOf("2012-04-12 11:20:41"), new Integer(2), new Boolean(false) },
{ "Petro", "White", Timestamp.valueOf("2010-04-12 11:20:21"), new Integer(20), new Boolean(true) },
{ "Ivan", "Brown", Timestamp.valueOf("2011-04-11 11:20:41"), new Integer(10), new Boolean(true) } };
fireTableDataChanged();
}
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)
{
if (data.length > 0 && data[0] != null) {
return data[row][col];
}
return null;
}
/*
* JTable uses this method to determine the default renderer/ editor for
* each cell. If we didn't implement this method, then the last column
* would contain text ("true"/"false"), rather than a check box.
*/
public Class getColumnClass(int c)
{
Object valueAt = getValueAt(0, c);
return valueAt == null ? Object.class : valueAt.getClass();
}
/*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col)
{
return true;
}
/*
* Don't need to implement this method unless your table's data can
* change.
*/
public void setValueAt(Object value, int row, int col)
{
if (data.length > 0 && data[0] != null) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
}
public static void main(String[] args) throws ParseException
{
new DateFormatDemo();
}
}
Does anybody know how to set space between cells so that cell's content is not erased?
SOLVED:
dataSearchResultTable.setRowHeight(25);
dataSearchResultTable.setRowMargin(5);
dataSearchResultTable.getColumnModel().setColumnMargin(20);
But rowMargin causes selected row to be narrower than row borders. The solution is to set rowHeight only without rowMargin. This ensures that selection is not narrower than row itself.
Thanks!
Instead of using setRowHeight() method as you mentioned, I recommend you to use next method for your table(found here):
private void updateRowHeights(JTable table ) {
for (int row = 0; row < table.getRowCount(); row++) {
int rowHeight = table.getRowHeight();
for (int column = 0; column < table.getColumnCount(); column++) {
Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
rowHeight = Math.max(rowHeight,comp.getPreferredSize().height);
}
table.setRowHeight(row, rowHeight);
}
}
Because it calculates height for each row. In this case all your rows will be look fine.
When you use setRowHeight() isn't always solve problems of system default properties, try to add that line to your code for example and you will see what I mean dataSearchResultTable.setFont(new Font("Arial", 1, 25));
Is there anyone know how I can add a checkbox in this code:
String data[][]={
{"Apple","Banana","Mango"}, {"Apple","Banana","Mango"}, {"Apple","Banana","Mango"}
};
String column[]={"Fruits","Fruits","Fruits"};
table=new JTable(new DefaultTableModel(data, column)){
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);
public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent)c;
// Add a border to the selected row
if (isRowSelected(row))
jc.setBorder( highlight );
return c;
}
};
jScrollPane1.setViewportView(table);
I just want to add checkboxes so that if I check a checkbox it will highlight and all checked checkboxes will be highlighted. Thank You in advance for helping me!
Here is pseudo code, I found somewhere in my repository. Use it according to your use.
import java.awt.Color;
import java.awt.Component;
import javax.swing.*;
import javax.swing.table.*;
public class TableCheckBoxHighLight extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
public TableCheckBoxHighLight() {
Object[] columnNames = { "Col1", "Col2", "Select" };
Object[][] data = {
{ "Item1", "123", false },
{ "Item2", "345", false },
{ "Item3", "678", false },
{ "Item4", "901", false }
};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model) {
private static final long serialVersionUID = 1L;
#SuppressWarnings("unchecked")
#Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return String.class;
case 1:
return String.class;
default:
return Boolean.class;
}
}
#Override
public Component prepareRenderer(TableCellRenderer renderer,
int row, int col) {
Component c = super.prepareRenderer(renderer, row, col);
int[] selCols = table.getSelectedColumns();
table.setSelectionBackground(Color.GREEN);
for (int i : selCols)
c.setBackground(Color.RED);
return c;
}
};
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() {
TableCheckBoxHighLight frame = new TableCheckBoxHighLight();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
});
}
}
I have a JTextArea that I'm using as a cell renderer for a table. In the getTableCellRenderercomponent method I have:
setText(getTextForCell());
setSize(table.getColumnModel().getColumn(column).getWidth(), 0);
getUI().getRootView(textArea).setSize(textArea.getWidth(), 0f);
updateSize();
private void updateSize() {
int prefHeight = textArea.getPreferredSize().height;
int currHeight = table.getRowHeight(r);
if (prefHeight > currHeight) {
table.setRowHeight(row, prefHeight);
}
When the text area uses wrap style word, it is sometimes a row short.
If I call this updateSize method from outside getTableCellRendererComponent then it works properly. But with a large table, calling update size on all rows whenever a column size adjusts is not feasible because it is too slow, so I've been trying to find a way to do the resize during the row rendering.
There is a related Java bug (that is marked as fixed but it does not appear that it really is) http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4446522, but this workaround does not appear to work when using word wrap.
Can anyone provide an alternative on how to make this work properly?
An alternative works properly
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;
public class AutoWrapInJTable {
public AutoWrapInJTable() {
String[] columnNames = {"TextAreaCellRenderer"};
Object[][] data = {
{"123456789012345678901234567890"},
{"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddx"},
{"----------------------------------------------0"},
{">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>|"},};
TableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable table = new JTable(model) {
private static final long serialVersionUID = 1L;
#Override
public void doLayout() {
TableColumn col = getColumnModel().getColumn(0);
for (int row = 0; row < getRowCount(); row++) {
Component c = prepareRenderer(col.getCellRenderer(), row, 0);
if (c instanceof JTextArea) {
JTextArea a = (JTextArea) c;
int h = getPreferredHeight(a) + getIntercellSpacing().height;
if (getRowHeight(row) != h) {
setRowHeight(row, h);
}
}
}
super.doLayout();
}//http://tips4java.wordpress.com/2008/10/26/text-utilities/
private int getPreferredHeight(JTextComponent c) {
Insets insets = c.getInsets();
View view = c.getUI().getRootView(c).getView(0);
int preferredHeight = (int) view.getPreferredSpan(View.Y_AXIS);
return preferredHeight + insets.top + insets.bottom;
}
};
table.setEnabled(false);
table.setShowGrid(false);
table.setTableHeader(null);
table.getColumnModel().getColumn(0).setCellRenderer(new TextAreaCellRenderer());
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane sp = new JScrollPane(table);
sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JPanel p = new JPanel(new BorderLayout());
p.add(sp);
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.add(p);
//f.pack();
f.setSize(200, 200);
f.setLocation(150, 150);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
AutoWrapInJTable autoWrapInJTable = new AutoWrapInJTable();
}
});
}
}
class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {
private static final long serialVersionUID = 1L;
private final Color evenColor = new Color(230, 240, 255);
public TextAreaCellRenderer() {
super();
setLineWrap(true);
setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
}
#Override
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());
setBackground((row % 2 == 0) ? evenColor : getBackground());
}
setFont(table.getFont());
setText((value == null) ? "" : value.toString());
return this;
}
}