// setAutoCreateRowSorter(true)
import java.awt.*;

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.util.Vector;

// Schalter Event
import java.awt.event.*;


// setAutoCreateRowSorter(true)

// Tabelle mit einer Klasse  und Sortierung

public class Tabelle08 extends JFrame {

   JTable _tabelle;
   Vector _liste = new Vector();

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

    public Tabelle08() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setGUI();
      setSize(600,400);
      setTitle("Tabelle08.java");
    }



    private void setGUI() {
        this.getContentPane().setLayout( new BorderLayout() );
        setdata();
        _tabelle = createTable();
        _tabelle.setFont( new Font("Arial", Font.BOLD,18)  );


        // Finish setting up the table.
        JScrollPane scrollpane = new JScrollPane(_tabelle);
      	scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));

        

        this.getContentPane().add(scrollpane, BorderLayout.CENTER);
        // this.pack();
       
      
    }


  // createTable
  public JTable createTable() {
    // Create a model of the data.
        TableModel dataModel = new AbstractTableModel() {
            // These methods always need to be implemented.
            public int getColumnCount() {
              return names.length;
            }
            public int getRowCount() {
              return _liste.size();
            }

            public Object getValueAt(int row, int col) {
              Student std = (Student) _liste.elementAt(row);
             // return data.returnTableData(col);

              switch (col) {
                case 0: return std.getVorname();
                case 1: return std.getName();
                case 2: return std.getMatrNummer();
                case 3: return std.getUNummer();
                case 4: return std.getFarbe();
                default: return "xxxxx"+Integer.toString(col);
              }
            }

            // The default implementations of these methods in
              // AbstractTableModel would work, but we can refine them.
            public String getColumnName(int column) {
              return names[column];
            }
            public Class getColumnClass(int column) {
              return getValueAt(0, column).getClass();
            }
            public boolean isCellEditable(int row, int column) {
              return true;
            }
            public void setValueAt(Object aValue, int row, int column) {
              Student std = (Student) _liste.elementAt(row);
              switch( column ) {
                case 0: std.setName( (String) aValue );
                case 1: std.setVorname( (String) aValue );
                case 2: Integer I = Integer.parseInt( (String) aValue);
                        std.setMatrNummer( I.intValue() );
                case 3: std.setUNummer( (String) aValue );
              }
            }
         };

     JTable table = new JTable(dataModel);

    table.setAutoCreateRowSorter(true);

        return table;
  } // createTable

      

     // Eintragen
    private void setdata() {
      Student std;

      std = new Student( "Müller","Kai", 4722, "U12345","Yellow");
        _liste.add(std);

      std = new Student( "Meyer","Uwe", 4712, "U12345","Red");
        _liste.add(std);

      std = new Student( "Schmidt","Petra", 4714, "U12325","Blue");
        _liste.add(std);

      std = new Student( "Brian","Adams", 6714, "U13325","Pink");
        _liste.add(std);

      std = new Student( "Hold","Toni", 6614, "U13345","White");
        _liste.add(std);

      std = new Student( "Boll","Toni", 6613, "U13345","Maroon");
        _liste.add(std);

      std = new Student( "Seneca","Toni", 6314, "U13341","Gray");
        _liste.add(std);

      std = new Student( "Kredel","Helmut", 8613, "U23345","Phthalo Blue");
        _liste.add(std);


      std = new Student( "Bräunl","Thomas", 8700, "U23315","Maroon");
        _liste.add(std);
      std = new Student( "Doug","Lea", 3613, "U11345","Blue");
        _liste.add(std);
      std = new Student( "Rauber","Rünger", 5612, "U53344","Red");
        _liste.add(std);
      std = new Student( "Walrath","Kathy", 4613, "U33345","Green");
        _liste.add(std);
      std = new Student( "Weber","Harald", 9613, "U23345","Black");
        _liste.add(std);

    }



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

}



class Student  {

    private String _Name;
    private String _Vorname;
    private int _MatrNummer;
    private String _UNummer;
    private String _Farbe;

    public Student() {
    }

    public int getColumns() {
      return 5;
    }

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



   public String getName() {
     return _Name;
   }
   public void setName(String Name) {
     _Name = Name;
   }


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

   public String getMatrNummer() {
     return Integer.toString(_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;
   }

}  // Student





