
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import javax.swing.table.*;

// Tabelle mit data-Array und Schalter zum NeuZeichnen

public class Tabelle03 extends JFrame {
  JToolBar toolBar = new JToolBar();
  JButton BnInsert = new JButton();

  private JTable _tabelle;

  String[] _names;
  Object[][] _data;
  final int AnzRows = 20;
  final int AnzCols = 30;

  //Frame konstruieren
  public Tabelle03() {
    this.setSize(750, 550);
    this.setTitle("Tabelle3");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  //Initialisierung der Komponente
  private void setGUI()  {
    BnInsert.setText("Einfügen der Daten");
    BnInsert.setToolTipText("1. Button Open File");
    BnInsert.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        BnInsert_actionPerformed(e);
      }
    });
    toolBar.add(BnInsert);

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

    JScrollPane Scrollpanel = createTable();
    this.getContentPane().add(Scrollpanel, BorderLayout.CENTER);

     // Position des Fensters
    setLocation(100,100);
  } // setGUI


  // createTable
  public JScrollPane createTable() {
    _names = new String[AnzCols];
    for (int i=0; i<AnzCols; i++) {
      _names[i] = new String( Integer.toString(i+1) );
    }
        // Create the dummy data (a few rows of names)
   _data = new String[ AnzRows][AnzCols];
   for (int r=0; r<AnzRows; r++) {
     for (int c=0; c<AnzCols; c++) {
       _data[r][c] = "X";
     }
   }


    // Create a model of the data.
    TableModel dataModel = new AbstractTableModel() {
        public int getColumnCount() {
            return AnzCols;
        }
        public int getRowCount() {
          return AnzRows;
        }
        public Object getValueAt(int row, int column) {
          return _data[row][column];
        }
        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 getColumnClass(column) == String.class;
        }

        public void setValueAt(Object aValue, int row, int column) {
          _data[row][column] = aValue;
        }
     };


        // Create the table
        _tabelle = new JTable(dataModel);

        // setzen der Zeilenhöhe
        _tabelle.setRowHeight(20);
        _tabelle.setFont( new Font("Arial", Font.BOLD, 18) );

        return new JScrollPane(_tabelle);
  } // createTable


  void BnInsert_actionPerformed(ActionEvent e) {
    java.util.Random random;
    // int i =e.getID();
    random = new java.util.Random();
    for (int r=0; r<AnzRows; r++) {
     for (int c=0; c<AnzCols; c++) {
       if (random.nextBoolean() )
        _data[r][c] = "1";
       else
        _data[r][c] = "0";
     }
   }
   _tabelle.updateUI();  // neu Zeichnen
   System.out.println("JButton1");
 }


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


}
