Verständnisproblem mit Interface

Kalle_Mett

Mitglied
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:
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:

Marco13

Top Contributor
Nur überflogen, aber ... da wird an einigen Stellen mit
cellAtt = new DefaultCellAttribute(numRows,numColumns);
eine Instanz von "DefaultCellAttribute" erzeugt, und die implementiert wohl CellSpan.
 

Kalle_Mett

Mitglied
Vielen Dank erst mal und entschuldige ich habe tatsächlich übersehen, dass die Klasse DefaultCellAtribute das Interface implementiert.
In der combine-Methode oben in der ersten Klasse ist cellAtt aber vom Typ CellSpan. Wird der Wert dann trotzdem in DefaultCellAttribute gespeichert?
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
N Verständnisproblem bei Fehlermeldung bei "showMessage" AWT, Swing, JavaFX & SWT 3
M JavaFX Tooltip bei setOnMouseMoved Verständnisproblem AWT, Swing, JavaFX & SWT 6
S Verständnisproblem mit CellRenderer AWT, Swing, JavaFX & SWT 1
B OpenGl lwjgl Verständnisproblem AWT, Swing, JavaFX & SWT 5
C JTextField und JTextArea - Verständnisproblem AWT, Swing, JavaFX & SWT 14
E JTable und tablemodel Verständnisproblem AWT, Swing, JavaFX & SWT 3
S Implementierung Java Swing HelloWorld - verständnisproblem AWT, Swing, JavaFX & SWT 3
D Verständnisproblem mit Layoutmanagern. AWT, Swing, JavaFX & SWT 7
A Verständnisproblem bei JTable AWT, Swing, JavaFX & SWT 6
M Nutzer interface Programmieren AWT, Swing, JavaFX & SWT 2
kilopack15 Interface mit Layout verknüpfen AWT, Swing, JavaFX & SWT 2
K Fehlermeldung mit Interface AWT, Swing, JavaFX & SWT 9
J Swing Erstellung eines User Interface AWT, Swing, JavaFX & SWT 2
D JavaFX Interface Initializable - Was machen die Parameter in der Methode inizializeable() ? AWT, Swing, JavaFX & SWT 4
G JavaFX Nutzen von Interface Callback<P,R> AWT, Swing, JavaFX & SWT 2
S Interface Printable AWT, Swing, JavaFX & SWT 5
trash Dynamisches Interface AWT, Swing, JavaFX & SWT 6
C Interface für JButtons,... AWT, Swing, JavaFX & SWT 7
A User Interface aufteilen? AWT, Swing, JavaFX & SWT 3
B In eine Konsolenanwendung ein Interface einbinden AWT, Swing, JavaFX & SWT 5
J instanceof von einem Interface und aufrufen der Methoden AWT, Swing, JavaFX & SWT 5
S Sowas wie ein drawable-interface? AWT, Swing, JavaFX & SWT 3
B Scrollable - Wie benutzt man das Interface richtig? AWT, Swing, JavaFX & SWT 5

Ähnliche Java Themen

Neue Themen


Oben