Java Swing制作JTable Columns填充JScrollPane

时间:2015-07-10 08:50:25

标签: java swing jtable jscrollpane

我制作了一个显示数据库的GUI JTable。我有一个尺码问题。

我怎样才能使JTable的列填充JScrollPane,但如果一个表中有很多列,那么它只会保持默认大小并让它们滚动。

基本上..

如果其中一个SQL表没有填充JTable并且不需要滚动,那么我希望JTable的列更大,以便它们适合。

如果SQL JTable确实需要滚动,那么我只是希望它保持这样,所以它需要滚动。

这是我制作JTable的代码:

        JPanel panel1 = new JPanel();
        JTable table = new JTable(){
        private static final long serialVersionUID = 1L;

            @Override
                public boolean isCellEditable(int row, int column) {
                   return false;
                }

        };
        JScrollPane stable = new JScrollPane (table);
        stable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        stable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        panel1.add(stable);

3 个答案:

答案 0 :(得分:2)

在您的代码中注明以下行:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

这会使表格列拉伸以填充滚动窗格的宽度

拉伸色谱柱以适应表格的整个宽度使其看起来很难看。相反,我建议在您的表中添加以下功能。

http://www.jroller.com/santhosh/entry/jtable_becomes_uglier_with_auto http://www.jroller.com/santhosh/entry/packing_jtable_columns

这使你的jtable看起来更专业

答案 1 :(得分:2)

在使用新模型更新表后,准确获取您要求调用的方法:

public static void tweakColumns(JTable table){
    Enumeration<TableColumn> columns = table.getColumnModel().getColumns();

    int required = 0;
    while(columns.hasMoreElements()){
        TableColumn column = columns.nextElement();
        int width = (int)table.getTableHeader().getDefaultRenderer()
                    .getTableCellRendererComponent(table, column.getIdentifier()
                            , false, false, -1, column.getModelIndex()).getPreferredSize().getWidth();
        required += width;
    }

    JViewport viewport = (JViewport)SwingUtilities.getAncestorOfClass(JViewport.class, table);
    int viewportWidth = viewport.getWidth();
    table.setAutoResizeMode(required<viewportWidth ? JTable.AUTO_RESIZE_ALL_COLUMNS : JTable.AUTO_RESIZE_OFF);
}

答案 2 :(得分:0)

import java.awt.Dimension;
import java.awt.Font;
import javax.swing.table.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class StatusPage extends JPanel
{
    static DefaultTableModel dm;
    JTable table;
    JScrollPane jsp_table;
    static Connection conn;
    static Statement stmt;
    static ResultSet rs;

    public StatusPage(Connection c)
    {
        conn = c;
        String col_name[] = {"S.No.","Book Name","Author Name","ISBN","Available Book","Issue Book","Total Book"};
        dm = new DefaultTableModel(null,col_name);
        table = new JTable(dm);
        table.getTableHeader().setFont( new Font( "Goudy Old Style" , Font.BOLD, 15 ));
                table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                table.getColumnModel().getColumn(0).setPreferredWidth(50);
                table.getColumnModel().getColumn(1).setPreferredWidth(175);
                table.getColumnModel().getColumn(2).setPreferredWidth(175);
                table.getColumnModel().getColumn(3).setPreferredWidth(125);
                table.getColumnModel().getColumn(4).setPreferredWidth(111);
                table.getColumnModel().getColumn(5).setPreferredWidth(90);
                table.getColumnModel().getColumn(6).setPreferredWidth(90);
        jsp_table = new JScrollPane(table);
        jsp_table.setPreferredSize(new Dimension(850, 520));
        addRowTable();
        add(jsp_table);
    }
    public static void addRowTable()
    {
        try
        {
                    int a = dm.getRowCount();
            int i=0;
            while(i<a)
            {
                dm.removeRow(0);
                i++;
            }

            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery("SELECT * FROM BOOK_TABLE");

            int count = 1;
            while(rs.next())
            {
                String s[] = new String[7];

                s[0] = ""+count;
                s[1] = rs.getString(6);
                s[2] = rs.getString(1);
                s[3] = rs.getString(2);
                s[4] = ""+rs.getInt(4);
                s[5] = ""+rs.getInt(5);
                s[6] = ""+rs.getInt(3);

                count++;

                dm.addRow(s);
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
}

阅读此代码并更改代码因为此代码有效