Hallo, ich möchte gerne einen JTable mit teils verbundenen Zellen.
Ich gehe nun schon seit längeren ein Beispiel von der Uni Dresden durch wobei ich den Code an einer Stelle nicht nachvollziehen kann.
Und zwar wird in einer Klasse (MultiSpanCellTableExample) ein Button mit Listener instanziert.
Mit Klick auf den Button wird dann unter anderem eine Methode (combine) eines Interfaces (CellSpan) aufgerufen. Dieses Interface macht aber gar nichts mit den Übergabeparametern und das Interface wurde auch von keiner Klasse implementiert (also die Methode auch nicht überschrieben). Die Instanz des Interfaces wurde aus einer anderen Klasse (AttributiveCellTableModel) gecastet.
So wie ich das sehe läuft der Aufruf der Methode doch ins leere, jedoch funktioniert das Programm.
Oder steh ich irgendwo total auf dem Schlauch. Ich bitte um Hilfe!
class MultiSpanCellTableExample:
interface CellSpan
class AttributiveCellTableModel
Ich gehe nun schon seit längeren ein Beispiel von der Uni Dresden durch wobei ich den Code an einer Stelle nicht nachvollziehen kann.
Und zwar wird in einer Klasse (MultiSpanCellTableExample) ein Button mit Listener instanziert.
Mit Klick auf den Button wird dann unter anderem eine Methode (combine) eines Interfaces (CellSpan) aufgerufen. Dieses Interface macht aber gar nichts mit den Übergabeparametern und das Interface wurde auch von keiner Klasse implementiert (also die Methode auch nicht überschrieben). Die Instanz des Interfaces wurde aus einer anderen Klasse (AttributiveCellTableModel) gecastet.
So wie ich das sehe läuft der Aufruf der Methode doch ins leere, jedoch funktioniert das Programm.
Oder steh ich irgendwo total auf dem Schlauch. Ich bitte um Hilfe!
class MultiSpanCellTableExample:
Java:
package tablebsp;
/*
* (swing1.1beta3) jfc#96
*/
import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.*;
/**
* @version 1.0 11/26/98
*/
public class MultiSpanCellTableExample extends JFrame {
MultiSpanCellTableExample() {
super( "Multi-Span Cell Example" );
AttributiveCellTableModel ml = new AttributiveCellTableModel(10,6);
// AttributiveCellTableModel ml = new AttributiveCellTableModel(10,6) {
// public Object getValueAt(int row, int col) {
// return "" + row + ","+ col;
// }
// };
final CellSpan cellAtt =(CellSpan)ml.getCellAttribute();
final MultiSpanCellTable table = new MultiSpanCellTable( ml );
JScrollPane scroll = new JScrollPane( table );
JButton b_one = new JButton("Combine");
b_one.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] columns = table.getSelectedColumns();
int[] rows = table.getSelectedRows();
cellAtt.combine(rows,columns);
table.clearSelection();
table.revalidate();
table.repaint();
}
});
JButton b_split = new JButton("Split");
b_split.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int column = table.getSelectedColumn();
int row = table.getSelectedRow();
cellAtt.split(row,column);
table.clearSelection();
table.revalidate();
table.repaint();
}
});
JPanel p_buttons = new JPanel();
p_buttons.setLayout(new GridLayout(2,1));
p_buttons.add(b_one);
p_buttons.add(b_split);
Box box = new Box(BoxLayout.X_AXIS);
box.add(scroll);
box.add(new JSeparator(SwingConstants.HORIZONTAL));
box.add(p_buttons);
getContentPane().add( box );
setSize( 400, 200 );
setVisible(true);
}
public static void main(String[] args) {
MultiSpanCellTableExample frame = new MultiSpanCellTableExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
}
}
interface CellSpan
Java:
package tablebsp;
/*
* (swing1.1beta3)
*
*/
/**
* @version 1.0 11/22/98
*/
public interface CellSpan {
public final int ROW = 0;
public final int COLUMN = 1;
public int[] getSpan(int row, int column);
public void setSpan(int[] span, int row, int column);
public boolean isVisible(int row, int column);
public void combine(int[] rows, int[] columns);
public void split(int row, int column);
}
class AttributiveCellTableModel
Java:
package tablebsp;
/*
* (swing1.1beta3)
*
*/
import java.awt.Dimension;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
/**
* @version 1.0 11/22/98
*/
public class AttributiveCellTableModel extends DefaultTableModel {
protected CellAttribute cellAtt;
public AttributiveCellTableModel() {
this((Vector)null, 0);
}
public AttributiveCellTableModel(int numRows, int numColumns) {
Vector names = new Vector(numColumns);
names.setSize(numColumns);
setColumnIdentifiers(names);
dataVector = new Vector();
setNumRows(numRows);
cellAtt = new DefaultCellAttribute(numRows,numColumns);
}
public AttributiveCellTableModel(Vector columnNames, int numRows) {
setColumnIdentifiers(columnNames);
dataVector = new Vector();
setNumRows(numRows);
cellAtt = new DefaultCellAttribute(numRows,columnNames.size());
}
public AttributiveCellTableModel(Object[] columnNames, int numRows) {
this(convertToVector(columnNames), numRows);
}
public AttributiveCellTableModel(Vector data, Vector columnNames) {
setDataVector(data, columnNames);
}
public AttributiveCellTableModel(Object[][] data, Object[] columnNames) {
setDataVector(data, columnNames);
}
// WAR IM ORGINAL NICHT AUSKOMMENTIERT JEDOCH DANN TOTSCHLEIFE MIT SETCOLUMN... UND SETDATAVECTOR.. jetzt nur eine Zeile geändert
public void setDataVector(Vector newData, Vector columnNames) {
if (newData == null)
throw new IllegalArgumentException("setDataVector() - Null parameter");
dataVector = new Vector(0);
// setColumnIdentifiers(columnNames);
columnIdentifiers = columnNames;
dataVector = newData;
//
cellAtt = new DefaultCellAttribute(dataVector.size(),
columnIdentifiers.size());
newRowsAdded(new TableModelEvent(this, 0, getRowCount()-1,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}
public void addColumn(Object columnName, Vector columnData) {
if (columnName == null)
throw new IllegalArgumentException("addColumn() - null parameter");
columnIdentifiers.addElement(columnName);
int index = 0;
Enumeration enumeration = dataVector.elements();
while (enumeration.hasMoreElements()) {
Object value;
if ((columnData != null) && (index < columnData.size()))
value = columnData.elementAt(index);
else
value = null;
((Vector)enumeration.nextElement()).addElement(value);
index++;
}
//
cellAtt.addColumn();
fireTableStructureChanged();
}
public void addRow(Vector rowData) {
Vector newData = null;
if (rowData == null) {
newData = new Vector(getColumnCount());
}
else {
rowData.setSize(getColumnCount());
}
dataVector.addElement(newData);
//
cellAtt.addRow();
newRowsAdded(new TableModelEvent(this, getRowCount()-1, getRowCount()-1,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}
public void insertRow(int row, Vector rowData) {
if (rowData == null) {
rowData = new Vector(getColumnCount());
}
else {
rowData.setSize(getColumnCount());
}
dataVector.insertElementAt(rowData, row);
//
cellAtt.insertRow(row);
newRowsAdded(new TableModelEvent(this, row, row,
TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
}
public CellAttribute getCellAttribute() {
return cellAtt;
}
public void setCellAttribute(CellAttribute newCellAtt) {
int numColumns = getColumnCount();
int numRows = getRowCount();
if ((newCellAtt.getSize().width != numColumns) ||
(newCellAtt.getSize().height != numRows)) {
newCellAtt.setSize(new Dimension(numRows, numColumns));
}
cellAtt = newCellAtt;
fireTableDataChanged();
}
/*
public void changeCellAttribute(int row, int column, Object command) {
cellAtt.changeAttribute(row, column, command);
}
public void changeCellAttribute(int[] rows, int[] columns, Object command) {
cellAtt.changeAttribute(rows, columns, command);
}
*/
}
Zuletzt bearbeitet: