I have a Simple JTable holding Integers in the first column and Strings in the Second. I would like to be able to sort each Column.
My Renderer:
package gui.table;
import gui.DaimlerColor;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
#SuppressWarnings("serial")
public class StandardCellRenderer 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);
c.setBackground((isSelected ? DaimlerColor.LIGHT_BLUE : (row % 2 == 1 ? DaimlerColor.DARK_WHITE : DaimlerColor.WHITE)));
if(isSelected){
c.setBackground(DaimlerColor.LIGHT_BLUE);
}
return c;
}
}
My TableModel:
package gui.table;
import java.util.List;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import org.jdom2.Element;
import reporting.FailureClassificationCatalogue;
#SuppressWarnings("serial")
public class ReportTableModel extends DefaultTableModel {
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
public ReportTableModel(List<Element> reports) {
super(createDataVector(reports), createColumnNames());
}
private static Vector<Vector<Object>> createDataVector(List<Element> reports) {
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
for (int i = 0; i < reports.size(); i++) {
Vector<Object> row = new Vector<Object>();
if(reports.get(i).getName().equals("Default")){
row.add("-");
}
else{
row.add(Integer.valueOf(i+1));
}
String title = reports.get(i).getAttributeValue("type");
try{
title = FailureClassificationCatalogue.valueOf(title).getName();
}catch(IllegalArgumentException iae){
}catch(NullPointerException npe){
}
row.add(title);
data.add(row);
}
return data;
}
#Override
public Class<?> getColumnClass(int colNum) {
switch (colNum) {
case 0:
return Integer.class;
case 1:
return String.class;
default:
return String.class;
}
}
private static Vector<String> createColumnNames() {
Vector<String> columns = new Vector<String>();
columns.add("Number");
columns.add("Error Type");
return columns;
}
/**
* overridden to ignore null values
*/
#Override
public void setValueAt(Object value, int row, int col) {
if (value == null) {
return;
}
// delegate to the parent method
super.setValueAt(value, row, col);
}
}
If I don´t implement the getColumnClass then the renderer works fine but the Integers are sorted as Strings. As soon as I implement the method the renderer doesn´t work properly (background is only set in one column) but the Integers are sorted correctly. How can I resolve this?
My Code to set the renderer and sorter:
reporttable = new JTable();
reporttable.setName("report");
reporttable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
reporttable.setRowHeight(40);
reporttable.addMouseListener(tgc.mouseListener);
reporttable.setDefaultRenderer( Object.class, new StandardCellRenderer());
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>();
reporttable.setRowSorter(sorter);
sorter.setModel(reportModel);
Any ideas? Thanks.
Absent an sscce, two several things stand out:
The call to setDefaultRenderer() should specify the data type to which it applies, e.g. String.class or Integer.class. Alternatively, override the table's prepareRenderer() to consider all cells, as shown here.
reporttable.setDefaultRenderer(Integer.class, new StandardCellRenderer());
Your TableRowSorter constructor "Creates a TableRowSorter with an empty model." Instead, pass your TableModel to the constructor or use setAutoCreateRowSorter(true).
reporttable.setAutoCreateRowSorter(true);
"When using a sorter, always remember to translate cell coordinates," as discussed here.
Related
Below code to display a JTable with 3 columns, which respectively contain a JComboBox, a String and a double, and which should display yellow. The problem is that I cannot get the JComboBox in the first column to display as ... a combo box; instead I get a String saying "javax.swing.JComboBox...". What am I doing wrong?
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;
import java.awt.*;
public class BadDialog extends JDialog {
//Instantiate the data for the table, which is 2 rows x 3 cols
private final JComboBox col0ComboBox = new JComboBox(new String[]{"aaa", "bbb"}); //Goes in all rows of Col 0
private final String[] col1Data = {"Mickey", "Mouse"};
private final double[] col2Data = {111, 222};
public BadDialog() {
//Instantiate table
JTable badTable = new JTable();
//Assign a tableModel to the table, put the table in a scroller, add it to this dialog, and sort out the renderer
TableModel badTableModel = new BadTableModel();
badTable.setModel(badTableModel);
JScrollPane scroller = new JScrollPane(badTable);
add(scroller);
BadTableCellRenderer badTableCellRenderer = new BadTableCellRenderer();
badTable.setDefaultRenderer(JComboBox.class, badTableCellRenderer); //Col 0
badTable.setDefaultRenderer(String.class, badTableCellRenderer); //Col 1
badTable.setDefaultRenderer(Double.class, badTableCellRenderer); //Col 2
//Assign col0ComboBox to Col 0
badTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(col0ComboBox));
//Show the dialog
setPreferredSize(new Dimension(300, 470));
pack();
setModal(true);
setLocation(10, 10);
setVisible(true);
}
private final class BadTableModel extends AbstractTableModel {
#Override
public int getRowCount() {
return 2;
}
#Override
public int getColumnCount() {
return 3;
}
#Override
public Object getValueAt(int rowIndex, int colIndex) {
if (colIndex == 0) return col0ComboBox;
if (colIndex == 1) return col1Data[rowIndex];
return col2Data[rowIndex];
}
#Override
public Class<?> getColumnClass(int colIndex) {
if (colIndex == 0) return JComboBox.class;
if (colIndex == 1) return String.class;
return Double.class;
}
}
private static class BadTableCellRenderer extends 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);
//Make all columns yellow
c.setBackground(Color.YELLOW);
c.setForeground(Color.RED);
c.setFont(new Font("Dialog", Font.PLAIN, 12));
return c;
}
}
public static void main(String[] args) {
new BadDialog();
}
}
Never return a component in a TableModel. The whole point of having a separate model and view is that the model contains only data, not components. The model’s job is to provide data; the view’s job is to determine how to display that data.
Your TableModel’s getColumnClass method should look like this:
public Class<?> getColumnClass(int colIndex) {
if (colIndex == 0) return String.class; // String, not JComboBox
if (colIndex == 1) return String.class;
return Double.class;
}
and your getValueAt method needs to return the actual data value at that row:
public Object getValueAt(int rowIndex, int colIndex) {
if (colIndex == 0) return (rowIndex % 1 == 0 ? "aaa" : "bbb");
if (colIndex == 1) return col1Data[rowIndex];
return col2Data[rowIndex];
}
The cell renderer is part of the view, not the model, so it can make use of a JComboBox. Your render needs to use the value argument to modify your JComboBox:
private static class BadTableCellRenderer extends DefaultTableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
if (row != 0) {
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
}
JComboBox c = col0ComboBox;
c.setSelectedItem(value);
//Make all columns yellow
c.setBackground(Color.YELLOW);
c.setForeground(Color.RED);
c.setFont(new Font("Dialog", Font.PLAIN, 12));
return c;
}
}
Your TableModel is completely wrong.
The data of the TableModel must be stored in the TableModel, not as an Array external to the model.
Don't extend AbstractTableModel. Instead extend DefaultTableModel. The DefaultTableModel already provides data storage and methods to update the data of each cell. Then the only method you need to override is the getColumnClass(...) method, to make sure the appropriate renderer/editor is used for each column.
See: Sorting JTable programmatically for a basic example showing how to add initial data to a JTable. The getColumnClass(...) method in that example is more generic.
You can simplify the getColumnClass(...) method as follows:
#Override
public Class getColumnClass(int column)
{
switch (column)
{
case 2: return Double.class;
default: return super.getColumnClass(column);
}
}
Then if you want the editor of the first column to be a combobox you would use:
badTable.getColumnModel().getColumn(0).setCellEditor( new DefaultCellEditor(col0ComboBox));
You should NOT use the same renderer for all 3 columns because typically numbers are displayed right aligned in the column.
There should be no need for custom renderers. Instead you change properties of the table:
table.setBackground(...);
table.setForeground(...);
table.setFont(...);
These values will then be used by each of the default column renderers.
Ok I'm in this situation... I have Renderer in my class but have no idea how to use it to make certain cell's background red. It's a room renting app and I have Jtable as calendar so I want paint cells which are rent red. So It should somehow take specific column and row and make that cell red. My renderer down bellow but as I said no Idea how to use it since Im new to java. Real question how do I pass that column and row, I have problem with that. Cell rendered worked with some other code but that wasnt what I need.
ublic class TableColour extends javax.swing.table.DefaultTableCellRenderer {
#Override
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
cellComponent.setBackground(java.awt.Color.RED);
return cellComponent;
}
}
Alright oh wow I might have some trouble figuring this out. But maybe somehow. You said you dont know how my code looks, well I have some basic renderer. One thing to have in mind I have 2 dimensional array ReservedOne which holds row index and column index of taken room also room number date, time until which its reserved. So now Im a bit confused when looking at your example how to use my array to set colors. I hope I wont have mental breakdown
Your TableModel should be modeling this data, this is very important, as it allows the rest of the API to revolve around it
Real question how do I pass that column and row, I have problem with that. Cell rendered worked with some other code but that wasnt what I need.
This is why it's important to have the TableModel wrap the data, as the table API will pass the row and column information to the TableCellRenderer, but it will also pass the cell value!
public class RoomTableModel extends AbstractTableModel {
private Room[][] reservations;
public RoomTableModel(Room[][] reservations) {
this.reservations = reservations;
}
#Override
public int getRowCount() {
return reservations.length;
}
#Override
public int getColumnCount() {
return reservations[0].length;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return reservations[rowIndex][columnIndex];
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return Room.class;
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (aValue instanceof Room) {
Room room = (Room) aValue;
reservations[rowIndex][columnIndex] = room;
fireTableCellUpdated(rowIndex, columnIndex);
}
}
}
This means we can now setup the cell renderer to display the information we need
public static class RoomTableCellRenderer extends DefaultTableCellRenderer {
private static Color BOOKED_COLOR = Color.RED;
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Room && value != null) {
if (isSelected) {
setBackground(table.getSelectionBackground());
setForeground(table.getSelectionForeground());
} else {
setBackground(table.getBackground());
setForeground(table.getForeground());
}
// Update the details based on the room properties
} else { //if (value == null) {
setBackground(BOOKED_COLOR);
setText(null);
}
return this;
}
}
Don't forget, if you want the table to use your renderer, you need to register it...
table.setDefaultRenderer(Room.class, new RoomTableCellRenderer());
Updated...
Based on the available data been stored in 2D String array (you really don't like me).
This is a little dirty. In reality, the data should be setup in such away as it could be passed to the TableModel and let it take care of the details. You're also going to have be careful in how you update the array, as updates won't be reflected by the table until you can force it to refresh ... and that won't be pretty.
public class LocalDateTableCellRenderer extends DefaultTableCellRenderer {
protected static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd");
private String[][] bookings;
public LocalDateTableCellRenderer(String[][] bookings) {
this.bookings = bookings;
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
if (value instanceof LocalDate) {
LocalDate date = (LocalDate) value;
if (hasBookingFor(date)) {
setForeground(Color.WHITE);
setBackground(Color.RED);
}
setText(formatter.format(date));
} else {
setText(null);
}
return this;
}
protected boolean hasBookingFor(LocalDate date) {
for (String[] data : bookings) {
int day = Integer.parseInt(data[2]);
int month = Integer.parseInt(data[3]);
int year = 2017; // Because :P
LocalDate booking = LocalDate.of(year, month, day);
if (booking.isEqual(date)) {
return true;
}
}
return false;
}
}
Basically, this allows you to pass the booking information to the TableCellRenderer, as I've stated, this is NOT how you really should be doing this, but it would require a significant restructure of your code to make it work properly.
Now, I create a TableModel which basically takes a year and month value and returns a LocalDate for each cell (or null if the cell is out of the month range)
public class CalendarModel extends AbstractTableModel {
public static String[] COLUMN_NAMES = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
private int rows = 0;
private LocalDate startOfCalendar;
private LocalDate firstDayOfMonth;
private LocalDate lastDayOfMonth;
public CalendarModel(int year, Month month) {
firstDayOfMonth = LocalDate.of(year, month, 1);
startOfCalendar = firstDayOfMonth.minusDays(firstDayOfMonth.getDayOfWeek().getValue());
lastDayOfMonth = firstDayOfMonth.with(TemporalAdjusters.lastDayOfMonth());
System.out.println(startOfCalendar.getDayOfWeek());
System.out.println(firstDayOfMonth);
System.out.println(lastDayOfMonth);
Duration between = Duration.between(startOfCalendar.atStartOfDay(), lastDayOfMonth.atStartOfDay());
long days = between.toDays();
rows = (int) Math.round(days / 7d) + 1;
}
#Override
public int getRowCount() {
return rows;
}
#Override
public int getColumnCount() {
return 7;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return LocalDate.class;
}
#Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
LocalDate date = null;
if (startOfCalendar != null) {
int day = (rowIndex * 7) + columnIndex;
date = startOfCalendar.plusDays(day);
if (date.isBefore(firstDayOfMonth) || date.isAfter(lastDayOfMonth)) {
date = null;
}
}
return date;
}
}
This means that the TableCellRenderer is been passed either a null value of LocalDate value, with this information, you then need to search your array for any possible bookings for the specified date.
This will scale horribly, which is why I've avoided doing and kept trying to get you to change how you manage your data, but here it is
And finally a really rough example...
This example doesn't really care for all the information you'll be managing, it will only care about the month and day information
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.time.Duration;
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
String[][] bookings = new String[7][6];
bookings[0][2] = "5";
bookings[0][3] = "4";
bookings[1][2] = "10";
bookings[1][3] = "4";
bookings[2][2] = "15";
bookings[2][3] = "4";
bookings[3][2] = "20";
bookings[3][3] = "4";
bookings[4][2] = "25";
bookings[4][3] = "4";
bookings[5][2] = "30";
bookings[5][3] = "4";
bookings[6][2] = "5";
bookings[6][3] = "5";
TableModel model = new CalendarModel(2017, Month.APRIL);
JTable table = new JTable(model);
table.setDefaultRenderer(LocalDate.class, new LocalDateTableCellRenderer(bookings));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
You have to set the cell renderer for every column of your JTable.
I hope this example can help you:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class JTableTest
{
public static void main( String[] args )
{
SwingUtilities.invokeLater( new Runnable()
{
#Override
public void run()
{
buildUI();
}
} );
}
public static void buildUI()
{
final int w = 500;
final int h = 200;
Object colNames[] =
{
"COL1", "COL2", "COL3"
};
Object[][] data =
{
};
DefaultTableModel dtm = new DefaultTableModel( data, colNames );
dtm.addRow( new Object[]
{
"a", "b", "c"
} );
dtm.addRow( new Object[]
{
"d", "e", "f"
} );
dtm.addRow( new Object[]
{
"g", "h", "i"
} );
dtm.addRow( new Object[]
{
"l", "m", "n"
} );
final JTable t = new JTable( dtm );
final TableColour tce = new TableColour();
t.getColumnModel().getColumn( 0 ).setCellRenderer( tce );
t.getColumnModel().getColumn( 1 ).setCellRenderer( tce );
t.getColumnModel().getColumn( 2 ).setCellRenderer( tce );
final JFrame f = new JFrame();
f.setBounds( 0, 0, w, h );
JScrollPane sp = new JScrollPane( t );
f.getContentPane().add( sp );
f.setVisible( true );
}
}
class TableColour
extends javax.swing.table.DefaultTableCellRenderer
{
#Override
public java.awt.Component getTableCellRendererComponent( javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column )
{
java.awt.Component cellComponent = super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
cellComponent.setBackground( java.awt.Color.RED );
return cellComponent;
}
}
I am using DefaultTableModel as follows:
DefaultTableModel model = new DefaultTableModel (COLUMNS, 0 )
{
#Override
public boolean isCellEditable(int row, int column)
{
return (getColumnName(column).equals("Selected"));
}
public Class getColumnClass(int columnIndex)
{
if(getColumnName(columnIndex).equals("Selected"))
return Boolean.class;
return super.getColumnClass(columnIndex);
}
};
Now I want to make only one checkbox selectable in the column "Selected". How can this be done. I have tried following method also but its not working.
public void fireTableCellUpdated(int row,int column)
{
if(getColumnName(column).equals("Selected"))
{
for(int i = 0; i<getRowCount() && i!=row;i++)
setValueAt(Boolean.FALSE, row, column);
}
}
#eatSleepCode wrote #mKorbel can you please give example code for implementing setValueAt method.
code for (OP used) DefaultTableModel,
for code based on AbstractTableModel is required to hold code ordering for notifier fireTableCellUpdated(rowIndex, columnIndex);, because/otherwise nothing will be repainted in JTables view,
there are a few important differencies betweens those two models and its notifiers, and (my view) there isn't reason to bothering with and to use AbstractTableModel for basic stuff (99pct of table models)
. . . . . . . .
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
public class TableRolloverDemo {
private JFrame frame = new JFrame("TableRolloverDemo");
private JTable table = new JTable();
private String[] columnNames = new String[]{"Column"};
private Object[][] data = new Object[][]{{false}, {false}, {true}, {true},
{false}, {false}, {true}, {true}, {false}, {false}, {true}, {true}};
public TableRolloverDemo() {
final DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private boolean ImInLoop = false;
#Override
public Class<?> getColumnClass(int columnIndex) {
return Boolean.class;
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (columnIndex == 0) {
if (!ImInLoop) {
ImInLoop = true;
Boolean bol = (Boolean) aValue;
super.setValueAt(aValue, rowIndex, columnIndex);
for (int i = 0; i < this.getRowCount(); i++) {
if (i != rowIndex) {
super.setValueAt(!bol, i, columnIndex);
}
}
ImInLoop = false;
}
} else {
super.setValueAt(aValue, rowIndex, columnIndex);
}
}
};
table.setModel(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
TableRolloverDemo tableRolloverDemo = new TableRolloverDemo();
}
});
}
}
You get an stack overflow exception because setValueAt() method triggers fireTableCellUpdated() method once again and again.
Instead, try using a table listener which would listen to check box's value change and would set all other check boxes' value to false.
You can create your own custom cell editor that joins all check boxes in a column in a ButtonGroup. here's how:
public class VeryComplicatedCellEditor extends DefaultCellEditor {
private ArrayList<ButtonGroup> groups;
public getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JCheckBox checkBox = new JCheckBox();
growToSize(column);
groups.get(column).add(checkBox);
return checkBox;
}
private growToSize(int size) {
groups.ensureCapacity(size);
while (groups.size() < size)
groups.add(new ButtonGroup());
}
}
There are some complications that come from the fact that we don't know how big the table is, which are mostly taken care of in the growToSize method.
The way this works is by maintaining a list of ButtonGroups, one for each column. The editor component for each cell is added to the button group for its column.
I have here a JTable with two(2) columns. The right column is an editable one while the other is not.
So, what my problem is that whenever the user changed the value of a cell, that specific cell will changed its cell color.
I wanna do this because I want to let the user know that he/she made some changes in the table.
I found this somewhere and it somehow solved my problem but 1 thing that didn't come up with my expectation is that after changing the value and clicked another cell, the color changes back to its original color. I want to let it stay until it is saved.
#Override
public Component prepareEditor(TableCellEditor editor, int data, int columns) {
Component c = super.prepareEditor(editor, data, columns);
c.setBackground(Color.RED);
return c;
}
Is it possible? If yes, please show some example.
UPDATE:
String[] columnname = {"Student Name", "Grade"};
Object[][] data = {};
gradetable = new JTable(data, columnname){
private Object[][] rowData;
public boolean isCellEditable(int data, int columns){
return columns == 1;
}
public Component prepareRenderer(TableCellRenderer r, int data, int columns){
final Component c = super.prepareRenderer(r, data, columns);
if (data % 2 == 0){
c.setBackground(Color.LIGHT_GRAY);
}
else{
c.setBackground(Color.WHITE);
}
if (isCellSelected(data, columns)){
c.setBackground(Color.ORANGE);
}
return c;
}
#Override
public Component prepareEditor(TableCellEditor editor, int data, int columns) {
Component c = super.prepareEditor(editor, data, columns);
c.setBackground(Color.RED);
return c;
}
};
gradetable.setModel(new DefaultTableModel(data, columnname));
gradetable.setPreferredScrollableViewportSize(new Dimension (350, 130));
gradetable.setFillsViewportHeight(true);
gradetable.getTableHeader().setReorderingAllowed(false);
gradetable.setGridColor(new Color(128,128,128,128));
JScrollPane jsp = new JScrollPane(gradetable);
panel3.add(jsp);
Tables use a TableCellRenderer to paint values on the screen. The editors and renderers don't actually have anything to do with each other (from a painting point of view).
So once the editor has been dismissed (accepted or cancelled), the cell is repainted using the assigned TableCellRenderer
You need to supply, in your table model, some way to determine which rows have been updated and change the state of the renderer to match.
FYI- The DefaultTableCellRenderer uses a JLabel as it's base renderer, so it is transparent by default; you will need to make it opaque to make it render properly.
Check out Using custom renderers for more details
Update with example
This is nothing more then a proof of concept. It will not meet your absolute requirements and you should take a serious look at the tutorial linked above.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class TableEdit {
public static void main(String[] args) {
new TableEdit();
}
public TableEdit() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JTable table = new JTable(new MyTableModel());
table.setSurrendersFocusOnKeystroke(true);
TableColumnModel model = table.getColumnModel();
model.getColumn(1).setCellRenderer(new MyTableCellRenderer());
add(new JScrollPane(table));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class MyData {
private String key;
private String value;
private boolean changed;
public MyData(String key, String value) {
this.key = key;
this.value = value;
this.changed = false;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
public void setValue(String newValue) {
if (value == null ? newValue != null : !value.equals(newValue)) {
value = newValue;
changed = true;
}
}
public boolean hasChanged() {
return changed;
}
}
public class MyTableModel extends AbstractTableModel {
private List<MyData> data;
public MyTableModel() {
data = new ArrayList<>(25);
for (int index = 0; index < 5; index++) {
data.add(new MyData("A" + (index + 1), "B" + (index + 1)));
}
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public int getColumnCount() {
return 2;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
MyData myData = data.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = myData.getKey();
break;
case 1:
value = myData.getValue();
break;
}
return value;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}
public boolean hasChanged(int rowIndex) {
MyData myData = data.get(rowIndex);
return myData.hasChanged();
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
MyData myData = data.get(rowIndex);
myData.setValue(aValue == null ? null : aValue.toString());
}
}
public class MyTableCellRenderer extends DefaultTableCellRenderer {
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setOpaque(isSelected);
TableModel model = table.getModel();
if (model instanceof MyTableModel) {
MyTableModel myModel = (MyTableModel) model;
if (myModel.hasChanged(row)) {
if (!isSelected) {
setBackground(Color.RED);
setOpaque(true);
}
}
}
return this;
}
}
}
I would like to set the color of a cell based on the value of the cell. Having googled around for a bit i found out that i can do it using something like this:
public class TableCellRenderer extends DefaultTableCellRenderer {
#Override
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int col)
{
// get the DefaultCellRenderer to give you the basic component
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
// apply your rules
if (value.toString().equals("Red"))
c.setBackground(Color.RED);
else
c.setBackground(Color.GRAY);
return c;
}
}
The problem i have though is that the code i would like to modify is already setting the TableCellRendererer for the columns of the JTable. There is a function in the code that looks like this:
private void configureTableColumns() {
Enumeration columns = this.table.getColumnModel().getColumns();
while (columns.hasMoreElements()) {
TableColumn tableColumn = (TableColumn) columns.nextElement();
this.setCellRenderer(tableColumn);
TableCellRenderer renderer = tableColumn.getCellRenderer();
if (renderer == null) {
renderer = this.table.getDefaultRenderer(Object.class);
}
tableColumn.setCellRenderer(renderer);
this.setCellEditor(tableColumn);
}
}
With the above code, do i still need to add the TableCellRenderer class shown previously? All i want to do is to check if the value of the cell is 'ABC' and set the background to RED.
Update:
I did try adding my version of the TableCellRenderer as an inner class in the code i want to modify but i get an error that there is a type mismatch at tableColumn.getCellRenderer().
Type mismatch: cannot convert from TableCellRenderer to MyTableExample.TableCellRenderer
Thanks
Override prepareRenderer method for doing that.
Example:
public Component prepareRenderer (TableCellRenderer renderer, int rowIndex, int columnIndex){
Component componenet = super.prepareRenderer(renderer, rowIndex, columnIndex);
if(getValueAt(rowIndex, columnIndex).toString().equals("Red")) {
componenet.setBackground(Color.RED);
} else if(getValueAt(rowIndex, columnIndex).toString().equals("Green")) {
componenet.setBackground(Color.GREEN);
}
return componenet;
}
UPDATE:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class PrepareRendereEx {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(400, 100));
Object data[][] = { {"java", "j2ee"},
{"java", "j2ee"},
{"java", "j2ee"}
};
Object[] columnNames = {"Java", "J2EE"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model) {
#Override
public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
int columnIndex) {
JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex);
if(getValueAt(rowIndex, 0).toString().equalsIgnoreCase("java") && columnIndex == 0) {
component.setBackground(Color.RED);
} else if(getValueAt(rowIndex, 1).toString().equalsIgnoreCase("j2ee") && columnIndex == 1){
component.setBackground(Color.GREEN);
}
return component;
}
};
frame.add(new JScrollPane(table));
frame.setVisible(true);
}
}
OUTPUT: