How to make JTable without JScrollpane

I did a pretty decent search, and I can't say what I'm looking for. basically I want JTable in JPanel, and for this, JPanel has to resize the JTable size itself. I know that the usual way to implement JTable is to use JScrollpane, but in this case it is not needed and it makes my program look ... dirty.

the reason why I can not find the answer that I need, every time someone asks about JTable in JPanel, someone gives them an answer to enter it in scrollpane. im aware of the benefits of using scrollpane, but in this case they are not profitable.

+4
source share
2 answers
  • yes it is possible to add JTable to JPanel , pay attention to a few lines that fill the actual screen resolution

  • you need to add TableHeader separatelly to the container, otherwise it will not be added and not be visible, for JTable in JScrollPane do not need to add or define TableHeader separatelly

  • it would be better to remove borders from JScrollPane.setBorders(null) and with disable VERTICAL and HORIZONTAL_SCROLLBAR to NEVER

  • to reduce the width of the JScrollPane suitable for JTables Dimension , you can use table.setPreferredScrollableViewportSize(table.getPreferredSize());

+7
source

Try overriding the getPreferredSize() method of your JPanel and returning the value of your JTable getPreferredSize() :

 final JTable table = new JTable(); JPanel panel = new JPanel() { public Dimension getPreferredSize() { return table.getPreferredSize(); } } 
0
source

All Articles