dataModelPlayers = new AbstractTableModel() { public int getColumnCount() { return overskrifterPlayers.size(); } public int getRowCount() { return players.size();} public Object getValueAt(int row, int col) { return ((Vector)players.elementAt(row)).elementAt(col); } public String getColumnName(int column) {return (String)overskrifterPlayers.get(column);} public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int col) {return true;} public void setValueAt(Object aValue, int row, int column) { //beskeder[row][column] = aValue; } }; tableViewPlayers = new JTable(dataModelPlayers); tableViewPlayers.addMouseListener(this); }
På et senere tidspunkt ændrer jeg indholdet af vectoren players, så vil jeg gerne have at det også giver et udslag på min brugergrænseflade, jeg repainter brugergrænseflade som en besat, men det hjælper ikke...
Hvis du f.eks. har en metode hvor du ændrer vectoren players kan du kalde fireTableDataChanged() eller en af de mere specialiserede fireTable*() metoder. Eks.:
public void vectorChanged() { // denne kode ændrer vector players fireTableDataChanged(); }
public SimpleTabelModel( int rows, int columns ) { super(); columnNames = new Vector( columns ); for ( int j = 0; j < columns; j++ ) columnNames.addElement( new String( DEFAULT_COLUMN_NAME ) ); data = new Vector( rows ); for ( int i = 0; i < rows; i++ ) { Vector v = new Vector( columns ); data.addElement( v ); for ( int j = 0; j < columns; j++ ) v.addElement( "" ); } lastEditedValue = ""; editable = true; JFrame frame = new JFrame(); frame.setSize( 600, 300 ); Container c = frame.getContentPane(); c.setLayout( new FlowLayout() ); _fButton.addActionListener( this ); JTable _lJTabel = new JTable( this ); c.add( _lJTabel ); c.add( _fButton ); frame.show(); frame.setDefaultCloseOperation( 3 ); }
public SimpleTabelModel() { this( 0, 0 ); }
public int getRowCount() { return data.size(); }
public int getColumnCount() { if ( data.size() == 0 ) return 0; else return ( ( Vector ) data.elementAt( 0 ) ).size(); }
public String getColumnName( int columnIndex ) { return ( String ) columnNames.elementAt( columnIndex ); }
public void setColumnName( String name, int columnIndex ) { columnNames.setElementAt( name, columnIndex ); fireTableStructureChanged(); }
public Class getColumnClass( int c ) { return ( new String() ).getClass(); }
public Object getValueAt( int rowIndex, int columnIndex ) { return ( ( Vector ) data.elementAt( rowIndex ) ).elementAt( columnIndex ); }
public synchronized void insertRows( int firstRowIndex, int count ) { int rows = data.size(); int cols = getColumnCount(); if ( firstRowIndex > rows ) firstRowIndex = rows; for ( int i = 0; i < count; i++ ) { Vector v = new Vector( cols ); data.insertElementAt( v, firstRowIndex + i ); for ( int j = 0; j < cols; j++ ) v.addElement( "" ); } fireTableStructureChanged(); }
public synchronized void removeRows( int firstRowIndex, int count ) { int rows = data.size(); int cols = getColumnCount(); if ( firstRowIndex >= rows ) firstRowIndex = rows - 1; if ( firstRowIndex + count > rows ) count = rows - firstRowIndex; for ( int i = 0; i < count; i++ ) data.removeElementAt( firstRowIndex ); fireTableStructureChanged(); }
public synchronized void insertColumns( int firstColumnIndex, int count ) { int rows = data.size(); int cols = getColumnCount(); if ( firstColumnIndex > cols ) firstColumnIndex = cols; for ( int j = 0; j < count; j++ ) columnNames.insertElementAt( new String( DEFAULT_COLUMN_NAME ), firstColumnIndex + j ); for ( int i = 0; i < rows; i++ ) { Vector v = ( Vector ) data.elementAt( i ); for ( int j = 0; j < count; j++ ) v.insertElementAt( "", firstColumnIndex + j ); } fireTableStructureChanged(); }
public synchronized void removeColumns( int firstColumnIndex, int count ) { int rows = data.size(); int cols = getColumnCount(); if ( count != 0 ) { if ( firstColumnIndex > cols ) firstColumnIndex = cols; if ( firstColumnIndex + count > cols ) count = cols - firstColumnIndex; for ( int j = 0; j < count; j++ ) columnNames.removeElementAt( firstColumnIndex ); for ( int i = 0; i < rows; i++ ) { Vector v = ( Vector ) data.elementAt( i ); for ( int j = 0; j < count; j++ ) v.removeElementAt( firstColumnIndex ); } } fireTableStructureChanged(); }
public void fireTableDataChanged() { super.fireTableDataChanged(); }
Vector getDataCopy() { Vector v = ( Vector ) data.clone(); for ( int i = 0; i < v.size(); i++ ) v.setElementAt( ( ( Vector ) v.elementAt( i ) ).clone(), i ); return v; }
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.