我几天来一直在寻找答案。
我有一个JTable,它填充了来自本地SQLite-DB的数据。
现在目标是,用户选择单元格(1-max。可用),编辑单元格并在Jtable中查看其输入。此外,我想将此输入保存为SQL语句。
现在,当用户输入并离开单元格时,我陷入了Point。 我没有让JTable显示变化。
我尝试重绘,我尝试了TableModelListener,但没有成功。 JTable仍然是一样的。 这可能不是什么大问题。但我可以在这里使用一些帮助。 如何达到显示更新的JTable的目标,包括用户的输入?
package views;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import main.DatenHolen;
public class GUI extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new GUI().setVisible(true);
}
});
}
private JButton btLoad;
public JTable table;
final static String DB_PATH = "C:\\DB.db"; //
private JButton btnDBNeuSpeichern;
private JButton btnDatenbankVergleich;
private JButton btnAusgewhlteDateienLoeschen;
private JButton btnZuLoeschendeDateien;
public GUI() throws HeadlessException {
createComponents(); //Formular und Bestandteile aufbauen
createEvents(); //Events und Logiken erstellen
}
private void createComponents() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
btLoad = new JButton("Datenbank laden");
btLoad.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
loadData();
}
});
btLoad.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btLoad.setBounds(537, 25, 183, 28);
getContentPane().add(btLoad);
btnDBNeuSpeichern = new JButton("Laufwerke neu in DB laden");
btnDBNeuSpeichern.setBounds(537, 65, 183, 28);
getContentPane().add(btnDBNeuSpeichern);
btnDatenbankVergleich = new JButton("Datenbank mit bisheriger vergleichen");
btnDatenbankVergleich.setBounds(537, 105, 273, 28);
getContentPane().add(btnDatenbankVergleich);
btnAusgewhlteDateienLoeschen = new JButton("Ausgewählte Dateien löschen");
btnAusgewhlteDateienLoeschen.setBounds(537, 145, 205, 28);
getContentPane().add(btnAusgewhlteDateienLoeschen);
btnZuLoeschendeDateien = new JButton("Zu löschende Dateien selektieren");
btnZuLoeschendeDateien.setBounds(537, 183, 205, 28);
getContentPane().add(btnZuLoeschendeDateien);
setSize(918, 850);
}
private void createEvents() {
btnDBNeuSpeichern.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
}
});
}
private void loadData() {
DatenHolen DBLaden = new DatenHolen(); // DBQuers-Object
table = new JTable(DBLaden.run(DB_PATH)); //DB_PATH is the SQLite-DB including path ; Method is returning TableModel and therefore populating the Jtable
table.getModel().addTableModelListener(new TableModelListener(){
@Override
public void tableChanged(TableModelEvent arg0) {
}
});
}
}
这是我的JTableModel-Class
package main;
import javax.swing.table.DefaultTableModel;
public class TableModelSpezial extends DefaultTableModel{
private static final long serialVersionUID = 1L;
public boolean isCellEditable( int rowIndex, int columnIndex )
{
return true; //falls Änderung auf eine Zeile beschränkt wird, zb 1. dann columnIndex==0;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
System.out.println(aValue);
System.out.println(rowIndex);
System.out.println(columnIndex);
// fireTableCellUpdated(rowIndex, columnIndex);
}
}
// TableModel& SQL_Build
package main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.table.TableModel;
public class DatenHolen extends Thread{
public TableModelSpezial tableModel= new TableModelSpezial();
public TableModel run(String DB_PATH) {
try (Connection conn = DriverManager.getConnection("jdbc:sqlite:" + DB_PATH); //put up conn to db
Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery(" select* from dateien");
ResultSetMetaData metaData = rs.getMetaData();
// Names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) { //Test 1=2, damit i=1 die Spaltennummerierung darstellt
columnNames.add(metaData.getColumnName(i));
}
// Data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int i = 1; i <= columnCount; i++) {
vector.add(rs.getObject(i));
}
data.add(vector);
System.out.println(vector);
}
tableModel.setDataVector(data, columnNames);
} catch (Exception e) {
e.printStackTrace();
}
return tableModel;
}
}
答案 0 :(得分:1)
秘密在表格模型中,您尚未完全发布。
然而,该模型执行从后备存储到用户界面的转换,反之亦然。因此,您需要实现getValueAt()以检索在给定单元格中显示的正确值。在setValueAt()中,您必须更新后备存储,以便下一次调用getValueAt()将返回新值。此外,您重新激活注释的fireTableCellUpdated,以便表本身知道必须获取新值。
答案 1 :(得分:0)
在处理数据之前,您需要停止对单元格进行编辑。
两种方法:
查看Table Stop Editing以了解这两种方法的示例。