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

import javax.swing.table.*;

// Tabelle mit Array, Farben werden als String dargestellt

public class Tabelle10 extends JFrame {

  private JScrollPane _panel;
  private javax.swing.JTable tableView;



  //Frame konstruieren
  public Tabelle10() {
    setSize(500,400);
    setLocation(50,50);
    this.setTitle("Tabelle10");
    setGUI();
  }

  //Initialisierung der Komponente
  private void setGUI() {
    this.setSize(new Dimension(500, 400));
    this.setTitle("Tabelle4");

    _panel = new JScrollPane();
    _panel = createTable();
    this.getContentPane().add(_panel, BorderLayout.CENTER);
  }


  private ImageIcon loadIcon(String name, String description) {
   	// String path = "images" + name;
   	String path = name;
    return new ImageIcon(path, description);
  }


  public JScrollPane createTable() {

        // final
    final String[] names = {"First Name", "Last Name", "Favorite Color",
                            "Favorite Sport", "Favorite Number", "Favorite Food"};
    JScrollPane scrollpane;

      // verwendete Bilder
    ImageIcon burger = loadIcon("burger.gif","burger");
    ImageIcon fries = loadIcon("fries.gif","fries");
    ImageIcon softdrink = loadIcon("softdrink.gif","soft drink");
    ImageIcon hotdog = loadIcon("hotdog.gif","hot dog");
    ImageIcon pizza = loadIcon("pizza.gif","pizza");
    ImageIcon icecream = loadIcon("icecream.gif","ice cream");
    ImageIcon pie = loadIcon("pie.gif","pie");
    ImageIcon cake = loadIcon("cake.gif","cake");
    ImageIcon donut = loadIcon("donut.gif","donut");
    ImageIcon treat = loadIcon("treat.gif","treat");
    ImageIcon grapes = loadIcon("grapes.gif","grapes");
    ImageIcon banana = loadIcon("banana.gif","banana");
    ImageIcon watermelon = loadIcon("watermelon.gif","watermelon");
    ImageIcon cantaloupe = loadIcon("cantaloupe.gif","cantaloupe");
    ImageIcon peach = loadIcon("peach.gif","peach");
    ImageIcon broccoli = loadIcon("broccoli.gif","broccoli");
    ImageIcon carrot = loadIcon("carrot.gif","carrot");
    ImageIcon peas = loadIcon("peas.gif","peas");
    ImageIcon corn = loadIcon("corn.gif","corn");
    ImageIcon radish = loadIcon("radish.gif","radish");


        // Create the dummy data (a few rows of names)
        final Object[][] data = {
	  {"Mike", "Albers",        Color.green, "Soccer",    new Integer(44), banana},
	  {"Mark", "Andrews",       Color.red, "Baseball",    new Integer(2), broccoli},
	  {"Tom", "Ball",           Color.blue, "Football",   new Integer(99), burger},
	  {"Alan", "Chung",         Color.green, "Baseball",  new Integer(838), cake},
	  {"Jeff", "Dinkins",       Color.magenta, "Football",new Integer(8), cantaloupe},
	  {"Amy", "Fowler",         Color.yellow, "Hockey",   new Integer(3), carrot},
	  {"Brian", "Gerhold",      Color.green, "Rugby",     new Integer(7), corn},
	  {"James", "Gosling",      Color.pink, "Tennis",     new Integer(21), donut},
	  {"Earl", "Johnson",       Color.green, "Bicycling", new Integer(8), carrot},
	  {"David", "Karlton",      Color.red, "Baseball",    new Integer(1), fries},
	  {"Dave", "Kloba",         Color.yellow, "Football", new Integer(14), grapes},
	  {"Peter", "Korn",         new Color(100, 100, 255), "Scuba Diving", new Integer(12), broccoli},
	  {"Dana", "Miller",        Color.blue, "Ice Skating",new Integer(8), banana},
	  {"Phil", "Milne",         Color.magenta, "Rugby",   new Integer(3), banana},
	  {"Dave", "Moore",         Color.green, "Tennis",    new Integer(88), peach},
	  {"Hans", "Muller",        Color.magenta, "Baseball",new Integer(5), peas},
	  {"Rick", "Levenson",      Color.blue, "Football",   new Integer(2), pie},
	  {"Tim", "Prinzing",       Color.blue, "Baseball",   new Integer(22), pizza},
	  {"Chester", "Rose",       Color.black, "Hockey",    new Integer(0), radish},
	  {"Chris", "Ryan",         Color.black, "None",      new Integer(6), softdrink},
	  {"Ray", "Ryan",           Color.gray, "Football",   new Integer(77), treat},
	  {"Georges", "Saab",       Color.red, "Hockey",      new Integer(4), watermelon},
	  {"Tom", "Santos",         Color.blue, "Football",   new Integer(3), banana},
	  {"Rich", "Schiavi",       Color.blue, "Hockey",     new Integer(4), grapes},
	  {"Nancy", "Schorr",       Color.blue, "Hockey",     new Integer(8), corn},
	  {"Violet", "Scott",       Color.magenta, "Basketball", new Integer(44), grapes},
	  {"Joseph", "Scheuhammer", Color.green, "Hockey",    new Integer(66), corn},
	  {"Jeff", "Shapiro",       Color.black, "Skiing",    new Integer(42), peach},
	  {"Willie", "Walker",      Color.blue, "Hockey",     new Integer(4), banana},
	  {"Kathy", "Walrath",      Color.blue, "Baseball",   new Integer(8), banana},
	  {"Arnaud", "Weber",       Color.green, "Football",  new Integer(993), peach},
	  {"Steve", "Wilson",       Color.green, "Baseball",  new Integer(7), fries}
        };

        // Create a model of the data.
        TableModel dataModel = new AbstractTableModel() {
            public int getColumnCount() {
              return names.length;
            }
            public int getRowCount() {
              return data.length;
            }
            public Object getValueAt(int row, int col) {
              return data[row][col];
            }
              // Header
            public String getColumnName(int column) {
              return names[column];
            }
              // Welche Klasse ist in Spalte c ?
            public Class getColumnClass(int c) {
              return getValueAt(0, c).getClass();
            }
              // Ist die Spalte editiertbar ?
            public boolean isCellEditable(int row, int col) {
              return getColumnClass(col) == String.class;
            }
             // Objekt zurückschreiben
            public void setValueAt(Object aValue, int row, int column) {
              data[row][column] = aValue;
            }
         };


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

        // Show colors by rendering them in their own color.


        DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() {

     	    public void setValue(Object value) {
	          if (value instanceof Color) {
	              Color c = (Color)value;
	              setBackground(c);
	              setForeground( new Color(c.getRGB() ^ 0xffffff ) );
	              setText(c.getRed() + ", " + c.getGreen() + ", " + c.getBlue());
   	        }
    	    }
        };

          // Ausrichtung der Schrift
        colorRenderer.setHorizontalAlignment(JLabel.RIGHT);
        tableView.getColumn("Favorite Color").setCellRenderer(colorRenderer);


        // ---------------------------------


        tableView.setRowHeight(20);

        scrollpane = new JScrollPane(tableView);
        return scrollpane;
  } // 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);
    }
  }


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


}



/*
     	    public void setValue(Object value) {
	          if (value instanceof Color) {
	              Color c = (Color)value;
                setBackground(c);
	              setForeground(Color.black);
	              setText(c.getRed() + ", " + c.getGreen() + ", " + c.getBlue());
   	        }
    	    }
*/
