【问题标题】:Getting correct table headings with vectors jswing使用向量 jswing 获取正确的表格标题
【发布时间】:2014-04-18 20:41:24
【问题描述】:

我有一个表格,它显示了 netbeans 数据库中的所有信息。表格与滚动条和信息完美显示,但标题都是一样的。数据库表中的第五个元素。因此,除了名称、类型、价格之外的 tble 标题...他们都说 example2、example2...

我不知道为什么。我觉得如果矢量概念可以填充所有信息,它也应该可以填充标题。

感谢您的帮助。

/*
 * FullDatabaseTable.java
 *
 * 
 */
package brainstormer95;

import java.awt.Color;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author ME
 */
public class FullDatabaseTable extends JPanel{
    Vector stringInfo;

    /** Creates new form FullDatabaseTable */
    public FullDatabaseTable() throws SQLException {

        //initComponents();
    }

    public void createTable() throws SQLException {
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/brainstormer", "name", "name");
        Statement stmt = con.createStatement();
        PreparedStatement psmt = con.prepareStatement(" SELECT ITEM_NAME,SPECIFIC_PURPOSE,EXAMPLE_1,EXAMPLE_2 FROM APP.INVENTORY");
        ResultSet rs = psmt.executeQuery();

        for (int i=1;i<=rs.getRow();i++){
        String tableTitle = (rs.getMetaData().getTableName(i));
        JLabel title = new JLabel(tableTitle);
        }

        int columnCount = rs.getMetaData().getColumnCount();
        Vector columns = new Vector(columnCount);

        for (int i = 1; i <= columnCount; i++) {
            columns.add(rs.getMetaData().getColumnName(columnCount));
        }

        Vector stringVector = new Vector();
        while (rs.next()) {
            stringInfo = new Vector(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                stringInfo.add(rs.getString(i));
            }
            stringVector.add(stringInfo);
        }

        //Format
        JScrollPane tableScrollPane;
        Dimension tableSize = new Dimension();
        tableSize.setSize(1400,800);
        Dimension scrollSize = new Dimension();
        scrollSize.setSize(1000,600);
        JTable table = new JTable(stringVector,columns);
        table.setPreferredSize(tableSize);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tableScrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        tableScrollPane.setPreferredSize(scrollSize);
        table.setShowGrid(true);
        table.setGridColor(Color.BLACK);
        table.setEnabled(false);
        JPanel jPanel = new JPanel();
        jPanel.add(tableScrollPane);
        add(jPanel);
        setVisible(true);
        setBackground(Color.white);

    }
     public JComponent returnGUI() {
        return this;

    }
}

【问题讨论】:

    标签: java swing vector derby


    【解决方案1】:

    这看起来不正确。您每次迭代都使用columnCount

       for (int i = 1; i <= columnCount; i++) {
            columns.add(rs.getMetaData().getColumnName(columnCount));
        }
    

    你可能是这个意思:

       for (int i = 1; i <= columnCount; i++) {
            columns.add(rs.getMetaData().getColumnName(i));
        }
    

    这可以解释为什么您会看到错误的标题名称。

    【讨论】:

    • 差点让我热泪盈眶:D 非常感谢!我一直在努力解决这个问题。
    猜你喜欢
    • 2015-12-01
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    相关资源
    最近更新 更多