Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2004
    Messaggi
    162

    [JAVA] JTable: selezione riga + valori non modificabili

    Salve a tutti.
    Sto sperimentando la JTable, ma ho due problemi:

    1) La mia tabella ha 3 colonne: ID - COGNOME - NOME
    Vorrei che cliccando su una riga (quindi cliccando indifferentemente su id o su nome o su cognome) mi venga restituito il valore ID relativo alla riga selezionata.

    2) Di default facendo doppio click su una cella della tabella, si può modificarne il contenuto: come si disabilità questa possibilità?

    In pratica ho bisogno delle due insieme, ovvero facendo doppio click su una cella non entri in modalità modifica valore, ma mi venga restituito il valore ID di quella riga.

    Grazie dell'aiuto.
    Ciao

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2004
    Messaggi
    162
    Risolto così:

    1)
    codice:
    JTable table = new JTable(model){
                            public boolean isCellEditable(int r, int c) {
                                    return false;
                            }
                    };
    2)

    codice:
    private boolean ALLOW_COLUMN_SELECTION = false;
    private boolean ALLOW_ROW_SELECTION = true;
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            if (ALLOW_ROW_SELECTION) { // true by default
        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting()) return;
    
                ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow
                                       + " is now selected. " + table.getValueAt(selectedRow,0));
                }
            }
        });
    } else {
        table.setRowSelectionAllowed(false);
    }
    
    if (ALLOW_COLUMN_SELECTION) { // false by default
        if (ALLOW_ROW_SELECTION) {
            //We allow both row and column selection, which
            //implies that we *really* want to allow individual
            //cell selection.
            table.setCellSelectionEnabled(true);
        }
        table.setColumnSelectionAllowed(true);
        ListSelectionModel colSM =
            table.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting()) return;
    
                ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No columns are selected.");
                } else {
                    int selectedCol = lsm.getMinSelectionIndex();
                    System.out.println("Column " + selectedCol
                                       + " is now selected." + table.getValueAt(0,selectedCol));
                }
            }
        });
    }
    Grazie lo stesso

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 © 2024 vBulletin Solutions, Inc. All rights reserved.