Ciao a tutti,
Ho una tabella che contiene una lista in ordine alfabetico (tranne le prime 3 colonne dove si possono inserire valori).
Aggiungo una riga scelta da combo e la inserisco nella tabella ( aggRiga() ).
In efetti aggiungo la riga in coda all'array che popola la tabella, lo riordino, e richiamo la tabella
( tabella t = new tabella(); t.Tabella(); ).
Tutto funziona bene, tranne un fastidioso inconveniente, quando chiamo la tabella la barra di scorrimento laterale torna al top.
Mi piacerebbe che la tabella rimanesse dove era (inteso come scroll verticale) al memento dell'inserimento riga.
Suppongo che dovrei evitare di inizializzare la tabella dopo l'inserimento, ma non so come aggiornarla altrimenti.
Qualcuno ha un'idea ?
Grazie.
codice:// TODO class tabella { public class tabella { //<<<<<<< dati() public void dati(String[] colonne,String[][] dati) { ......... popolo arraiDatiTabella } //<<<<<<< aggRiga() public void aggRiga(){ for (int a = 0; a < arraiDatiTabella.length; a++) { if (arraiDatiTabella[a][3] == null) { numListino3=a; break; } } selCodifica = comboCodifica.getSelectedIndex(); for (int ar = 0; ar < 14; ar++) { arraiDatiTabella[numListino3][ar]=arraiDatiCodifica[selCodifica][ar]; } for (int ar1 = 0; ar1 < arraiIntListino.length; ar1++) { arraiDatiListino[numListino3][ar1]=arraiDatiCodifica[selCodifica][ar1]; } ordina(arraiColTabella,arraiDatiTabella); // metodo per ordinare alfabeticamente } //<<<<<<< CancRiga() public void CancRiga() { ........ simile ad aggRiga() } //<<<<<<< tabella-------------------------------- public void Tabella() { final JTable TableListino = new JTable(new MyTableModel()); TableListino.setCellSelectionEnabled(true); if (tabScroll==true) { TableListino.setAutoResizeMode (JTable.AUTO_RESIZE_ALL_COLUMNS); } if (tabScroll==false) { TableListino.setAutoResizeMode (JTable.AUTO_RESIZE_OFF); } TableListino.getColumnModel().getColumn(0).setCellRenderer(new myRender1()); TableListino.getColumnModel().getColumn(0).setPreferredWidth(col1Larg); TableListino.getColumnModel().getColumn(1).setCellRenderer(new myRender1()); TableListino.getColumnModel().getColumn(1).setPreferredWidth(50); TableListino.getColumnModel().getColumn(2).setCellRenderer(new myRender1()); TableListino.getColumnModel().getColumn(2).setPreferredWidth(50); // evita che si attacchi vecchio testo al nuovo TableListino.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { int col3 = TableListino.getSelectedColumn(); int row3= TableListino.getSelectedRow(); int keyCode = e.getKeyCode(); if(keyCode!=40){ if(keyCode!=38){ if(keyCode!=39){ if(keyCode!=37){ if(keyCode!=10){ TableListino.setValueAt("", row3, col3); }}}}} } }); TableListino.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { rigaNum= TableListino.getSelectedRow(); } }); TableListino.getModel().addTableModelListener(new TableModelListener() { public void tableChanged(TableModelEvent e) { colonna = TableListino.getSelectedColumn(); row = TableListino.getSelectedRow(); String aa = TableListino.getModel().getValueAt(row, colonna).toString(); } } }); jScrollPane1.getViewport().add(TableListino); } } //<<<<<<< MyTableModel class MyTableModel extends AbstractTableModel { // inserisce i dati in tabella private String[] colonne = arraiColTabella; private Object[][] righe = arraiDatiTabella; public int getColumnCount() { return colonne.length; } public int getRowCount() { return righe.length; } @Override public String getColumnName(int col) { return colonne[col]; } public Object getValueAt(int col,int row) { return righe[col][row]; } public void setData(String[][] newdata) { righe = newdata; fireTableDataChanged(); } public boolean isCellSelected(int row,int column) { if (column > 2) { return false; } else { return true; } } @Override public boolean isCellEditable(int col,int row) { if (row > 2) { return false; } else { return true; } } public boolean isCellSelectable(int row,int column) { if (column > 2) { return false; } else { return true; } } @Override public void setValueAt(Object value, int col,int row) { // gestisce inserimento valori in tabella ( solo numeri positivi nelle prime 3 colonne) boolean no=false; boolean numerico=false; String aa=value.toString(); String aa1=value.toString(); double numero =0; double numeroV =0; // formato numero if ( !aa.equals("")) { if ( controlloIntString(aa)==true) { // System.out.println("formato numero"); numero =Double.parseDouble(aa); if ( !arraiDatiListino[col][row].equals("")) { numeroV =Double.parseDouble(arraiDatiListino[col][row]); } arrotonda1(numero, 2); DecimalFormat df = new DecimalFormat("0.00"); String output = df.format(numero); for(int i=0; i<output.length(); i++){ char car = output.charAt(i); if(car==',') { output = output.substring(0,i) + '.'+ output.substring(i+1); } } aa1=String.valueOf(output); numerico=true; }} // controllo numero if ( !aa.equals("")) { if ( numerico==false) { righe[col][row]= arraiDatiListino[col][row]; no=true; }} // controllo segno if ( !aa.equals("")) { if ( numero<0) { righe[col][row]= arraiDatiListino[col][row]; no=true; }} // controllo colonne if (no==false) { if (row < 3) { righe[col][row]= value; }} // controllo colonne scrivi num format if ( !aa.equals("")) { if (no==false) { if (row < 3) { righe[col][row]= aa1; }}} // diff prezzo if ( !aa.equals("")) { if (no==false) { if ( !arraiDatiListino[col][row].equals("")) { if (row == 1) { double dif = numeroV-numero; arrotonda1(dif, 2); DecimalFormat df = new DecimalFormat("0.00"); String output = df.format(dif); String diff=String.valueOf(output); }}}} fireTableCellUpdated(col,row); if (DEBUG) { printDebugData(); } } private void printDebugData() { int numRows = getRowCount(); int numCols = getColumnCount(); for (int i=0; i < numRows; i++) { for (int j=0; j < numCols; j++) { } } } } //<<<<<<< myRender (non usato) class myRender extends DefaultTableCellRenderer { public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component cell = super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column); Font ArialFont = new Font("Arial", Font.PLAIN, 20); Font CourierFont = new Font("Courier", Font.PLAIN, 15); //....... etc.

Rispondi quotando