0

The JTable doesn't appear in the form.
This is a portion of my code:

table = new JTable(tasks, names);
table.setBounds(10, 43, 408, 455);
contentPane.add(table);
JScrollPane scroll = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
contentPane.add(scroll);

But when I removed the JScrollPane, it appears.
EDIT: I used AbsoluteLayout in Eclipse's drag and drop function.

Kenneth Sumang
  • 61
  • 1
  • 1
  • 11

1 Answers1

3

contentPane.add(table); isn't required and it looks like you're using a null layout which probably accounts for the fact that the table doesn't appear (as the JScrollPane has a default size of 0x0

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Take a look at Laying Out Components Within a Container for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I used AbsoluteLayout in Eclipse's drag and drop. – Kenneth Sumang Apr 15 '16 at 11:49
  • 1
    *"I used AbsoluteLayout .."* Yes. That is worse than using a `null` layout. It is just as bad (functionally) as a `null` layout, but gives the programmer the impression they have 'absolute' control over component placement and bounds - when they don't. Or to put that a different way to above: Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) .. – Andrew Thompson Apr 15 '16 at 11:51
  • 1
    .. along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Apr 15 '16 at 11:52
  • *"I used AbsoluteLayout in Eclipse's drag and drop."* - Congratulations, you've just learnt one of the many reasons we discourage form editors. I would, instead, strongly encourage you to change the layout manager within the form editor or, preferably, not use them till you have a better understanding of the layout management API (and other design habits). You are free of course to continue using them, but understand, they tend to make your life harder in the long run (I'm speaking from experience) – MadProgrammer Apr 15 '16 at 11:56