Change default row colors in jtable - java

I'm developing a simple java application using swing. I use JTable element.
The problem is that by default rows of tables are white and grey like in this post Setting color in a row of a Jtable .
I want to make them the same color, for example all rows white.

You can override the prepareRenderer method of JTable like this
JTable table = new JTable(...)
{
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
c.setBackground(Color.WHITE);
return c;
}
};
Or you could create your own TableCellRenderer which does the same thing (picking the background color to render) but on a Cell level and use that renderer for each of your columns.

Related

My columns in the JTable don't get all a background color

My columns in my table don't all get a background color. If I use a checkbox in my table, it does not get a background color.
I used this code to set the background:
participantsTable.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
c.setBackground(row % 2 == 0 ? new Color(230, 230, 230): Color.WHITE);
return this;
}
});
I've tried searching the internet for a solution, but to no avail. I'm not so familiar with the JTabel that I could come up with the error myself.
Here is what shouldn't happen:
So it shouldn't look like that, but the background of the checkbox should be the same as the column to the left of it.
What am I doing wrong and how can I fix this problem?
the background of the checkbox should be the same as the column to the left of it.
Different data types use different renderers.
In your case you would also need to customize the renderer for the Boolean.class.
Or alternatively you can override the prepareRenderer(...) method of the JTable.
A basic example would be:
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;
}
};
This method is invoked after the renderer for the cell has been determined so it will work for all columns of the table.
Check out Table Row Rendering for more information and examples of custom rendering using this approach.

DefaultCellRenderer() stops working when column width = 0

So, the thing is that I have a JTable which turns rows orange when the data on one of the columns is 'true' (using a class which extends DefaultCellRenderer), problem is I don't want to show that column, so I've tried removing it or setting its width to 0, but when I do that, it no longer highlights the row in orange. Is there a way to do this or does it always have to be shown info?
Thanks.
I've tried removing it or setting its width to 0, but when I do that, it no longer highlights the row in orange
I guess because the width is 0, there is nothing to render so the renderer is never invoked.
In any case, don't use a cell width of 0. As you tab through the table that column will still get focus, but the user won't know it has focus which will confuse the user.
Instead if you want to hide a column then remove the TableColumn from the TableColumnModel. You get the TableColumnModel from the JTable by using the getColumnModel() method. Removing the column from the model just prevents the column from being displayed but the data is still contained in the TableModel.
I have a JTable which turns rows orange when the data on one of the columns is 'true'
You can override the prepareRenderer(...) method of the JTable to do this easily. This will work even you have columns with different types of data so there is no need to create multiple renderers.
The basic logic is:
JTable table = new JTable(...)
{
public Component prepareRenderer(
TableCellRenderer renderer, int row, int column)
{
Component c = super.prepareRenderer(renderer, row, column);
if (!isRowSelected(row))
{
c.setBackground(getBackground());
int modelRow = convertRowIndexToModel(row);
boolean highlight = (Boolean)getModel().getValueAt(modelRow, ???);
if (highlight) c.setBackground(Color.ORANGE);
}
return c;
}
};
Check out Table Row Rendering for more information and a working example. The example on the "Data" tab does what you want.
Can you try this. I assume that you are familiar with table cell renderer. you can override the getTableCellRendererComponent method and change the color like below:
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// Get default renderer from the table
TableCellRenderer renderer = table.getDefaultRenderer(table.getColumnClass(column));
Component comp = renderer.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (value.equals("true") && row == 1 && col== 1){
comp.setBackground("YOUR COLOR");
}
}

Not able to set color on row from jcolorchooser [duplicate]

I am novice to Swing. I just started Swing couple of weeks ago and I stuck at some point..
I have designed a JTable having some rows on clicking (right) it opens a popup menu which have "CHANGE ROW COLOR" option (JMenuItem),on clicking which JColorChooser opens up and user can choose the color and same color will be set to the selected row.
Is it possible to do it? How?
enter image description here
enter image description here
If you want the ability to color each row separately then one way is to store the Color as part of the data in the TableModel. So you will need to add the Color as a column in the model.
But you will not want to display this column in the view of the table so you will need to remove it from the view:
table.removeColumn( table.getColumn(...) );
Next you will need to add custom rendering for the table. One way to do this is to add rendering for the entire row. Check out Table Row Rendering for an example of this approach.
So the basic code for the rendering would be something like:
Color background = table.getTableModel.getValueAt(row, ???);
if (background != null)
c.setBackground( background );
And when you display the color choose you would need to save the Color to the TableModel:
table.getTableModel().setValueAt(color, table.getSelectedRow(), ???);
Another way would be to save the row & the color in a map<Integer, Color>
(use table.getSelectedRow() )
To capture the color from the JColorchooser, use :
Color selectedColor = myColorChooser.getSelectionModel().getSelectedColor();
Then, modify the default renderer :
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
column);
if (listOfColor.containKey(row)) {
c.setBackground(listOfColor.get(row));
}
DefaultTableCellRenderer centerRenderer = (DefaultTableCellRenderer) c;
centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
return c;
}
});

