2

I am trying to add a JTable to a JScrollPane. But I can't see the Table after doing this.

scrollpane.setBounds(100,50,800,400);
JTable table = new JTable(myTableModel(res));
scrollpane.add(table);

What's wrong with me?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Have you added `scrollpane` to your frame/panel? – PakkuDon Jan 12 '14 at 07:48
  • 2
    For more details see [How to use Scroll Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) – MadProgrammer Jan 12 '14 at 07:54
  • 2
    unrelated: don't do any manual sizing/locating **ever** - that's the exclusive task of a suitable LayoutManager – kleopatra Jan 12 '14 at 08:52
  • 2
    `scrollpane.setBounds(100,50,800,400);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jan 12 '14 at 08:59
  • possible duplicate of [JTable doesn't show after adding JScrollPane](http://stackoverflow.com/questions/20911857/jtable-doesnt-show-after-adding-jscrollpane) – dic19 Jan 12 '14 at 19:24

3 Answers3

6

scrollpane.add(table);

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Try this:

JTable table = new JTable(myTableModel(res));
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setBounds(100,50,800,400);
neoprez
  • 349
  • 2
  • 11
-1

Try this

scrollpane.setBounds(100,50,800,400);

JTable table = new JTable(myTableModel(res));

scrollpane.setViewporView(table);
RAJIL KV
  • 399
  • 1
  • 7
  • 24