import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;

import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.awt.*;

import java.util.Vector;


// Tabelle mit einer Klasse
// Listener in der Spalte der U-Nummern
// zeilenweise highlight, bActivate
// Klasse item kapselt den Eintrag mit bActivate

public class Tabelle06 extends JFrame {


   Vector liste = new Vector();
   JTable tabelle;

   final String[]
   names = {"Vorname", "Nachname", "MatrNr", "UNumer", "Farbe"};

  public Tabelle06() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(100,100);
    this.setSize(550, 350);
    this.setTitle("Tabelle06");
    setGUI();
  }


    private void setGUI() {
      setdata(); // erstellt den Vector und füllt ihn
      tabelle = createTable();
      JScrollPane scrollpane = new JScrollPane(tabelle);
      scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
      scrollpane.setPreferredSize(new Dimension(430, 200));
      this.getContentPane().add(scrollpane, BorderLayout.CENTER);
      //this.pack();
    } // setGUI


  // createTable
  public JTable createTable() {
    // Create a model of the data.
        TableModel dataModel = new AbstractTableModel() {
            // Diese Methode muss immer implementiert werden
            public int getColumnCount() {
              return names.length;
            }
            // Diese Methode muss immer implementiert werden
            public int getRowCount() {
              return liste.size();
            }

            // Diese Methode muss immer implementiert werden
            public Object getValueAt(int row, int col) {
              myData data = (myData) liste.elementAt(row);
              Item  it;
              switch (col) {
                case 0: return new Item(
            data.getVorname(),data.getActivate()==1 );
                case 1: return new Item(data.getNachname(),data.getActivate()==2 );
                case 2: return new Item(Integer.toString(data.getMatrNummer()),
                            data.getActivate()==4 );
                case 3: return new Item(data.getUNummer(),data.getActivate()==8 );
                case 4: return new Item(data.getFarbe(),data.getActivate()==16 );
                default : return "XXXXXX";
              } // case

             }

            public String getColumnName(int column) {
              return names[column];
            }
            public Class getColumnClass(int c) {
              return getValueAt(0, c).getClass();
            }
            public boolean isCellEditable(int row, int col) {
              return true;
            }
            public int getColumnAlignment(int column) {   
              int[] alignmentArray = {// -1 links, 0 center, 1 rechts
                   	-1 , -1 , 1 , -1}; 
              return 1; // alignmentArray[column];
            }

            public void setValueAt(Object aValue, int row, int column) {
              myData data = (myData) liste.elementAt(row);
              switch( column ) {
                case 0: data.setVorname( (String) aValue );
                case 1: data.setNachname( (String) aValue );
                case 2: //data.setMatrNummer( aValue );
                case 3: data.setUNummer( (String) aValue );
                default: System.out.println("case Fall, setValue");
              }
            }
         };

     JTable table = new JTable(dataModel);
     table.setFont( new Font("Arial", Font.BOLD, 18) );

   // names = {"Vorname", "Nachname", "MatrNr", "UNumer", "Farbe"};


     DefaultTableCellRenderer numberColumnRenderer
          = new DefaultTableCellRenderer() {
          public void setValue(Object value) {
              Item it;
              boolean bActivate;
              if (value instanceof Item) {
                it = (Item) value;
                bActivate = it.activate;
                if (bActivate)
                  System.out.println("true "+it.Name);
                else
                  System.out.println("false    "+it.Name);
              }
              else {
                bActivate=false;
                System.out.println("xx");
              }
              // cellValue = (value instanceof Number) ? ((Number)value).intValue() : 0;

              if (bActivate) {
                //setForeground(Color.black);
                setBackground(Color.red);
              }
              else {
                setBackground(Color.white);
              }

              setText((value == null) ? "" : value.toString());
            }
           };

            // numberColumnRenderer.setHorizontalAlignment(JLabel.RIGHT);
            // numbersColumn.setPreferredWidth(110);
     for (int jj=0; jj<names.length; jj++) {
        TableColumn numbersColumn = table.getColumn( names[jj] );
        numbersColumn.setCellRenderer(numberColumnRenderer);
     }  // for jj

     return table;
  } // createTable


     // Eintragen
    private void setdata() {
      myData data;

      data = new myData( "Müller","Kai", 4722, "U12345","Yellow", 0);
        liste.add(data);

      data = new myData( "Meyer","Uwe", 4712, "U12345","Red", 0);
        liste.add(data);

      data = new myData( "Schmidt","Petra", 4714, "U12325","Blue", 1);
        liste.add(data);

      data = new myData( "Brian","Adams", 6714, "U13325","Pink",0);
        liste.add(data);

      data = new myData( "Hold","Toni", 6614, "U13345","White", 4);
        liste.add(data);

      data = new myData( "Boll","Toni", 6613, "U13345","Maroon", 0);
        liste.add(data);

      data = new myData( "Seneca","Toni", 6314, "U13341","Gray", 0);
        liste.add(data);

      data = new myData( "Kredel","Helmut", 1613, "U23345","Phthalo Blue", 0);
        liste.add(data);


      data = new myData( "Bräunl","Thomas", 1623, "U23315","Maroon", 2);
        liste.add(data);
      data = new myData( "Doug","Lea", 2613, "U11345","Blue", 0);
        liste.add(data);
      data = new myData( "Rauber","Rünger", 1612, "U23344","Red", 0);
        liste.add(data);
      data = new myData( "Walrath","Kathy", 1613, "U23345","Green", 0);
        liste.add(data);
      data = new myData( "Weber","Harald", 1613, "U23345","Black", 0);
        liste.add(data);

    }


    public static void main(String[] args) {
      Tabelle06 frame = new Tabelle06();
      frame.setVisible(true);
    }

}

// 1 = Vorname
// 2 = Nachname
// 4 = matrikelnummer
// 8 = UNummer
class Item {
    public String Name;
    public boolean activate;

    public Item(String Name, boolean activate) {
      this.Name = Name;
      this.activate = activate;
    }

    public String toString() {
      return Name;
    }
}

class myData {
    private String _Name;
    private String _Vorname;
    private int _MatrNummer;
    private String _UNummer;
    private String _Farbe;

    private int _activate;

    public myData() {
    }

    public int getColumns() {
      return 5;
    }

    public myData( String Name, String Vorname, int MatrNummer,
         String UNummer, String Farbe, int activate) {
      _Name = Name;
      _Vorname = Vorname;
      _MatrNummer = MatrNummer;
      _UNummer = UNummer;
      _Farbe = Farbe;
      _activate = activate;
    }


    public int getActivate() {
       return _activate;
    }

   public String getNachname() {
     return _Name;
   }
   public void setNachname(String Name) {
     _Name = Name;
   }


   public String getVorname() {
     return _Vorname;
   }
   public void setVorname(String Vorname) {
     _Vorname = Vorname;
   }

   public int getMatrNummer() {
     return _MatrNummer;
   }
   public void setMatrNummer(int MatrNummer) {
     _MatrNummer = MatrNummer;
   }


   public String getUNummer() {
     return _UNummer;
   }
   public void setUNummer(String UNummer) {
     _UNummer = UNummer;
   }


   public String getFarbe() {
     return _Farbe;
   }
   public void setFarbe(String Farbe) {
     _Farbe = Farbe;
   }

}  // myData


