Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2010
    Messaggi
    132

    problema JTable (append in scrittura al contenuto della cella)

    Ciao a tutti,
    sto costruendo una JTable, ed ho il problema che quando scrivo un valore in una cella, la stringa viene attaccata a quella già presente nella cella invece di sostituirla.
    Vorrei che come in excell, appena comincio a scrivere in 1 cella, questa diventi vuota.

    Allego il codice della mia tabella, non è un capolavoro : ) sto ancora cercando di capire quando convenga "DefaultTableCellRenderer" o "AbstractTableModel" e come funzionano.

    Grazie per l'aiuto.

    codice:
    public void Tabella() {
    
            final JTable TableListino = new JTable(new MyTableModel());
    
            TableListino.setCellSelectionEnabled(true);
            TableListino.setAutoResizeMode (JTable.AUTO_RESIZE_ALL_COLUMNS);
            TableColumn col = TableListino.getColumnModel().getColumn(0);
            col.setCellRenderer(new myRender());
    
    
        ScrollListino.getViewport().add(TableListino);
        System.out.println("fine Tabella()");
    }
    }
    
    class MyTableModel extends AbstractTableModel {
    
           private String[] colonne = arraiColTabella;
           private Object[][] righe = arraiDatiTabella;
           boolean rowSelectionAllowed=false;
    
           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();
                  }
    
            @Override
      public Class getColumnClass(int c) {
        return getValueAt(c, 0).getClass();
        }
    
    
    public boolean isCellSelected(int row,int column) {
    if (row > 2) {
            return false;
        } else {
            return true;
        }
    }
    
            @Override
       public boolean isCellEditable(int col,int row) {
      if (row > 2) {
            return false;
        } else {
            return true;
        }
     }
    
    
     
            @Override
    public void setValueAt(Object value, int col,int row) {
        if (DEBUG) {
        }
        righe[col][row]= value;
        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++) {
         }
        }
       }
       }
    
    
    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);
    if(row==0) cell.setBackground(Color.red);
    else
    cell.setBackground(Color.green );
    return cell;
    }
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Nov 2010
    Messaggi
    132

    soluzione artigianale

    Bè, nel frattempo ho trovato una soluzione artigianale.
    Inserire nel metodo tabella()

    codice:
    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);
            }}}}}
            }
            });
    C'è di meglio?

    Grazie.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.