Cores com JList implementando ListCellRenderer

0 respostas
A

Fiz esse pequeno exemplo … mas para aprendizagem com ListCellRenderer…
Ao Selecionar um item inserido no JList, segue um padrão definido…
Avaliem ae valeu…

public class FramePrincipal extends JFrame{
    
    List<String> nomes = new ArrayList<>();
    DefaultListModel<String> model = null;   
    MyJListCell listCell = null;      
    
    public FramePrincipal(){
        initComponents();
        setResizable(false);
        setLocationRelativeTo(null);
    }
    
    private void cadastro(){ 
        if(!jtfCadastro.getText().equals("")){
            nomes.add(jtfCadastro.getText());
            myModelJList(nomes);
            jtfCadastro.setText("");           
        }else{
            JOptionPane.showMessageDialog(null, "Insira algum Nome !!");
        }
    }    
  
    private void myModelJList(List<String> nome){
        model = new DefaultListModel<>();
        for(int i = 0; i < nome.size(); i++){
            jList.setModel(model);
            model.addElement(nome.get(i));            
        }
    }
    
    private void apagarLista(){
        nomes.clear();
        model.clear();       
    }
    
    private void myJListCellRedender(){
        listCell = new MyJListCell();      
        listCell.setCorMyJListCellRenderer(nomes.get(jList.getSelectedIndex()), jList.getSelectedIndex());
        jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);        
        jList.setCellRenderer(listCell);       
    }

    //Generated Code

   private void jbCadastroActionPerformed(java.awt.event.ActionEvent evt) {                                           
        cadastro();       
    }                                          

    private void jListValueChanged(javax.swing.event.ListSelectionEvent evt) {                                   
        if(evt.getValueIsAdjusting()){
            myJListCellRedender();
        }
    }                                  

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        apagarLista();
    }

    public static void main(String args[]){      
        //Look and feel
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FramePrincipal().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify
public class MyJListCell extends JLabel implements ListCellRenderer{    
    
    private int result;  
    private int indexLista;
    
    public MyJListCell(){ 
                
    }  
    
    public void setCorMyJListCellRenderer(String nome, int indexLista){        
        if(nome != null){
            this.indexLista = indexLista;
            if(nome.length() >= 1 && nome.length() <= 3){
                result = 1; 
            } 
            if(nome.length() > 3 && nome.length() <= 6){
                result = 2;    
            }                   
            if(nome.length() > 6 && nome.length() <= 9){
                result = 3;    
            }                   
            if(nome.length() > 9){
                result = 4;    
            }                   
        }
    }    
  
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
        setText(value.toString());
        if(index == indexLista){
            if(isSelected){
                if(result == 1){
                    setBackground(Color.red);
                    setForeground(Color.black);
                }
                if(result == 2){
                    setBackground(Color.blue);
                    setForeground(Color.black);
                }                
                if(result == 3){
                    setBackground(Color.orange);
                    setForeground(Color.black);
                }                
                if(result == 4){
                    setBackground(Color.green);
                    setForeground(Color.black);
                }                
            }else{
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            }
        }else{
            if(isSelected){
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }else{
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
        }
        setOpaque(true);
        setEnabled(list.isEnabled());
        setFont(list.getFont());    
        list.setSelectedIndex(indexLista);
        return this;
    }    
}
Criado 5 de fevereiro de 2013
Respostas 0
Participantes 1