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;

import java.awt.Font;
import java.awt.event.*;

import java.awt.BorderLayout;

// Tabelle mit einer Klasse

public class Tabelle09b extends JFrame {

  JToolBar toolBar;
  JButton Bn[];
  JTable _tabelle;
  TableModel _dataModel;
  Vector _tableColumn;
  JScrollPane _scrollpane;

  Vector liste = new Vector();

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



    public Tabelle09b() {

        setSize(800,400);

        this.getContentPane().setLayout( new BorderLayout() );
        setdata();
        _tabelle = createTable();

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

        _scrollpane.setPreferredSize(new Dimension(430, 200));

        this.getContentPane().add(_scrollpane, BorderLayout.CENTER);

        toolBar = new JToolBar();

        Bn = new JButton[5];
        for (int i=0; i<5; i++) {
          Bn[i] = new JButton( Integer.toString(i+1)+" Spalte" );
          Bn[i].setFont( new Font("Arial", Font.BOLD,28));
          Bn[i].addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(ActionEvent e) {
              Bn1_5_Click(e);
             }
          });
          toolBar.add(Bn[i]);
        }


        this.getContentPane().add(toolBar, BorderLayout.NORTH);

        // this.pack();
    }

  private void Bn1_5_Click(ActionEvent e) {
    for (int i=0; i<5; i++) {
      if (e.getSource() == Bn[i] ) {
        System.out.println("Datamodel VOR Anzahl der Spalten: "+_dataModel.getColumnCount());

        /*
        _tabelle = createTable();

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

        scrollpane.setPreferredSize(new Dimension(430, 200));

        this.getContentPane().add(scrollpane, BorderLayout.SOUTH);
        */

        int oldCols = myData.getColumns();

        int newCols = i+1;
        myData.setAnzCols(newCols);
        if (oldCols<newCols) {
          for (int j=oldCols; j<newCols; j++) {
            TableColumn col = new TableColumn(j);
            _tableColumn.add(col);
            _tabelle.addColumn(col);
          }
        }
        else {
          if (oldCols>newCols) {
            for (int j=oldCols; j>newCols; j--) {
              TableColumn col = (TableColumn) _tableColumn.elementAt(j-1);;
              _tabelle.removeColumn(col);
              _tableColumn.remove(col);
            }
          }
        }

        _tabelle.updateUI();
        _tabelle.paintImmediately(0,0,1000,700);
        System.out.println("Anzahl der Spalten: "+(i+1) );
        System.out.println("Datamodel NACH Anzahl der Spalten: "+_dataModel.getColumnCount());
        _scrollpane.updateUI();
        //_dataModel.
        return;
      }
    }
  }

  // createTable
  public JTable createTable() {
    // Create a model of the data.
    // DefaultTableModel(java.util.Vector data, java.util.Vector columnNames)

        _dataModel = new AbstractTableModel() {
            // These methods always need to be implemented.
            public int getColumnCount() {
              System.out.println("   ******  in getColumnCount: "+myData.getColumns() );
              //fireTableChanged(null);
              return myData.getColumns();
            }
            public int getRowCount() {
              return liste.size();
            }

            public Object getValueAt(int row, int col) {
              myData data = (myData) liste.elementAt(row);
              return data.getData(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 c) {
              return getValueAt(0, c).getClass();
            }
            public boolean isCellEditable(int row, int col) {
              return true;
            }
            /*
            public void setValueAt(Object aValue, int row, int column) {
                System.out.println("Setting value to: " + aValue);
                data[row][column] = aValue;
            }
            */
         };

     // _TableColModel = new TableColumnModel();
     myData.setAnzCols(0);
     JTable table = new JTable(_dataModel); // , _TableColModel);

     myData.setAnzCols(2);
     _tableColumn = new Vector();
     for (int j=0; j<myData.getColumns(); j++) {
         TableColumn col = new TableColumn(j);
         _tableColumn.add(col);
         table.addColumn(col);
     }

     table.setFont( new Font("Arial", Font.PLAIN,24) );
     table.setRowHeight(30);
     table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
     table.setAutoCreateColumnsFromModel(true);


        return table;
  } // createTable






  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }


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

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

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

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

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

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

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

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

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


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

    }


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

}


