我无法使其工作......如何从抽象模型中的数据库中获取数据?我真的需要帮助谢谢..我尝试了不同的解决方案,但没有成功......有人可以帮助我,谢谢......甚至只是链接上帝保佑!
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.BoxLayout;
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.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;
public class ConsolidateInvoices extends JPanel {
private JTable table;
private JTextField Name;
private JTextField BusinessPartner;
private TableRowSorter<Model> sorter;
private String[] columnNames;
private Object[][] data;
public ConsolidateInvoices() {
JPanel form = new JPanel(new SpringLayout());
JLabel lblName = new JLabel("Name:", SwingConstants.TRAILING);
form.add(lblName);
Name = new JTextField();
form.add(Name);
JLabel lblBusinessPartner = new JLabel("Business Partner:",
SwingConstants.TRAILING);
form.add(lblBusinessPartner);
BusinessPartner = new JTextField();
// Whenever filterText changes do the filter method
BusinessPartner.getDocument().addDocumentListener(
new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
newFilter();
}
public void insertUpdate(DocumentEvent e) {
newFilter();
}
public void removeUpdate(DocumentEvent e) {
newFilter();
}
});
lblBusinessPartner.setLabelFor(BusinessPartner);
form.add(BusinessPartner);
SpringUtilities.makeCompactGrid(form, 2, 2, 6, 6, 6, 6);
add(form);
try {
Class.forName("org.postgresql.Driver");
Connection connection = null;
connection = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/adempiere2",
"postgres", "postgres");
// Read data from a table
String query = "Select c_invoice_id, documentno "
+ "from adempiere.c_invoice";
Statement stmt = connection.createStatement();
ResultSet rst = stmt.executeQuery(query);
ResultSetMetaData md = rst.getMetaData();
int columns = md.getColumnCount();
int rows = 1;
//new Boolean(false)
columnNames = new String[columns];
// Get column names
for (int i = 1; i <= columns; i++) {
columnNames[i-1] = md.getColumnName(i);
}
System.out.println(rows);
data = new Object[rows][columns];
// Get row data
while (rst.next()) {
int ctr = 0;
for (int i = 1; i <= columns; i++) {
data[ctr][i-1] = rst.getObject(i);
}
ctr++;
}
rst.close();
stmt.close();
connection.close();
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// Create a separate panel for the table
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
table = new JTable(new Model());
// Sorter
Model model = new Model();
sorter = new TableRowSorter<Model>(model);
table.setRowSorter(sorter);
table.setPreferredScrollableViewportSize(new Dimension(700, 700));
table.setFillsViewportHeight(true);
// To make the selection single
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// To create the scroll panel
JScrollPane scrollPane = new JScrollPane(table);
// initColumnSizes(table);
// setUpSportColumn(table, table.getColumnModel().getColumn(2));
add(scrollPane);
}
// The filter method used for the business partner
private void newFilter() {
RowFilter<Model, Object> rf = null;
// If current text doesn't parse, don't update.
try {
rf = RowFilter.regexFilter(BusinessPartner.getText(), 0);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
sorter.setRowFilter(rf);
}
class Model extends AbstractTableModel {
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
// To make the column a check box
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
return false;
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
}
private static void showWindow() {
// Create and set up the window.
JFrame frame = new JFrame("Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
ConsolidateInvoices newContentPane = new ConsolidateInvoices();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
showWindow();
}
});
}
}
答案 0 :(得分:3)
虽然你没有说明问题所在,但我已经看到了几个问题:
table = new JTable(new Model());
// Sorter
Model model = new Model();
您应该创建两个模型,而应该使用相同的实例。现在你可以解决这个问题,因为它们共享相同的data
。
} catch (SQLException e) {
// Always log or print an exception
} catch (ClassNotFoundException e) {
e.printStackTrace();
// If this exception occurs, the rest of the code is pretty much useless since you won't have a connection to the DB. You should rather forward this exception as an error
}
// To make the column a check box
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
如果没有值,则会出现异常(ArrayIndexOutOfBounds或NullPointerException)。此信息应该基于ResultMetadataSet
。
public boolean isCellEditable(int row, int col) {
return false;
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
如果你说你的单元格不可编辑,不需要实现setValueAt,它将永远不会被调用。
你的for循环有什么用?:
for (int i = 1; i <= columns; i++) {
columnNames[i-1] ...
只需使用:
for (int i = 0; i < columns; i++) {
columnNames[i] ...
更清洁,更简单,更不容易出错。
答案 1 :(得分:2)
我想问题出在这里:
table = new JTable(new Model());
// Sorter
Model model = new Model();
sorter = new TableRowSorter<Model>(model);
table.setRowSorter(sorter);
您不对表格和分拣机使用相同的模型。怎么样:
Model model = new Model();
sorter = new TableRowSorter<Model>(model);
table = new JTable(model);
table.setRowSorter(sorter);
我还发现了OP建议使用的this similar post:
sorter.setSortsOnUpdates(true);
并将fireTableCellUpdated(row, col);
(在 setValueAt()中)替换为:
fireTableRowsUpdated(0, data.size() - 1);
如果你看看这个Oracle example,我们可以看到一个有趣的评论:
// Normally, one should call fireTableCellUpdated() when
// a value is changed. However, doing so in this demo
// causes a problem with TableSorter. The tableChanged()
// call on TableSorter that results from calling
// fireTableCellUpdated() causes the indices to be regenerated
// when they shouldn't be. Ideally, TableSorter should be
// given a more intelligent tableChanged() implementation,
// and then the following line can be uncommented.
// fireTableCellUpdated(row, col);