JTable with a ScrollPane misbehaving

I have a JPanel component with a JTable inside of it. When I run the code as it's written below, the table renders and updates correctly. As soon as I try to use the scrollPane approach, the table does not render at all. Can anyone explain to me why this is?

private static class GameHistoryPanel extends JPanel { private DataModel model; private JTable table; private int currentRow; private int currentColumn; private final Dimension HISTORY_PANEL_DIMENSION = new Dimension(190,460); public GameHistoryPanel() { this.setLayout(new BorderLayout()); this.model = new DataModel(); this.table = new JTable(model); this.add(table.getTableHeader(), BorderLayout.NORTH); this.add(table, BorderLayout.CENTER); // JScrollPane scrollPane = new JScrollPane(); // scrollPane.setViewportView(table); // this.add(scrollPane); setPreferredSize(HISTORY_PANEL_DIMENSION); this.currentRow = 0; this.currentColumn = 0; } public void increment(Board board, Move move) { model.setValueAt(move, currentRow, currentColumn); if(board.currentPlayer().getAlliance() == Alliance.WHITE) { currentColumn++; } else if (board.currentPlayer().getAlliance() == Alliance.BLACK) { currentRow++; currentColumn = 0; } validate(); repaint(); } }


It appears that you are using a JTable as view of a TableModel in which each cell exists in one of two states. Visible changes to a cell should result only from a change to the model, which may be examined in preparing the cell's renderer. In particular, invoking the methods validate() and repaint() should not be required. Their presence suggests that you are altering the view without the model's knowledge, which could explain the anomaly seen.



JScrollPane scrollPane = new JScrollPane(table); this.add(scrollPane);


This may be stating the obvious, but remember that you can only add a JComponent to a container <strong>once</strong>.


this.setLayout(new BorderLayout()); this.model = new DataModel(); this.table = new JTable(model);

either you

this.add(table, BorderLayout.CENTER); // note that table-headers should not be added explicitly // commented out: this.add(table.getTableHeader(), BorderLayout.NORTH);

or you

JScrollPane scrollPane = new JScrollPane(table); this.add(scrollPane);

but trying to do both will result in problems.

I recommend using Swingx's JXTable, if at all possible. Much more flexible, and out-of-the-box column sorting and hiding/reordering.


  • Macro to copy a row with an empty cell in any of the last 5 columns to other sheet
  • How to remove extra white space from the bottom of the JTable?
  • Android: How to keep corner radius while setting a background drawable?
  • Android border bottom for custom view not working
  • spatie/laravel-analytics performQuery error
  • How to make a reader app where text should adjust in multiple pages according to screen size and fon
  • How to set my android layout in all devices?
  • Android Textview text cut off at bottom
  • Layout for Fragment overlapping Toolbar
  • How insert a LinearLayout between AppBarLayout and the scrolling content, when using CollapsingToolb
  • ImageSwitcher is not showing outAnimation
  • Doing a URL Re-Write
  • Extract value from multidimensional array and place in comma separated string
  • How to overlap java graphics and an Image and make it look nice?
  • Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException if-statement
  • Display some part of text in bold and some part in italic in SWT label
  • How to make the JDialog invisible when the JProgressBar reaches 100%?
  • Java Listen to Button and Keyboard click
  • How to add a scrollbar for long JComponents in a JPanel? How to center a JComponent?
  • How to create vertical TitledBorder in JPanel (javax swing)
  • Custom jQuery Pseudo-selector Receiving Undefined Arguments
  • geom_map “map_id” reference issue
  • adMob | Extend Banner size?
  • Custom auto hide floatingActionButton behavior is not working
  • GridBagLayout padding
  • Getting proper map boundaries
  • setSelected() with JRadioButton r[]=new JRadioButton[3] not working [duplicate]
  • How to rotate the dial in a gauge chart? Using python plotly
  • Plotting densities in R
  • Update Google Maps traffic layer without page reloading
  • Linq Merge lists
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Installing iPhone App to iPhone
  • how to adjust image in a panel in Java swing?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • UserPrincipal.Current returns apppool on IIS