How to add image and text in same JTable cell? [duplicate] - java

I know how to put a String into a JTable cell, and I know how to put an image into a JTable cell. But is it possible to put an image and a String into the SAME JTable cell?
The reason for this is that I have a 'status' column in my JTable, which at the moment contains either a green, amber or red image. And in order to fulfill the design requirement, I need to add some explanatory text alongside each image (so the text next to the green image would be "Online", the text next to the amber image would be "Unknown" and the text next to the red image would be "Offline"). I need to do this in a single column (or what looks/behaves like a single column) rather than two columns.
I have researched this, but found no info at all.

Yes.
You need to use a custom cell renderer. Check out How to use Tables for more details.
You actually have two choices, you could simply set the icon and the text of the cell or you could use the renderers tooltip text instead...
public class IconTextCellRemderer extend DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setText(...);
setIcon(...);
setToolTipText(...);
return this;
}
}
Of course you need to apply the renderer to the column...
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(x).setCellRenderer(new IconTextCellRemderer());

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.

How to format contents of a JTable

I am using a JTable to display numerical data and strings. The numerical data default formats to the right hand side of the JTable, and the strings format to the left. I want both to be formatted into the center of the cell. I am using Nedbeans to develop the GUI but it does not seem to help with this issue.
My attempt was to create a cell renderer class that overrides the JTable default cell renderer, but I
don't know the line of code to actually change the formatting in the new cell renderer.
Any help would be appreciated.
Your thinking is correct. Within the custom TableCellRenderer, you can actually check which column/row/cell is rendered and subsequently assign a column/row/cell specific formatting.
public static class CustomTableCellRenderer extends DefaultTableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
DefaultTableCellRenderer c = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// center everything in the first column
if (column == 0) {
c.setHorizontalAlignment(JLabel.CENTER);
}
// the background and border of the first cell should be gray
if (column == 0 && row == 0) {
c.setBackground(Color.GRAY);
c.setBorder(BorderFactory.createMatteBorder(0, 5, 0, 5, Color.GRAY));
}
return c;
}
}
Please note that the DefaultTableCellRenderer is called for each individual cell.
All available formatting functions are well described in the respective documentation:
https://docs.oracle.com/javase/10/docs/api/javax/swing/table/DefaultTableCellRenderer.html

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;
}
});

Categories