Results 1 to 4 of 4
  1. #1
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default fill table with cells

    Hi, I'd like to fill whole scroll pane (viewport of table) with grid.
    1) how can I get width of scroll pane? (The dimension of scroll pane is (0, 0) (when I call pane.getSize()). When I'd know width of scroll pane, I could calculate widths of columns.)
    2) is possible to fill whole height of viewport with grid?
    Thank you :)
    Last edited by BigBear; 01-26-2010 at 05:56 PM. Reason: wrong formulation

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You're likely calling getSize before the JScrollPane has been rendered, before pack() or setVisible(true) has been called on its root container.

    As for filling the viewport with the grid, usually the jscrollpane takes care of this for you. You may wish to post code with pics of what you have and what you want.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The dimension of scroll pane is (0, 0) (when I call pane.getSize()
    Use getPreferredSize before realization, getSize after, as mentioned.
    is possible to fill whole height of viewport with grid?
    Maybe.
    This might give you some ideas.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class TableSize {
        JScrollPane scrollPane;
    
        private JScrollPane getCenterComponent() {
             scrollPane = new JScrollPane();
             scrollPane.setPreferredSize(new Dimension(400,400));
             return scrollPane;
        }
    
        private JPanel getLastPanel() {
            JButton button = new JButton("Set table in scrollpane");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Dimension extentSize = scrollPane.getViewport().getExtentSize();
                    System.out.printf("viewport size: [%d, %d]%n",
                                       extentSize.width, extentSize.height);
                    DefaultTableModel model = new DefaultTableModel(1, 1);
                    JTable table = new JTable(model);
                    int cols = 4;
                    int rowHeight = table.getRowHeight();
                    int headerHeight = table.getTableHeader()
                                            .getPreferredSize().height;
                    int rows = (extentSize.height - headerHeight)/rowHeight;
                    System.out.printf("rowHeight: %d  headerHeight: %d  rows: %d%n",
                                       rowHeight, headerHeight, rows);
                    model.setColumnCount(cols);
                    model.setRowCount(rows);
                    scrollPane.getViewport().setView(table);
    
                    Dimension d = table.getPreferredSize();
                    System.out.printf("table prefSize: [%d, %d]%n",
                                       d.width, d.height);
                    int extraHeight = extentSize.height - headerHeight - d.height;
                    System.out.printf("extraHeight: %d%n", extraHeight);
                }
            });
            JPanel panel = new JPanel();
            panel.add(button);
            return panel;
        }
    
        public static void main(String[] args) {
            TableSize test = new TableSize();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getCenterComponent());
            f.add(test.getLastPanel(), "Last");
            f.pack();
            f.setLocation(100, 100);
            f.setVisible(true);
        }        
    }
    If you ignore the headerHeight you'll get one more row and a vertical scrollBar.

  4. #4
    BigBear is offline Member
    Join Date
    Jan 2010
    Posts
    37
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You're likely calling getSize before the JScrollPane has been rendered, before pack() or setVisible(true) has been called on its root container.

    As for filling the viewport with the grid, usually the jscrollpane takes care of this for you. You may wish to post code with pics of what you have and what you want.

    ad 1) yes, I called it before pack(). This is because I have class TablePage, which extends JPanel and I set instance of TablePage to contentPane of frame. In this class I write everything about this "page". So if I'll need some modifications, what need any size of some panel, I have to do them additionally, am I?
    Btw the width of table I fixed this way.

    ad 2) I have table with few rows and viewport of pane is more higher than it. If I call
    Java Code:
    table.setFillsViewportHeight(true);
    white background of table fills viewport. Rows with data have grid and the rest of viewport is just white area. I want grid in whole viewport.
    I think that answer by hardwired could help.

    Thank you :)

Similar Threads

  1. Coloring JTable cells
    By ProgrammingPup in forum Advanced Java
    Replies: 2
    Last Post: 11-04-2009, 10:57 PM
  2. How to merge cells of JTable
    By nehaa in forum AWT / Swing
    Replies: 1
    Last Post: 05-19-2009, 01:07 PM
  3. Color cells and rows in SWT table
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:31 PM
  4. Replies: 0
    Last Post: 07-11-2008, 03:07 PM
  5. merging selected table cells using javascript
    By Renjini in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-17-2008, 01:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •