0

I know this has been asked, but I can't seem to get this to work. I have a jTable with four columns inside of a JscrollPane. I can only see 3 columns though when I scroll? Can anyone else see what may be going wrong?

I checked these threads, but they didn't help: JTable Missing Column Headers Issues with JTable, I can add to the table but can't see all rows

Thanks

Code:

/*
 * 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;

    }
}
Community
  • 1
  • 1
JustBlossom
  • 1,259
  • 3
  • 24
  • 53
  • 3
    Start by not using setPreferredSize on the JTable – MadProgrammer Apr 19 '14 at 01:42
  • @MadProgrammer Cool! I can see them all now, but the columns are super thin. If I do `setWidth();` will I get the same problem later on? – JustBlossom Apr 19 '14 at 01:45
  • 1
    Use [`TableColumn.setPreferredWidth(int)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/table/TableColumn.html#setPreferredWidth-int-). More generally though, see [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Apr 19 '14 at 01:52
  • Also consider overriding `getPreferredScrollableViewportSize()`. – trashgod Apr 19 '14 at 11:46

0 Answers0