olá amigos
estou tentando aprender a usar o TableModel
mas quando tento adicionar a lista de livros ao construtor de TestTableModel dá erro
alguem pode me ajudar?
abraço
REI DA MANGUAÇA
1. import javax.swing.JFrame;
2. import javax.swing.JPanel;
3. import javax.swing.JScrollPane;
4. import javax.swing.JTable;
5.
6.
7. public class ExeTModel {
8.
9. public static void main(String[] args) {
10.
11. JFrame f = new JFrame();
12. JPanel p = new JPanel();
13.
14. JTable t = new JTable();
15.
16. t.setModel(new TestTableModel(valor)); //ERRO AQUI QUANDO ADICIONO A LISTA DE LIVROS
17.
18. JScrollPane scroll = new JScrollPane(t);
19. scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
20. scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
21.
22. p.add(scroll);
23. f.getContentPane().add(p);
24. f.setTitle(" *** CLIENTES CADASTRADOS *** ");
25. f.setSize(500,100);
26. f.setLocation(400,410);
27. f.setVisible(true);
28.
29. }
30. }
1. import java.util.ArrayList;
2. import java.util.List;
3. import javax.swing.table.AbstractTableModel;
4.
5. public class TestTableModel extends AbstractTableModel{
6.
7. private static final long serialVersionUID = 1L;
8.
9.
10. public static final int NOME = 0;
11. public static final int IDADE = 0;
12.
13. List<Livro>valor;
14.
15. public TestTableModel(List<Livro>v){
16. this.valor = new ArrayList<Livro>(v);
17. }
18.
19. @Override
20. public int getColumnCount() {
21. return 2;
22. }
23.
24. @Override
25. public int getRowCount() {
26. return valor.size();
27. }
28.
29. public String getColuna(int col){
30. if(col == NOME) return "Nome";
31. if(col == IDADE)return "Idade";
32. return "";
33. }
34.
35. @Override
36. public Object getValueAt(int linha, int col) {
37. Livro titulo = valor.get(linha);
38. if(col == NOME) return titulo.getNome();
39. if(col == IDADE)return titulo.getPaginas();
40. return "";
41. }
42.
43. public boolean isCellEditable(int rowIndex, int columnIndex) {
44. return true;
45. }
46.
47. public Livro get(int row) {
48. return valor.get(row);
49. }
50.
51. }
CLASSE LIVRO
1. public class Livro{
2.
3. private static final long serialVersionUID = 1L;
4.
5. String nome;
6. int paginas;
7.
8. public String getNome() {
9. return nome;
10. }
11. public void setNome(String nome) {
12. this.nome = nome;
13. }
14. public int getPaginas() {
15. return paginas;
16. }
17. public void setPaginas(int paginas) {
18. this.paginas = paginas;
19. }
20.
21.
22. }
