olá , estou tentando adicionar mais linhas de registro na minha table model.
criei um metodo insert que adiciona livros a lista, mais quando tento mudar add para append ele chia.
alguem pode me ajudar?
abraço
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class TestTableModel extends AbstractTableModel{
private static final long serialVersionUID = 1L;
public static final int NOME = 0;
public static final int IDADE = 1;
List<Livro>valor = new ArrayList<Livro>();
//CONTRUTOR RECEBE LISTA DE LIVROS
public TestTableModel(List<Livro>v){
this.valor = v;
}
//INSERIR
public void insert(Livro livro){
valor.add(livro);// porque nao consigo mudar de add para append?
}
//REMOVER
public void apaga(Livro l){
valor.remove(l);
}
//NUMEROS DE COLUNAS
@Override
public int getColumnCount() {
return 2;
}
//QUANTIDADE DE LINHAS
@Override
public int getRowCount() {
return valor.size();
}
//NOME DA COLUNAS
public String getColumnName(int col){
if(col == NOME) return "Nome";
if(col == IDADE)return "Idade";
return "";
}
@Override
public Object getValueAt(int linha, int col) {
Livro titulo = valor.get(linha);
if(col == NOME) return titulo.getNome();
if(col == IDADE)return titulo.getPaginas();
return titulo.getPaginas();
}
//CELULAS EDITAVEIS OU NÃO
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public Livro get(int row) {
return valor.get(row);
}
}
public class Livro{
private static final long serialVersionUID = 1L;
String nome;
String paginas;
Livro(){
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getPaginas() {
return paginas;
}
public void setPaginas(String paginas) {
this.paginas = paginas;
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class Aplic{
private static final long serialVersionUID = 1L;
JFrame ff = new JFrame();
JPanel p;
JTextField tf1;
JLabel l = new JLabel("Nome:");
JPanel p2;
JTextField tf2;
JLabel l2 = new JLabel(" age: ");
JPanel p3;
JButton bt;
Vector<String> a = new Vector<String>();
JTable t;
DefaultTableModel mod;
String [][] comp;
JFrame f;
public static void main(String[] args) {
Aplic a = new Aplic();
a.init();
}
public void init(){
ff.getContentPane().add(getP(),BorderLayout.NORTH);
ff.getContentPane().add(getP2(),BorderLayout.CENTER);
ff.getContentPane().add(getP3(),BorderLayout.SOUTH);
ff.setTitle(" ************** CADASTRO *********** ");
ff.setSize(400,120);
ff.setLocation(400,400);
ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ff.setVisible(true);
}
public JPanel getP(){
if(p == null){
p = new JPanel();
p.add(l);
p.add(getTf1());
}
return p;
}
public JPanel getP2(){
if(p2 == null){
p2 = new JPanel();
p2.add(l2);
p2.add(getTf2());
}
return p2;
}
public JPanel getP3(){
if(p3 == null){
p3 = new JPanel();
p3.add(getBt());
}
return p3;
}
public JTextField getTf1(){
if(tf1 == null){
tf1 = new JTextField(15);
tf1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
}
});
}
return tf1;
}
public JTextField getTf2(){
if(tf2 == null){
tf2 = new JTextField(15);
tf2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
}
});
}
return tf2;
}
public JButton getBt(){
if(bt == null){
bt = new JButton("Listar");
bt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
f = new JFrame();
JPanel p = new JPanel();
JButton bt = new JButton("voltar");
bt.addActionListener(new Botao());
//OBJETOS DO TIPO LIVRO
Livro l = new Livro();
l.setNome(tf1.getText());
l.setPaginas(tf2.getText());
//OBJETOS DO TIPO
Aplic mod = new Aplic();
//LISTA DE LIVROS
List<Livro>valor = new ArrayList<Livro>();
JTable t = new JTable();
//MUDANDO MODELO DEFAULT PARA TestTableModel
t.setModel(new TestTableModel(valor));
TestTableModel Tmod = new TestTableModel(valor);
//INSERINDO NO TABLE MODEL
Tmod.insert(l);
//EXCLUINDO DO TABLE MODEL
//Tmod.apaga(l);
JScrollPane scroll = new JScrollPane(t);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
p.add(scroll);
p.add(bt,BorderLayout.SOUTH);
f.getContentPane().add(p);
f.setTitle(" *** CLIENTES CADASTRADOS *** ");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500,100);
f.setLocation(400,410);
f.setVisible(true);
/*f = new JFrame();
JPanel p = new JPanel();
JButton bt =new JButton("voltar");
bt.addActionListener(new Botao());
JPanel p2 = new JPanel();
p2.add(bt);
String[] col = {"Nome", "País"};
String[][] comp = {
{tf1.getText(), tf2.getText()},
};
mod = new DefaultTableModel(comp,col);
a.add(tf1.getText() + "\n");
a.add(tf2.getText());
mod.addRow(a);
t = new JTable(mod);
JScrollPane scroll = new JScrollPane(t);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
p.add(scroll);
f.getContentPane().add(p, BorderLayout.CENTER);
f.getContentPane().add(p2, BorderLayout.SOUTH);
f.setTitle(" *** CLIENTES CADASTRADOS *** ");
f.setSize(500,140);
f.setLocation(400,410);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
*/
}
});
}
return bt;
}
class Botao implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
f.dispose();
}
}
}//class
