testei aqui e funciona perfeitamente... mas me fala, qual é o layout que vc ta usando ?? pode ser pq vc n esteja usando layout nenhum, ja vi pessoas reclamando disto...
fiz um pequeno exemplo, que apaga e adciona randomicamente numeros randomicos a uma JTable... da uma olhada ^^
A logica é a seguinte:
- cada linha contem uma coluna com index(auto_incrase), e outra com um numero randomico de 0 a 10.000 ...
- inicia com um numero randomico de linhas de 2 a 10
- sempre apaga ou remove linhas, randomicamente
- Ao apagar, escolhe randomicamente a linha a ser apagada, não necessariamente é a ultima.
- caso haja 0 linhas, vai sempre adcionar
- caso haja 10 linhas vai sempre apagar
- portanto sempre teremos as linhas entre 0 e 10
- O text Field mantem o registro do número de linhas
Depois de 126 cliques
[img]http://img294.imageshack.us/img294/1352/temp3ho0.png[/img]
pode ver, não tem 1 repaint ou algo parecido... e funfa perfect
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class TempPanel extends javax.swing.JPanel {
private JTable jTable1;
private JButton jButton1;
private JTextField jTextField1;
private JPanel jPanel1;
/**
* Auto-generated main method to display this
* JPanel inside a new JFrame.
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new TempPanel());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public TempPanel() {
super();
initGUI();
}
private void initGUI() {
try {
GridLayout thisLayout = new GridLayout(1, 1);
thisLayout.setColumns(1);
thisLayout.setHgap(5);
thisLayout.setVgap(5);
this.setLayout(thisLayout);
setPreferredSize(new Dimension(400, 300));
{
DefaultTableModel dtm = new DefaultTableModel(
new Integer[][] { RandomInt.getRow(),RandomInt.getRow() },
new String[] { "Código" , "random" });
for (int i = 0; i< (int)(Math.random()*8);i++)
dtm.addRow(RandomInt.getRow());
jTable1 = new JTable();
this.add(jTable1);
jTable1.setModel(dtm);
}
{
jPanel1 = new JPanel();
this.add(jPanel1);
GridLayout jPanel1Layout = new GridLayout(1, 1);
jPanel1Layout.setColumns(1);
jPanel1Layout.setHgap(5);
jPanel1Layout.setVgap(5);
jPanel1.setLayout(jPanel1Layout);
{
jButton1 = new JButton();
jPanel1.add(jButton1);
jButton1.setName("jButton1");
jButton1.setText("jButton1");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
}
{
jTextField1 = new JTextField();
jPanel1.add(jTextField1);
jTextField1.setName("jTextField1");
jTextField1.setText(""+jTable1.getModel().getRowCount());
}
}
//Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
} catch (Exception e) {
e.printStackTrace();
}
}
public JTable getJTable1() {
return jTable1;
}
public JButton getJButton1() {
return jButton1;
}
public JTextField getJTextField1() {
return jTextField1;
}
private synchronized void jButton1ActionPerformed(ActionEvent evt) {
DefaultTableModel dtm = (DefaultTableModel)getJTable1().getModel();
int rows = dtm.getRowCount();
if (rows == 0)
dtm.addRow(RandomInt.getRow());
else if (rows > 9)
dtm.removeRow(RandomInt.randomRowIndex(rows));
else {
if (RandomInt.randomBoolean())
dtm.addRow(RandomInt.getRow());
else
dtm.removeRow(RandomInt.randomRowIndex(rows));
}
getJTextField1().setText(""+dtm.getRowCount());
}
private static class RandomInt {
private static int count = 0;
private static Random random = new Random();
public static synchronized Integer[] getRow() {
Integer[] row = new Integer[2];
row[0] = count++;
row[1] = random.nextInt(10000);
return row;
}
public static boolean randomBoolean() {
return random.nextBoolean();
}
public static int randomRowIndex(int rowSize) {
int result = random.nextInt(rowSize) -1;
return (result < 0) ? 0 : result;
}
}
}
roda perfeitamente... sem problemas