I have seen similar questions here, on the stackoverflow but anyway I couldn't resolve my problem with these answers.
What i would like to do:
- click double on cell in the JTable (which is editable thanks to isCellEditable method)
- save new value of a cell in my custom TableModel to print this new value
- update data in my database (SQlite)
What I have done:
- this is my custom TableModel
.
import javax.swing.table.AbstractTableModel;
public class KierunkiTableModel extends AbstractTableModel {
private boolean DEBUG = false;
private String[] columnNames = { "Id", "Data Wstawienia",
"Data Modyfikacji", "Kierunek", "Opis" };
private Object[][] data = DodEdKierunki.populateData(DodEdKierunki.count);
@Override
public int getRowCount() {
return data.length;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public boolean isCellEditable(int row, int col) {
return true;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
@Override
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
- this is my JPanel where I print my JTable:
.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import baza.danych.DBConnector;
public class DodEdKierunki extends JPanel implements ActionListener {
private JButton dodaj;
private JButton edytuj;
private JButton next;
private JButton previous;
static JTable table;
static Object[][] data = new Object[1][5];
static int count = setValue();
public DodEdKierunki() {
dodaj = new JButton("Dodaj");
edytuj = new JButton("Edytuj");
next = new JButton("Pokaż kolejne 5");
previous = new JButton("Pokaż poprzednie 5");
setLayout(new FlowLayout());
dodaj.addActionListener(this);
edytuj.addActionListener(this);
next.addActionListener(this);
previous.addActionListener(this);
add(dodaj);
add(edytuj);
add(next);
add(previous);
table = new JTable(new KierunkiTableModel());
table.setFillsViewportHeight(true);
table.getColumnModel().getColumn(0).setPreferredWidth(30);
table.getColumnModel().getColumn(1).setPreferredWidth(100);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(130);
table.getColumnModel().getColumn(4).setPreferredWidth(130);
table.setEnabled(true);
add(table.getTableHeader(), BorderLayout.PAGE_START);
add(table, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == dodaj) {
new DodajKierunekFrame();
} else if (source == edytuj) {
new EdytujKierunekJFrame();
} else if (source == next) {
DBConnector db = new DBConnector();
int id = db.getHighestID("Kierunki");
int currId = count + 5;
if (currId <= id) {
count = count + 5;
data = populateData(count);
KierunkiTableModel model = new KierunkiTableModel();
model.fireTableDataChanged();
table.setModel(model);
setLayout(new FlowLayout());
table.getColumnModel().getColumn(0).setPreferredWidth(30);
table.getColumnModel().getColumn(1).setPreferredWidth(130);
table.getColumnModel().getColumn(2).setPreferredWidth(130);
table.getColumnModel().getColumn(3).setPreferredWidth(100);
table.getColumnModel().getColumn(4).setPreferredWidth(130);
table.repaint();
db.closeConnection();
}
} else if (source == previous) {
if (count > 5) {
count = count - 5;
data = populateData(count);
KierunkiTableModel model = new KierunkiTableModel();
model.fireTableDataChanged();
table.setModel(model);
setLayout(new FlowLayout());
table.getColumnModel().getColumn(0).setPreferredWidth(30);
table.getColumnModel().getColumn(1).setPreferredWidth(130);
table.getColumnModel().getColumn(2).setPreferredWidth(130);
table.getColumnModel().getColumn(3).setPreferredWidth(100);
table.getColumnModel().getColumn(4).setPreferredWidth(130);
table.repaint();
}
}
}
static Object[][] populateData(int count) {
DBConnector db = new DBConnector();
Object[][] lista = db.selectKierunki(count, "Kierunki");
db.closeConnection();
return lista;
}
private static int setValue() {
DBConnector db = new DBConnector();
int value = db.getHighestID("Kierunki");
db.closeConnection();
return value;
}
}
My problem is: I can edit a cell but I don't know how to save this changes. So my question is: How to change data model after editing a row in JTable ?
I have read http://download.oracle.com/javase/tutorial/uiswing/components/table.html