class myData {
    private String _Name;
    private String _Vorname;
    private int _MatrNummer;
    private String _UNummer;
    private String _Farbe;
    private static int _AnzCols=2;

    public myData() {
    }

    public static int getColumns() {
      return _AnzCols;
    }

    public static void setAnzCols(int AnzCols) {
      _AnzCols = AnzCols;
    }

    public Object getData(int col) {
      if (col>_AnzCols)
        return "xxxx";

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

    public myData( 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 Integer getMatrNummer() {
     return new Integer(_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


/*
    final Object[][] data = {
      {"Mark", "Andrews", "Red",    new Integer(2),   new Boolean(true)},
      {"Tom", "Ball", "Blue",       new Integer(99),  new Boolean(false)},
      {"Alan", "Chung", "Green",    new Integer(838), new Boolean(false)},
      {"Jeff", "Dinkins", "Turquois", new Integer(8), new Boolean(true)},
      {"Amy", "Fowler", "Yellow",   new Integer(3),   new Boolean(false)},
      {"Brian", "Gerhold", "Green", new Integer(0),   new Boolean(false)},
      {"James", "Gosling", "Pink",  new Integer(21),  new Boolean(false)},
      {"David", "Karlton", "Red",   new Integer(1),   new Boolean(false)},
      {"Dave", "Kloba", "Yellow",   new Integer(14),  new Boolean(false)},
      {"Peter", "Korn", "Purple",   new Integer(12),  new Boolean(false)},
      {"Phil", "Milne", "Purple",   new Integer(3),   new Boolean(false)},
      {"Dave", "Moore", "Green",    new Integer(88),  new Boolean(false)},
      {"Hans", "Muller", "Maroon",  new Integer(5),   new Boolean(false)},
      {"Rick", "Levenson", "Blue",  new Integer(2),   new Boolean(false)},
      {"Tim", "Prinzing", "Blue",   new Integer(22),  new Boolean(false)},
      {"Chester", "Rose", "Black",  new Integer(0),   new Boolean(false)},
      {"Ray", "Ryan", "Gray",       new Integer(77),  new Boolean(false)},
      {"Georges", "Saab", "Red",    new Integer(4),   new Boolean(false)},
      {"Willie", "Walker", "Phthalo Blue", new Integer(4), new Boolean(false)},
      {"Kathy", "Walrath", "Blue",  new Integer(8),   new Boolean(false)},
      {"Arnaud", "Weber", "Green",  new Integer(44),  new Boolean(false)}
    };

*/


/*




 public void updateUI() {
        // Update the UIs of the cell renderers, cell editors and header renderers.
        TableColumnModel cm = getColumnModel();
        for(int column = 0; column < cm.getColumnCount(); column++) {
            TableColumn aColumn = cm.getColumn(column);
           // updateSubComponentUI(aColumn.getCellRenderer());
            updateSubComponentUI(aColumn.getCellEditor());
           // updateSubComponentUI(aColumn.getHeaderRenderer());
        }

        // Update the UIs of all the default renderers.
        /*
        Enumeration defaultRenderers = defaultRenderersByColumnClass.elements();
        while (defaultRenderers.hasMoreElements()) {
            updateSubComponentUI(defaultRenderers.nextElement());
        }
   
    public void columnAdded(TableColumnModelEvent e) {
        // If I'm currently editing, then I should stop editing
        if (isEditing()) {
            removeEditor();
        }
        resizeAndRepaint();
    }


    public void columnRemoved(TableColumnModelEvent e) {
        // If I'm currently editing, then I should stop editing
        if (isEditing()) {
            removeEditor();
        }
        resizeAndRepaint();
    }
    

*/
