无法在滚动内部看到JTable中的所有列

时间:2014-04-19 01:29:41

标签: java swing jtable

我知道有人问这个问题,但我似乎无法让这个工作。我在JscrollPane里面有一个带有四列的jTable。当我滚动时,我只能看到3列?其他人可以看到可能出现的问题吗?

我检查了这些帖子,但是他们没有帮助: JTable Missing Column Headers Issues with JTable, I can add to the table but can't see all rows

由于

代码:

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

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
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;
import javax.swing.table.JTableHeader;
import brainstormer95.TableCellLongTextRenderer;

/**
 *
 * @author ...
 */
public class FullDatabaseTable extends JPanel {

    private Vector stringInfo;
    Font font2 = new Font("Calibri", Font.BOLD, 16);
    Font font3 = new Font("Calibri", Font.BOLD, 30);
    private JTable table;
    private JScrollPane scrollPane = new JScrollPane(table);

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

        //initComponents();

    }

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


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

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

        //Cell data
        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);
        }

        //Jtable Properties
        Dimension tableSize = new Dimension();
        tableSize.setSize(1400, 800);
        Dimension scrollSize = new Dimension();
        scrollSize.setSize(1000, 600);


        //JTable Creation
        table = new JTable(stringVector, columns);
        table.setPreferredSize(tableSize);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table.setFont(font2);

        //needs to be set on the appropriate column indicy
        table.getColumnModel().getColumn(1).setCellRenderer(new TableCellLongTextRenderer());

        //AutoScrolls
        scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setPreferredSize(scrollSize);

        //Grid Properties
        table.setShowGrid(true);
        table.setGridColor(Color.LIGHT_GRAY);

        //Header Properties
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.pink);
        header.setFont(font3);
        header.setAutoscrolls(true);

        //Final
        add(scrollPane);
        setVisible(true);
        setBackground(Color.LIGHT_GRAY);

    }

    public JComponent returnGUI() {
        return this;

    }
}

0 个答案:

没有答案