static class DateRenderer extends DefaultTableCellRenderer {
DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy");
public DateRenderer() { super(); }
public void setValue(Object value) {
if (formatter==null) {
formatter = DateFormat.getDateInstance();
}
setText((value == null) ? "" : formatter.format(value));
}
}
I use this code to render dates. I found it as this but now i need something else. I have a column with numbers as this (1234.56). I want to render the numbers as (1234.56 TL). But i am kinda beginner so i can't find the way to do it.
TableColumnModel m = table.getColumnModel();
m.getColumn(5).setCellRenderer(new TableRendererExample());
class TableRendererExample extends DefaultTableCellRenderer {
#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 (column == 5) {
setText(value.toString()+" TL");
}
return this;
}
}
This code works fine for answer.
Related
I want to change the format of column 2 (Columna 2) to currency format of all the data, I am trying with this, but it does not do anything.
Could you help me?
These are my data:
I want the data to come out like this:
With this code I am trying:
public class CurrencyCellRenderer extends DefaultTableCellRenderer {
#Override
public final Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
final Component result = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (value instanceof Number) {
setHorizontalAlignment(JLabel.CENTER);
setText(defaultFormat.format(value));
} else {
setText("NO");
}
return result;
}
}
jTable1.getColumnModel().getColumn(2).setCellRenderer(new CurrencyCellRenderer());
It does not do anything, could you help me?
Thanks!!!
You need to create result after changing value's value. Creating result before, will just return the same old data.
e.g.,
private class CurrencyCellRenderer extends DefaultTableCellRenderer {
#Override
public final Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null) {
value = 0.0;
}
value = myFormat.format(value);
final Component result = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
return result;
}
}
My MCVE for proof of concept:
import java.awt.BorderLayout;
import java.awt.Component;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
#SuppressWarnings("serial")
public class CurrencyTable extends JPanel {
private static final String[] TABLE_HEADERS = { "Text", "Value" };
private static final Object[][] DATA = { { "Foo", 1.1 }, { "Bar", 2.2 }, { "Bax", 3.3 } };
private DefaultTableModel tableModel = new DefaultTableModel(DATA, TABLE_HEADERS);
private JTable table = new JTable(tableModel);
private NumberFormat myFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
public CurrencyTable() {
table.getColumnModel().getColumn(1).setCellRenderer(new CurrencyCellRenderer());
setLayout(new BorderLayout());
add(new JScrollPane(table));
}
private class CurrencyCellRenderer extends DefaultTableCellRenderer {
#Override
public final Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null) {
value = 0.0;
}
value = myFormat.format(value);
final Component result = super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
return result;
}
}
private static void createAndShowGui() {
CurrencyTable mainPanel = new CurrencyTable();
JFrame frame = new JFrame("CurrencyTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
Ok, i have the solution:
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
public class MyRenderer extends JLabel implements TableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int col) {
DefaultTableCellRenderer renderer
= new DefaultTableCellRenderer();
Component c = renderer.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, col);
String s = "";
if (col == 2) {
double d = Double.parseDouble( String.valueOf(value)) ;
s = defaultFormat.format(d);
c = renderer.getTableCellRendererComponent(table, s,
isSelected, hasFocus, row, col);
((JLabel) c).setHorizontalAlignment(SwingConstants.RIGHT);
}
return c;
}
}
jTable1.getColumnModel().getColumn(2).setCellRenderer(new MyRenderer());
Image here
Thanks for all guys!
in my custom JTable I have multiple columns. One column is of type java.sql.Timestamp. I want to change the backgroundcolor of all cells and I want to change the format of only the Timestamp cells. I made the following customcellrenderer which I call by:
for(int i = 0; i < tableTask.getColumnCount(); i++)
{
Class columnClass = myTaskTM.getColumnClass(i);
TableCellRenderer defaultRenderer = tableTask.getDefaultRenderer(columnClass);
tableTask.getColumnModel().getColumn(i).setCellRenderer(new CustomRenderer(defaultRenderer));
}
customrenderer:
public class CustomRenderer extends DefaultTableCellRenderer
{
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
TableCellRenderer delegate;
public CustomRenderer(TableCellRenderer defaultRenderer)
{
super();
delegate = defaultRenderer;
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component c = delegate.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Color foreground, background;
if(!isSelected)
{
...
}
else
{
background = javax.swing.UIManager.getDefaults().getColor("Table.selectionBackground");
//foreground = javax.swing.UIManager.getDefaults().getColor("Table.selectionForeground");
foreground = Color.BLACK;
}
c.setForeground(foreground);
c.setBackground(background);
setValue(value);
return c;
}
#Override
public void setValue(Object value) {
if (formatter == null) {
formatter = DateFormat.getDateInstance();
}
if(value != null)
{
if(value.getClass().toString().equals("class java.sql.Timestamp"))
{
((JLabel)delegate).setText((value == null) ? "" : formatter.format(value));
}
else
{
((JLabel)delegate).setText(value.toString());
}
}
}
}
The setValue only works if getTableCellRendererComponent is commented out.
How can I get both to work?
The setValue() should call delegate.setText() because the delegate's component is actually used. Also I don't even see where the setValue() is called in the renderer.
Call it in your renderer passing the Object value.
I am changing the background color of a row in JTable using following code.
The color of row gets changed for all the cells which have String values however it does not gets changed for cells with Integer or Double values.
private 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) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
String status = (String)table.getModel().getValueAt(row, index);
if (Constants.seller.equals(status)) {
c.setBackground(Color.GRAY);
//setForeground(Color.WHITE);
} else {
c.setBackground(table.getBackground());
c.setForeground(table.getForeground());
}
return c;
}
});
return table;
}
Try to register the same renderer also for Integer and Double. These classes have separate default renderer registered by default. Something like this.
private 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) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
String status = (String)table.getModel().getValueAt(row, index);
if (Constants.seller.equals(status)) {
c.setBackground(Color.GRAY);
//setForeground(Color.WHITE);
} else {
c.setBackground(table.getBackground());
c.setForeground(table.getForeground());
}
return c;
}
});
table.setDefaultRenderer(Number.class, table.getDefaultRenderer(Object.class));
table.setDefaultRenderer(Double.class, table.getDefaultRenderer(Object.class));
return table;
}
public class cellRender extends DefaultTableCellRenderer
{
#Override
public Component getTableCellRendererComponent(JTable tblPackage, Object value, boolean isSelected, boolean hasocus, int row, int col)
{
Component c = super.getTableCellRendererComponent(tblPackage, value, isSelected, hasocus, row, col);
if(tblPackage.getColumnModel().getColumn(col).getIdentifier().equals("Package Status"))
{
if(value.toString().equals("ACTIVE"))
{
c.setBackground(Color.GREEN);
}
}
return this;
}
}
The symbol shows that it cannot find the symbol...what's the problem with that?
I assume you're talking about a compile time error but I don't have one. This class
compiles fine (and I didn't make any changes to your code except adding import
statements). So check if you have all import statements correct.
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class cellRender extends DefaultTableCellRenderer
{
#Override
public Component getTableCellRendererComponent(JTable tblPackage, Object value, boolean isSelected, boolean hasocus, int row, int col)
{
Component c = super.getTableCellRendererComponent(tblPackage, value, isSelected, hasocus, row, col);
if(tblPackage.getColumnModel().getColumn(col).getIdentifier().equals("Package Status"))
{
if(value.toString().equals("ACTIVE"))
{
c.setBackground(Color.GREEN);
}
}
return this;
}
}
In your return statement it should be the component so use the below:
return c;
In your code
return this;
this statement is wrong. Because this refers to the current object and you need to return the component.
So you need to replace this by c like
return c;
EDIT:
Try this I am not sure about this but...
tblPackage.setDefaultRenderer(Object.class, new TableCellRenderer(){
private DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
#Override
public Component getTableCellRendererComponent(JTable tblPackage, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
Component c = DEFAULT_RENDERER.getTableCellRendererComponent(tblPackage, value, isSelected, hasFocus, row, col);
if(tblPackage.getColumnModel().getColumn(col).getIdentifier().equals("Package Status"))
{
if(value.toString().equals("ACTIVE"))
{
c.setBackground(Color.GREEN);
}
}
return c;
}
});
I have a boolean column in JTable, so i each cell there is a checkbox. When i try to set a cell renderer in order to change the background color the checkbox from cell replaced by values (true, false).. What's goin wrong!!
My cell renderer:
class BackgroundTableCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int col) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, col);
if(row%2 == 0)
{
Color bColor = Color.decode("#EFF2ED");
c.setBackground(bColor);
}
else
c.setBackground(Color.white);
return c;
}
}
I set the renderer like that:
data_table.setDefaultRenderer(Boolean.class, new BackgroundTableCellRenderer());
use prepareRenderer for line decorating
or for (Xxx)TableCellRenderer you can to try with
.
class BooleanRenderer extends JCheckBox implements TableCellRenderer, UIResource {
private static final long serialVersionUID = 1L;
private final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
BooleanRenderer() {
super();
setHorizontalAlignment(JLabel.CENTER);
setBorderPainted(true);
}
#Override
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());
if (hasFocus) {
setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
} else {
setBorder(noFocusBorder);
}
return this;
}
}