Eventos em uma JTable

1 resposta
L

Boa noite, gostaria de transformar a String de título de uma coluna em um botão

Tenho uma tabela:

tablepaciente.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, Boolean.FALSE},
				{null, null, null, null, null, Boolean.FALSE},
				{null, null, null, null, null, Boolean.FALSE},
				{null, null, null, null, null, null},
				{null, null, null, null, null, Boolean.FALSE},
				{null, null, null, null, null, Boolean.FALSE},
				{null, null, null, null, null, Boolean.FALSE},
				{null, null, null, null, null, Boolean.FALSE},
				{null, null, null, null, null, null},
			},
			new String[] {
				"Nome", "CPF", "Data de Nascimento", "Profiss\u00E3o", "Editar", "Excluir"
			}
		) {
			Class[] columnTypes = new Class[] {
				Object.class, Object.class, Object.class, Object.class, Object.class, Boolean.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});

Gostaria que a String "Excluir" seja um botão, e que a coluna de Editar seja uma coluna de botões.

Ja tentei ultilizar uma classe extendida AbstractCellEditor

package View;

import java.awt.*;  
    
class ButtonColumn extends AbstractCellEditor implements TableCellRenderer, TableCellEditor{
	JTable table;
	JButton renderButton, editButton;
	String text;
	TableColumnModel columnModel;
	
	public ButtonColumn(JTable table, int column){
		super();
		this.table = table;
		renderButton = new JButton();
		editButton = new JButton();
		
		editButton.addActionListener(new java.awt.event.ActionListener() {
    			
			public void actionPerformed(java.awt.event.ActionEvent evt) {
    				
    				TelaPaciente tp = new TelaPaciente();
    				tp.pconsultar.setVisible(false);
    				tp.cadastrarPaciente.setVisible(false);
    				tp.scrollPane.setVisible(false);
    				tp.validate();
    				
    				}});
            editButton.setFocusPainted( false );   
            editButton.setBackground(new Color(255, 255, 255));	
            
            columnModel = table.getColumnModel();  
            columnModel.getColumn(column).setCellRenderer( this );  
            columnModel.getColumn(column).setCellEditor( this );  
        }  
   
        public Component getTableCellRendererComponent(  
            JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)  
        {  
            if (hasFocus)  
            {  
                renderButton.setForeground(table.getForeground());  
                renderButton.setBackground(UIManager.getColor("Button.background"));  
            }  
            else if (isSelected)  
            {  
                renderButton.setForeground(table.getSelectionForeground());  
                 renderButton.setBackground(table.getSelectionBackground());  
            }  
            else  
            {  
                renderButton.setForeground(table.getForeground());  
                renderButton.setBackground(UIManager.getColor("Button.background"));  
            }  
   
            renderButton.setText( (value == null) ? "" : value.toString() );  
            return renderButton;  
        }  
   
        public Component getTableCellEditorComponent(  
            JTable table, Object value, boolean isSelected, int row, int column)  
        {  
            text = (value == null) ? "" : value.toString();  
            editButton.setText( text ); 
            
            
            
            return editButton;  
        }  
   
        public Object getCellEditorValue()  
        {  
            return text;  
        }
    }

na minha tabela eu estancio esta classe e aponto sua posição no array da tabela:

Ex:
editar = new ButtonColumn(tablepaciente, 4);

O problema é que não consigo fazer eventos ultilizando esta maneira, mesmo implementando o ActionListener nesta classe, o evento não funciona!!!

Se alguem tiver uma solução, por gentileza me auxiliem!!

1 Resposta

S

Não poderia simplesmente colocar um JButton abaixo da tabela e, quando clicado, ele receber o índice da linha selecionada da tabela e excluí-la.

E outra, para facilitar o que falei acima, não use DefaultTableModel. Faça um TableModel personalizado
Veja: http://www.guj.com.br/java/199067-redimensionar-jtable---pra-variar--resolvido-/2#1001295

Criado 17 de outubro de 2012
Ultima resposta 26 de out. de 2012
Respostas 1
Participantes 2