но когда я перезагружаю данные и нажимаю jcheckbox, появляются старые данные. Я использую опцию defaulttablemodel.Пожалуйста, запустите прогу и помогите мне, где я ошибся.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class MainWindow extends JFrame {
JTable table;
private static final int CHECK_COL = 3;
String[] columnNames = {
"Country", "Capital", "Population in Millions", "Democracy"};
Object[][] data = {
{"USA", "Washington DC", 280, false},
{"Canada", "Ottawa", 32, false},
{"United Kingdom", "London", 60, false},
{"Germany", "Berlin", 83, false},
{"France", "Paris", 60, false},
{"Norway", "Oslo", 4.5, false},
{"India", "New Deli", 1046, false}
};
DefaultTableModel dtm ;
// static JScrollPane scrollpane= new JScrollPane();
public MainWindow() {
setTitle("Marking of data");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button1 = new JButton("load");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(dtm!=null){
System .out .println("NOT EMPTY");
int c = dtm.getRowCount();
// for (int i=c-1; i>=0; i--)
// {
// dtm.removeRow(i);
// table.revalidate();
// }
loadtable();
}
if(dtm==null){
System .out .println("EMPTY");
loadtable();
}
}
});
JButton button = new JButton("check");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int row = 0; row < table.getRowCount(); row++) {
Boolean b = ((Boolean) table.getValueAt(row, CHECK_COL));
if (b.booleanValue()) {
System.out.print("row " + row + " is " + b + ": ");
for (int col = 0; col < table.getColumnCount(); col++) {
System.out.print(table.getValueAt(row, col) + " ");
}
System.out.println();
}
}
}
});
JPanel buttonpanel1 = new JPanel();
buttonpanel1.add(button1);
JPanel buttonpanel = new JPanel();
buttonpanel.add(button);
add(buttonpanel, BorderLayout.SOUTH);
add(buttonpanel1, BorderLayout.NORTH);
pack();
setLocationByPlatform(true);
setVisible(true);
}
public void loadtable(){
dtm = new DefaultTableModel(data, columnNames) {
@Override
public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
@Override
public boolean isCellEditable(int rowIndex, int colIndex) {
return (colIndex == CHECK_COL);
}
};
table = new JTable(dtm);
JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainWindow();
}
});
}
}