Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to place JTable in JScrollpane and create vert and hor scrollbars?

holodMar 1 2007 — edited Mar 1 2007
I have a problem:
I overide some AbstractTableModel
(http://forum.java.sun.com/thread.jspa?threadID=5141966&tstart=70)
And then I create JTable.
I put JTable on JScrollpane.
The problem is:
With help of vertical scrollbar I can go through rows of JTable.
But what can I do with HORIZONTAL scrollbar?
I create it, but it is not ENABLED.
Also, I want to set FIXED width of columns.

So user will not see ALL columns. He will use horizontal scrollbar.
The same as rows. User uses vertical scrollbar to see rows (but it is set by default and I want to do the same with horizontal scrollbar.)

Help me,please.

My Class where I try to do that:
public class TableCreate extends JPanel{
    
    /** Creates a new instance of TableCreate */
    public TableCreate() {
        super(new GridLayout(1,0));
        JTable table = new JTable(new JTableAdapter("jdbc:mysql://localhost/Document?","com.mysql.jdbc.Driver","root",""));
        table.setPreferredScrollableViewportSize(new Dimension(500,210));
        
        JScrollPane scrollpane = new JScrollPane(table,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
         initColumnSizes(table);
        //scrollpane.setHorizontalScrollBar(scrollpane.createHorizontalScrollBar());
        //scrollpane.setHorizontalScrollBarPolicy(scrollpane.HORIZONTAL_SCROLLBAR_ALWAYS); 
                add(scrollpane);
    }
    /**returns reference to panel with Scrollpane and JTable*/
    public JPanel getJpanelTable(){
        return this;
    }
    /**Sets good column sizes*/
    private void initColumnSizes(JTable table){
        JTableAdapter model = (JTableAdapter)table.getModel();
        TableColumn column = null;
        Component comp = null;
        TableCellRenderer  headerRender = table.getTableHeader().getDefaultRenderer();
        int headerWidth = 0;
        int cellWidth = 0;
        
        for(int i=0;i<model.getColumnCount();i++){
            column = table.getColumnModel().getColumn(i);
            
            comp = headerRender.getTableCellRendererComponent(null, column.getHeaderValue(), 
                                                              false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;
            
            comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(
                                                                     table, model.getTableRowAsObject(i),
                                                                     false, false, 0, i);
            cellWidth = comp.getPreferredSize().width;
            
            System.out.println("MIN( " + headerWidth + " , " + cellWidth + " )=" + Math.min(headerWidth, cellWidth)+":::"+column.getHeaderValue());
            column.setPreferredWidth(Math.min(headerWidth, cellWidth));
        }
        
        
    }
    //public set_SheetTypeID

    public static void main(String[] args){
        JFrame jframe = new JFrame("Test");
            TableCreate tcreate = new TableCreate();
                jframe.add(tcreate.getJpanelTable());
                jframe.pack();
                jframe.setVisible(true);
}//main

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 29 2007
Added on Mar 1 2007
2 comments
564 views