Results 1 to 4 of 4
  1. #1
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default Table not showing up

    Hey

    I am making a table with a custom table model, here is my code

    PHP Code:
        public void table() {
            String headers[] = {"Numbers", "Alphabet"};
            TableModel model = new SparseTableModel(10, headers);
            model.setValueAt("one", 0, 0);
            model.setValueAt("ten", 9, 0);
            model.setValueAt("F", 5, 1);
            model.setValueAt("I", 8, 1);
            JTable table = new JTable(model);
            table.setPreferredScrollableViewportSize(new Dimension(500, 120));
            table.setFillsViewportHeight(true);
            scrollPane.add(table);
            pack();
        }
    
        class SparseTableModel extends AbstractTableModel {
    
        private Hashtable lookup;
        private final int rows;
        private final int columns;
        private final String headers[];
    
        public SparseTableModel(int rows, String columnHeaders[]) {
            if ((rows < 0) || (columnHeaders == null)) {
                throw new IllegalArgumentException("Invalid row count/columnHeaders");
            }
            this.rows = rows;
            this.columns = columnHeaders.length;
            headers = columnHeaders;
            lookup = new Hashtable();
        }
    
        public int getColumnCount() {
            return columns;
        }
    
        public int getRowCount() {
            return rows;
        }
        public Object getValueAt(int row, int column) {
            return lookup.get(new Point(row, column));
        }
    But the problem is that the table doesn't show up, even though i add it to a scrollPane which is on the main Jframe but it still doesn't show up. Any ideas

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    dilpreet28 is offline Member
    Join Date
    Jun 2010
    Posts
    23
    Rep Power
    0

    Default

    problem solved
    just needed to set the layout manager of the JPanel to borderLayout
    jPanel.setLayout(new BorderLayout());

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    With container it's easy to handle.

    Java Code:
    JTable table = new JTable(rows_object_array, columns_object_array);
        JScrollPane scrollPane = new JScrollPane(table);
        content.add(scrollPane, BorderLayout.CENTER); // content is the container...

Similar Threads

  1. Replies: 1
    Last Post: 01-08-2010, 06:19 AM
  2. Components not showing
    By byte237 in forum AWT / Swing
    Replies: 2
    Last Post: 12-27-2009, 06:05 PM
  3. Showing
    By bostonstate in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 07:49 PM
  4. Showing ResultSet in HTML table
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-27-2008, 08:09 PM
  5. Why isn't this showing?
    By JToolTip in forum Java Applets
    Replies: 2
    Last Post: 07-07-2007, 11:54 PM

Posting Permissions

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