-1

我不能让它工作......我怎样才能从抽象模型内的数据库中获取数据?我真的需要帮助谢谢..我尝试了不同的解决方案,但没有成功...有人可以帮我谢谢...即使只是链接上帝保佑!

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();
            }
        });
    }

}
4

2 回答 2

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] ...

更干净,更简单,更不容易出错。

于 2012-06-21T08:45:30.487 回答
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 建议使用的类似帖子:

sorter.setSortsOnUpdates(true);

并将fireTableCellUpdated(row, col);(在setValueAt()中)替换为:

fireTableRowsUpdated(0, data.size() - 1);

如果您看一下这个Oracle 示例,我们会看到一条有趣的评论:

        // 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);
于 2012-06-21T08:39:14.613 回答