JTable Diff
I have 2 Jtables on same JFrame
and i woud like to paint the each Cell in the first Jtable if his matching Cell on the second JTable is diffarent (if both have same row name ie first column starting with A)
any ideas on how to implement that?
Thank you
import java.awt.*;
import java.util.*;
import javax.swing.*;
publicclass TableBasicextends JFrame
{
public TableBasic()
{
String[] columnNames ={"Date","String","Integer","Boolean"};
Object[][] data1 =
{
{"A",new Date(),new Integer(1), Boolean.TRUE},
{"B",new Date(),new Integer(2), Boolean.FALSE},
{"C",new Date(),new Integer(9), Boolean.TRUE},
{"D",new Date(),new Integer(4), Boolean.FALSE}
};
Object[][] data2 =
{
{"A",new Date(),new Integer(3), Boolean.FALSE},
{"B",new Date(),new Integer(2), Boolean.FALSE},
{"M",new Date(),new Integer(9), Boolean.FALSE},
{"D",new Date(),new Integer(4), Boolean.FALSE}
};
JTable table1 =new JTable(data, columnNames);
JTable table2 =new JTable(data, columnNames);
JScrollPane scrollPane1 =new JScrollPane( table1 );
JScrollPane scrollPane2 =new JScrollPane( table2 );
getContentPane().add( scrollPane1 );
getContentPane().add( scrollPane2 );
}
publicstaticvoid main(String[] args)
{
TableBasic frame =new TableBasic();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo(null );
frame.setVisible(true);
}
}
[3743 byte] By [
SharonPla] at [2007-11-27 10:31:17]