Setting the Color on the row of the JTable using JColorChooser

I am novice to Swing. I just started Swing couple of weeks ago and I stuck at some point..
I have designed a JTable having some rows on clicking (right) it opens a popup menu which have "CHANGE ROW COLOR" option (JMenuItem),on clicking which JColorChooser opens up and user can choose the color and same color will be set to the selected row.
Is it possible to do it? How?
enter image description here
enter image description here
If you want the ability to color each row separately then one way is to store the Color as part of the data in the TableModel. So you will need to add the Color as a column in the model.
But you will not want to display this column in the view of the table so you will need to remove it from the view:
table.removeColumn( table.getColumn(...) );
Next you will need to add custom rendering for the table. One way to do this is to add rendering for the entire row. Check out Table Row Rendering for an example of this approach.
So the basic code for the rendering would be something like:
Color background = table.getTableModel.getValueAt(row, ???);
if (background != null)
c.setBackground( background );
And when you display the color choose you would need to save the Color to the TableModel:
table.getTableModel().setValueAt(color, table.getSelectedRow(), ???);
Another way would be to save the row & the color in a map<Integer, Color>
(use table.getSelectedRow() )
To capture the color from the JColorchooser, use :
Color selectedColor = myColorChooser.getSelectionModel().getSelectedColor();
Then, modify the default renderer :
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
column);
if (listOfColor.containKey(row)) {
c.setBackground(listOfColor.get(row));
}
DefaultTableCellRenderer centerRenderer = (DefaultTableCellRenderer) c;
centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
return c;
}
});

Java- Coloring matching cells in two different JTables

I am writing a utility that will compare metadata fields from a SQL database. I am able to get all of the data into a JTable. I am trying to now color each particular cell depending on if they match the cell of another JTable. From my understanding I need to write a CellRenderer (Change the color of specific rows in my JTable). I had thought doing something like the below to compare the two values would be the best solution
for(int i=0;i<col2;i++){
for(int j=0;j<row2;j++){
if(table1.getValueAt(i,j).equals(table2.getValueAt(i,j))){
table1.setSelectionBackground(Color.GREEN);
table2.setSelectionBackground(Color.GREEN);
}else if(!(table1.getValueAt(i, j).equals(table2.getValueAt(i, j)))){
table1.setSelectionBackground(Color.RED);
table2.setSelectionBackground(Color.RED);
}
}
}
I know the that the setSelectionBackground is not the method I want to call. I'm confused on how to write the CellRenderer listed in the above post to change the background color of a cell in depending if the contents match each other. Is writing the custom CellRenderer the only option?
EDIT 1:
As of right now, it appears to be taking in the correct color for background but it is coloring the entire table rather than a specific cell. Below is my CellRenderer and one of the for loops for how I think I should be calling the setBackground method
private class CellRenderer extends 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);
this.setOpaque(true);
this.setBackground(table.getBackground());
return this;
}
int col1 = table1.getColumnCount()-1;
int row1 = table1.getRowCount()-1;
int col2 = table2.getColumnCount()-1;
int row2 = table2.getRowCount()-1;
table1.setDefaultRenderer(Object.class, new CellRenderer());
table2.setDefaultRenderer(Object.class, new CellRenderer());
if(row1>row2){
if(col1>col2){
for(int i=0;i<row2;i++){
for(int j=0;j<col2;j++){
if(table1.getValueAt(i,j).equals(table2.getValueAt(i,j))){
color = Color.GREEN;
System.out.println(color);
table1.setBackground(color);
table2.setBackground(color);
}else if(!(table1.getValueAt(i, j).equals(table2.getValueAt(i, j)))){
color = Color.RED;
System.out.println(color);
table1.setBackground(color);
table2.setBackground(color);
}
}
}

Categories