2

I've been having trouble trying to get the table to scroll; it just won't. I've looked at other stack answers and tried them, but they aren't working; and I'm not sure if I have something conflicting with those solutions.

tableModel = new TableModel(); //Custom Table Model
table = new JTable();
table.setBorder(null);
table.setFillsViewportHeight(true);
table.setModel(tableModel);

JScrollPane tblScrollPane = new JScrollPane(table);
tblScrollPane.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
tblScrollPane.setBounds(245, 17, 560, 425);
frmServerAdministration.getContentPane().add(tblScrollPane);

EDIT: More info

So a window opens, which is the server program. Client programs connect to the server and when they do, a method is triggered in my table model class which adds a new row into the table. (I can see the row) Then at the end of that method it calls another but nothing changes in the ScrollPane. Do I need to do some kind of repainting? -

Server.updateTableScroll();

public static void updateTableScroll() {
    System.out.println("Here"); //NOTE: I do see this printed out
    int last = table.getModel().getRowCount() - 1;
    Rectangle r = table.getCellRect(last, 0, true);
    table.scrollRectToVisible(r);        
}

EDIT 2: Thoughts

So in Eclipse I use Window Builder to make the GUI, and the following for loop will display the table with the scrollbar! But when I run the same addClient() method at another point, then the scroll bar won't appear.

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Server window = new Server();
                window.frmServerAdministration.setVisible(true);
                for(int i = 0; i < 20; i++){
                    tableModel.addClient(i, String.valueOf(i));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Michael Scott
  • 429
  • 2
  • 8
  • 18

2 Answers2

4

Instead of setBounds(), override getPreferredScrollableViewportSize(), as suggested here, and pack() the enclosing top-level container. Also, the API authors "recommend that you put the component in a JPanel and set the border on the JPanel."

Addendum: As a JTable listens to its TableModel, verify that the correct event is fired from the model. If you extend AbstractTableModel, one of the fireTableXxx() methods will be appropriate.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • This looks very useful, but I can't really follow it. Can you elaborate some? – Michael Scott Aug 06 '13 at 01:01
  • And, which component goes on the JPanel ? – Michael Scott Aug 06 '13 at 01:11
  • Here's a complete [example](http://stackoverflow.com/a/14429388/230513); if it's not helpful, please edit your question to include an [sscce](http://sscce.org/) that exhibits any problem you encounter. – trashgod Aug 06 '13 at 01:12
  • I sincerely appreciate your help. I'm trying to make an sscce but I have a lot of code so in the example, I am copying and pasting code but it's working in the example. Ug – Michael Scott Aug 06 '13 at 01:36
  • In my sscce example, I don't have to do anything to make the scrollbar appear. :(----EDIT-Okay nvm what I said about the example. I can get the same thing to happen in the original, but my original problem still exists. Ill keep working on an scce – Michael Scott Aug 06 '13 at 01:39
  • Compare my example to yours, or simply use mine. – trashgod Aug 06 '13 at 01:40
  • 1
    Verify that the custom table model fires the correct events to notify the table. – trashgod Aug 06 '13 at 02:06
  • I very new to tables and table models, so I'm not quite sure how I would go about that. Also, I added more to the question – Michael Scott Aug 06 '13 at 02:08
1

All I needed to do was call this after I added data (I'm very new to table models :P ) this.fireTableStructureChanged();

Michael Scott
  • 429
  • 2
  • 8
  • 18