Campo grande em uma Jtable

1 resposta
E

Bom dia a todos… tenho uma jtable e gostaria de exibir em um campo um texto que possui quebra de linha

minha tabela e exibida assim

public void atualizarGrid() throws FileNotFoundException, IOException, SQLException{

        jtDados.getColumnModel().getColumn(0).setPreferredWidth(5);
        jtDados.getColumnModel().getColumn(1).setPreferredWidth(200);
        DefaultTableModel modelo = (DefaultTableModel)jtDados.getModel();
        modelo.setRowCount(0);
        
        try {
            
             tarefa = new tarefas();
             DaoConsultaGenerica consulta = new DaoConsultaGenerica();
             List<tarefas> lista = new ArrayList();
             lista = consulta.lista_todos("tarefas");
             int tamanhoLista  = lista.size();
        for(int i=0; i < tamanhoLista; i++ ){
            tarefa = lista.get(i);
            modelo.addRow(new Object[]{tarefa.getIdTarefa(),tarefa.getDescricao()});
        }
        
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Tentei utilizar o código:

jtDados.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

Não deu certo

Tenho esta classe que encontrei no forum mais não sei utiliza-la:

import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellRenderer;

public class TabelaTextAreaRenderer extends JTextArea implements TableCellRenderer{  
    // This method is called each time a cell in a column  
    // using this renderer needs to be rendered.  
      
    public  TabelaTextAreaRenderer(){  
        setLineWrap(true);  
        setWrapStyleWord(true);  
          
    }  
      
    public Component getTableCellRendererComponent(JTable table, Object value,  
            boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {  
        // 'value' is value contained in the cell located at  
        // (rowIndex, vColIndex)  
  
        if (isSelected) {  
            // cell (and perhaps other cells) are selected  
        }  
  
        if (hasFocus) {  
            // this cell is the anchor and the table has the focus  
        }  
        // Configure the component with the specified value  
        setText(value.toString());  
  
        // Set tool tip if desired  
        setToolTipText((String)value);  
  
        // Since the renderer is a component, return itself  
        return this;  
    }  
  
}

Já agradeço pela ajuda…

1 Resposta

J

Tenta com a alteração que fiz abaixo!

public void atualizarGrid() throws FileNotFoundException, IOException, SQLException{

jTDados.getColumnModel().getColumn(0).setCellRenderer(new TabelaTextAreaRenderer());//coloquei esta linha

jtDados.getColumnModel().getColumn(0).setPreferredWidth(5);

jTDados.getColumnModel().getColumn(0).setCellRenderer(new TabelaTextAreaRenderer());//coloquei esta linha

jtDados.getColumnModel().getColumn(1).setPreferredWidth(200);

DefaultTableModel modelo = (DefaultTableModel)jtDados.getModel();

modelo.setRowCount(0);
try {  
         
        tarefa = new tarefas();  
        DaoConsultaGenerica consulta = new DaoConsultaGenerica();  
        List<tarefas> lista = new ArrayList();  
        lista = consulta.lista_todos("tarefas");  
        int tamanhoLista  = lista.size();  
   for(int i=0; i < tamanhoLista; i++ ){  
       tarefa = lista.get(i);  
       modelo.addRow(new Object[]{tarefa.getIdTarefa(),tarefa.getDescricao()});  
   }  
     
   } catch (Exception e) {  
       e.printStackTrace();  
   }

}

Criado 3 de outubro de 2012
Ultima resposta 10 de mai. de 2015
Respostas 1
Participantes 2