Cor da linha selecionada na tabela

5 respostas
M

ola pessoal

tenho nas minhas tabelas o comando

tabela.setSelectionBackground( Color.green );

para mudar a cor da linha selecionada

mas em algumas tabelas eu uso o cellrender para formatar a visualizacao da celula, dae nesses casos ele perde o comando acima
como faço para mudar a cor da linha selecionada na tabela com cellrender
segue meu cell render

public class CellRender_Texto extends JLabel implements TableCellRenderer {

	private JLabel cell = null;

	private JLabel getCell() {
		if (cell == null)
			cell = new JLabel("");
		return cell;
	}

	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		if (value != null) {
			getCell().setText( value.toString() );
			getCell().setHorizontalAlignment(SwingConstants.LEFT);
		} else
			getCell().setText("");
		return getCell();
	}

}

chamada do cell rener

jTableTabelaPreco.getColumnModel().getColumn(0).setCellRenderer(new CellRender_Texto()); // mostra a celula

obrigado

5 Respostas

sf.marcius
public class CellRender_Texto extends JLabel implements TableCellRenderer {  
   
     private JLabel cell = null;  
   
     private JLabel getCell() {  
         if (cell == null)  
             cell = new JLabel("");  
         return cell;  
     }  
   
     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {  
         if (value != null) {  
             getCell().setText( value.toString() );  
             getCell().setHorizontalAlignment(SwingConstants.LEFT);  
         } else {
             getCell().setText("");  
         }
         // assim funcionaria???  
         if (isSelected) {
             getCell().setBackground(table.getSelectionBackground());
         }
         ///////////////////////////
         return getCell();  
     }  
   
 }

[]s

M

olá

desta maneira não funcionou
não sei porque mas …
so o setForeground funciona - cor da fonte
o setBackground nao funciona - cor do fundo

obrigado

M

acho que consegui pessoal
porque no cellrender estou usando jlabel
mudei para jtextfield e parece ter dado certo

obrigado

sf.marcius

estranho, pq eu sempre fiz com JLabel, só que eu sempre fazia assim, chamando “super” em vez de ter um get pro label.

public class CellRender_Texto extends JLabel implements TableCellRenderer {    
     
//     private JLabel cell = null;    
//     
//     private JLabel getCell() {    
//         if (cell == null)    
//             cell = new JLabel("");    
//         return cell;    
//     }    
     
     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
         JLabel lab = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
         if (value != null) {    
             lab.setText( value.toString() );    
             lab.setHorizontalAlignment(SwingConstants.LEFT);    
         } else {  
             lab.setText("");    
         }  
         // assim funcionaria??? //
         // if (isSelected) {  
         //    lab.setBackground(table.getSelectionBackground());  
         // }  
         /////////////////////////////
         return getCell();    
     }    
     
 }

[]s

kemperacc

Seu jlabel esta opaco?

Criado 25 de março de 2010
Ultima resposta 25 de mar. de 2010
Respostas 5
Participantes 3