package table;
/*
* Kreuz_Tabelle.java
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.border.*;
public class Kreuz_Tabelle extends JFrame implements ListSelectionListener{
private final String WERTLOS = "-----";
private JTable table;
private JToolBar toolbar;
private JButton redBtn, whiteBtn;
private HashMap<String, String> colorMap = new HashMap<String, String>();
private final Color redSelected = new Color(255,150,150);
public Kreuz_Tabelle() {
super("Kreuztabelle");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,300);
setLocationRelativeTo(null);
table = new JTable() {
public Component prepareRenderer(
final TableCellRenderer renderer, final int row, final int column) {
Component c = super.prepareRenderer(renderer, row, column);
String key = row+":"+column;
c.setBackground(Color.white );
boolean selected = isRowSelected(row) && isColumnSelected(column);
if( selected ){
if( table.getValueAt(row,column).toString().equals(WERTLOS)){
c.setBackground(table.getBackground());
((JComponent)c).setBorder(new LineBorder(table.getBackground()));
}else{
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;
}
};
table.setModel(new DefaultTableModel(
new Object [][] {
{"Jan", "50", "55", "50"},
{"Feb", "-----", "50", "50"},
{"Mär", "55", "-----", "-----"},
{"Apr", "40", "50", "55"},
{"Mai", "35", "40", "35"},
{"Jun", "40", "45", "45"},
{"Jul", "50", "50", "55"},
{"Aug", "-----", "55", "-----"},
{"Sep", "-----", "-----", "55"},
{"Okt", "50", "55", "45"},
{"Nov", "70", "65", "75"},
{"Dez", "75", "70", "65"}
},
new String [] {
"Jahr:", "2001", "2002", "2003"
}
) {
Class[] types = new Class [] {
String.class, String.class, String.class, String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false
};
public Class getColumnClass(final int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(final int rowIndex, final int columnIndex) {
return canEdit [columnIndex];
}
});
table.setCellSelectionEnabled(true);
table.getColumnModel().getSelectionModel().addListSelectionListener(this);
//table.setTableHeader(null);
table.getTableHeader().setReorderingAllowed(false);
getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
toolbar = new JToolBar();
redBtn = new JButton();
toolbar.setFloatable(false);
redBtn.setBackground(Color.red);
redBtn.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
colorButtonActionPerformed("red");
}
});
toolbar.add(redBtn);
whiteBtn = new JButton();
toolbar.setFloatable(false);
whiteBtn.setBackground(Color.white);
whiteBtn.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
colorButtonActionPerformed("white");
}
});
toolbar.add(whiteBtn);
getContentPane().add(toolbar, BorderLayout.NORTH);
}
public void valueChanged(final ListSelectionEvent e) {
ListSelectionModel source = (ListSelectionModel)e.getSource();
if(source.getMinSelectionIndex() == 0)
source.setSelectionInterval(1, source.getMaxSelectionIndex());
}
private void colorButtonActionPerformed(final String color) {
int[] row = table.getSelectedRows();
int[] column = table.getSelectedColumns();
for (int i = 0; i < row.length; i++) {
for (int j = 0; j < column.length; j++) {
String key = row[i]+":"+column[j];
String value = table.getValueAt(row[i],column[j]).toString();
if(!value.equals(WERTLOS))
colorMap.put(key, color);
}
}
table.updateUI();
}
public static void main(final String args[]) { new Kreuz_Tabelle().setVisible(true); }
}