import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.JPanel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import java.awt.Dimension;
import java.awt.Component;
import javax.swing.BoxLayout;
public class Tableau extends JPanel
implements TableModelListener {
private JTable table;
private boolean DEBUG = false;
public Tableau() {
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
//Add another button
JButton rechnenButton = new JButton("rechnen");
rechnenButton.setAlignmentX(Component.CENTER_ALIGNMENT);
final AbstractTableModel model = new MyTableModel();
rechnenButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int column = table.convertColumnIndexToModel(table.getSelectedColumn());
String currentValue=(String)model.getValueAt(row, column);
System.out.println(currentValue);
model.setValueAt(new Integer(5), row, column);
}
});
add(rechnenButton);
table.getModel().addTableModelListener(this);
}
class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"U",
"A",
"1",
"2",
"3",
"4",
"B",
"C",
"D",
"E",
"F"};
private Object[][] data = {
{"1", " ","", "1","5","4","","","","",""},
{"2", " ","4", "","6" ,"5","","","","",""},
{"3", " ","5","8", "","5","","","","",""},
{"4", " ","4", "4","5","","","","","",""} };
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public void setValueAt(Object obj, int r, int c) {
System.out.println(r+" "+c);
data[r][ c] = obj.toString();
System.out.println(data[r][ c]);
fireTableCellUpdated(r, c);
}
public boolean isCellEditable(int row, int col) {
if (col==2&&row==0) {
return false;
}
if (col==3&&row==1) {
return false;
}
if (col==4&&row==2) {
return false;
}
if (col==5&&row==3) {
return false;
}
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 1) {
return false;
} else {
return true;
}
}
}
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
//Objekt wird an array1 weitergegeben
String s = data.toString();
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TablePrintDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
Tableau newContentPane = new Tableau();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}