|
Ola amigos,
faco parte da lista a 1 semana
e tenho gostado muito do nivel de discussao que tem aparecido
aqui.
Bom, pra comecar sou novo em
programacao JAVA e SWING, mas tenho uma tarefa de desenvolver um programa. Eu
utilizo o VAJAVA 3.02, JFC 3.02, e JDK 1.1.8
Minha dificuldade eh que estou
tentando trabalhar com um JTable e utilizo um model (MyTableModel) para cria-la
mas nao tenho conseguido :
- Adicionar uma linha com novas informacoes.
-
Alterar em tempo de criacao a largura das colunas.
Tenho uma impressao que eu nao
estou sabendo trabalhar com o modelo da Tabela. Sera que eh este o meu
problema?
Alguem saberia que codigo eu
poderia utilizar para alterar estas informacoes na tabela ou algum codigo de
modelo que poderia estar utilizando?
Segue em anexo o codigo do
modelo.
Desde ja agradeco a atencao de
todos...
Um grande abraco...
Jader Tenguam...
import java.util.Vector;
public class MyTableModel extends com.sun.java.swing.table.AbstractTableModel { final Object[][] data = { {"01", "AMBOS", "Cliente zzz", "29/10/2001","20:30:00","Preview"} }; final String[] columnNames = {
"Coluna 1",
"Coluna 2", "Coluna 3", "Coluna 4", "Coluna 5", "Coluna 6"}; protected java.util.Vector dataVector; }
/**
* Coment�rio do construtor MyTableModel. */ public MyTableModel() { super(); } /** * Add a row to the end of the model. The new row will contain * <b>null</b> values unless <i>rowData</i> is specified. Notification * of the row being added will be generated. * * @param rowData optional data of the row being added */ public void addRow(Object[] rowData) { addRow(convertToVector(rowData)); } /** * Add a row to the end of the model. The new row will contain * <b>null</b> values unless <i>rowData</i> is specified. Notification * of the row being added will be generated. * * @param rowData optional data of the row being added */ public void addRow(Vector rowData) { if (rowData == null) { rowData = new Vector(getColumnCount()); } else { rowData.setSize(getColumnCount()); } dataVector.addElement(rowData);
// Generate
notification
newRowsAdded(new com.sun.java.swing.event.TableModelEvent(this, getRowCount()-1, getRowCount()-1, com.sun.java.swing.event.TableModelEvent.ALL_COLUMNS, com.sun.java.swing.event.TableModelEvent.INSERT)); } // // Protected Methods // /** Returns a Vector that contains the same
objects as the array */
protected static Vector convertToVector(Object[] anArray) { if (anArray == null) return null; Vector v = new
Vector(anArray.length);
for (int i=0; i < anArray.length; i++) { v.addElement(anArray[i]); } return v; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public int getColumnCount() { return columnNames.length; } public String getColumnName(int col) { return columnNames[col]; } public int getRowCount() { return data.length; } public Object getValueAt(int row, int col) { return data[row][col]; } /* * Don't need to implement this method unless your table's * editable. */ public boolean isCellEditable(int row, int col) { //Note that the data/cell address is constant, //no matter where the cell appears onscreen. //if (col < 2) { return false; //} else { // return true; //} } /** * This method will make sure the new rows have the correct number of columns. * It does so using the setSize method in Vector which truncates Vectors * which are too long, and appends nulls if they are too short. * This method also sends out a tableChanged() notification message * to all the listeners. * * @parameter event This TableModelEvent describes where the * rows were added. If <b>null</b> it assumes * all the rows were newly added. * @see #getDataVector() */ public void newRowsAdded(com.sun.java.swing.event.TableModelEvent event) { int start = event.getFirstRow(); int end = event.getLastRow(); if (start < 0) start = 0; if (end < 0) end = getRowCount()-1; // Have to make sure all the new
columns have the correct
// number of columns for (int i = start; i < end; i++) ((Vector)dataVector.elementAt(i)).setSize(getColumnCount()); // Now we send the
notification
fireTableChanged(event); } /* * Don't need to implement this method unless your table's * data can change. */ public void setValueAt(Object value, int row, int col) { data[row][col] = value; fireTableCellUpdated(row, col); } } |