# 1
package table;
/*
* TableBasic.java
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableBasic extends JFrame {
private SortedMap<String, String> colorMap = new TreeMap<String, String>();
private final Color redSelected = new Color(255,150,150);
private JTable table2;
private JTable table1;
private JButton btCompare;
public TableBasic() {
super("Comparing Tables");
String[] columnNames = {"String", "Date", "Integer", "Boolean"};
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
Object[][] data1 = null;
Object[][] data2 = null;
try {
data1 = new Object[][]
{
{"A", formatter.parse(formatter.format(new Date())), new Integer(1), Boolean.TRUE },
{"B", formatter.parse(formatter.format(new Date())), new Integer(2), Boolean.FALSE},
{"C", formatter.parse(formatter.format(new Date())), new Integer(9), Boolean.TRUE },
{"D", formatter.parse(formatter.format(new Date())), new Integer(4), Boolean.FALSE}
};
data2 = new Object[][]
{
{"A", formatter.parse(formatter.format(new Date())), new Integer(3), Boolean.FALSE},
{"B", formatter.parse(formatter.format(new Date())), new Integer(2), Boolean.FALSE},
{"M", formatter.parse(formatter.format(new Date())), new Integer(9), Boolean.FALSE},
{"D", formatter.parse(formatter.format(new Date())), new Integer(4), Boolean.FALSE}
};
} catch (ParseException e) {
e.printStackTrace();
}
DefaultTableModel model1 = new DefaultTableModel(data1, columnNames){
private Class[] types = {String.class, Date.class, Integer.class, Boolean.class};
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
};
table1 = new JTable(model1){
public Component prepareRenderer(
final TableCellRenderer renderer, final int row, final int column) {
Component c = super.prepareRenderer(renderer, row, column);
String key = String.valueOf(row)+";"+String.valueOf(column);
c.setBackground(Color.white );
boolean selected = isRowSelected(row) && isColumnSelected(column);
if( selected ){
c.setBackground(Color.cyan);
}
String colorValue = colorMap.get(key);
if( colorValue!=null ){
if( (colorValue).equals("red") ){
c.setBackground(Color.red);
if( selected ){
c.setBackground(redSelected);
}
}
if( (colorValue).equals("white") ) {
c.setBackground(Color.white);
if( selected ){
c.setBackground(Color.cyan);
}
}
}
return c;
}
};
DefaultTableModel model2 = new DefaultTableModel(data2, columnNames){
private Class[] types = {String.class, Date.class, Integer.class, Boolean.class};
public Class getColumnClass(int columnIndex) {
return types[columnIndex];
}
};
table2 = new JTable(model2);
DateColumnFormat dateCF1 = new DateColumnFormat();
table1.setDefaultRenderer(Date.class, dateCF1.getRenderer());
table1.setDefaultEditor(Date.class, dateCF1.getEditor());
DateColumnFormat dateCF2 = new DateColumnFormat();
table2.setDefaultRenderer(Date.class, dateCF2.getRenderer());
table2.setDefaultEditor(Date.class, dateCF2.getEditor());
JScrollPane scrollPane1 = new JScrollPane( table1 );
JScrollPane scrollPane2 = new JScrollPane( table2 );
getContentPane().add( scrollPane1 );
getContentPane().add( scrollPane2 , BorderLayout.EAST);
btCompare = new JButton("Compare");
getContentPane().add(btCompare, BorderLayout.NORTH);
btCompare.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
compareTables();
}
});
}
private void compareTables() {
for (int i = 0; i < table1.getRowCount(); i++) {
String name1 = (String) table1.getValueAt(i, 0);
for (int k = 0; k < table1.getColumnCount(); k++) {
String key = String.valueOf(i)+";"+String.valueOf(k);
colorMap.put(key, "white");
}
int j = 0;
for (; j < table2.getRowCount(); j++) {
String name2 = (String) table2.getValueAt(j, 0);
if(name1.equals(name2)){
for (int k = 0; k < table1.getColumnCount(); k++) {
if(!table1.getValueAt(i, k).equals(table2.getValueAt(j, k))){
Object o1 = table1.getValueAt(i, k);
Object o2 = table2.getValueAt(j, k);
if(o1 instanceof Date){
System.out.println(((Date)o1).getTime());
System.out.println(((Date)o2).getTime());
}
String key = String.valueOf(i)+";"+String.valueOf(k);
colorMap.put(key, "red");
}
}
break;
}
}
if(j >= table2.getRowCount()){
String key = String.valueOf(i)+";"+String.valueOf(0);
colorMap.put(key, "red");
}
}
table1.updateUI();
}
public static void main(final String[] args) {
TableBasic frame = new TableBasic();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
class DateColumnFormat {
private SimpleDateFormat formatterR, formatterE;
private DefaultTableCellRenderer renderer;
private DefaultCellEditor editor;
/**
* Constructs a <code>DateColumnFormat</code> using the pattern "dd/MM/yyyy" and
* the default date format symbols for the default locale.
*/
public DateColumnFormat(){
this(null);
}
/**
* Constructs a <code>DateColumnFormat</code> using the given pattern and
* the default date format symbols for the default locale.
*
*
* @param pattern the pattern describing the date format, "dd/MM/yyyy" if null
*/
public DateColumnFormat(String pattern){
this(pattern, pattern);
}
/**
* Constructs a <code>DateColumnFormat</code> using the given patterns and
* the default date format symbols for the default locale.
*
*
* @param patternR the pattern describing the date format of the renderer, "dd/MM/yyyy" if null
* @param patternE the pattern describing the date format of the editor, "dd/MM/yyyy" if null
*/
public DateColumnFormat(String patternR, String patternE){
if(patternR == null) patternR = "dd/MM/yyyy";
formatterR = new SimpleDateFormat(patternR);
formatterR.setLenient(false);
if(patternE == null) patternE = "dd/MM/yyyy";
formatterE = new SimpleDateFormat(patternE);
formatterE.setLenient(false);
renderer = new DateRenderer();
editor = new DateEditor();
}
/**
* Returns the date cell renderer.
* @return the table date cell renderer
*/
public DefaultTableCellRenderer getRenderer(){
return renderer;
}
/**
* Returns the date cell editor.
* @return the table date cell editor
*/
public DefaultCellEditor getEditor(){
return editor;
}
private class DateRenderer extends DefaultTableCellRenderer {
public DateRenderer() {
super();
}
@Override public void setValue(Object value) {
setText((value == null) ? "" : formatterR.format(value));
}
}
private class DateEditor extends DefaultCellEditor {
public DateEditor() {
super(new JTextField());
}
@Override public boolean stopCellEditing() {
String value = ((JTextField)getComponent()).getText();
if(!value.equals("")) {
try {
formatterE.parse(value);
} catch (ParseException e) {
((JComponent)getComponent()).setBorder(new LineBorder(Color.red));
return false;
}
}
return super.stopCellEditing();
}
@Override public Component getTableCellEditorComponent(final JTable table, final Object value,
final boolean isSelected, final int row, final int column) {
JTextField tf =((JTextField)getComponent());
tf.setBorder(new LineBorder(Color.black));
try {
tf.setText(formatterE.format(value));
} catch (Exception e) {
tf.setText("");
}
return tf;
}
@Override public Object getCellEditorValue() {
try {
Date value = formatterE.parse(((JTextField)getComponent()).getText());
return value;
} catch (ParseException ex) {
return null;
}
}
}
}
Message was edited by:
Andre_Uhres
Message was edited by:
Andre_Uhres