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.
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, forJTable
inJScrollPane
do not need to add or defineTableHeader
separatellyit would be better to remove borders from
JScrollPane.setBorders(null)
and with disableVERTICAL
andHORIZONTAL_SCROLLBAR
toNEVER
to reduce the width of the
JScrollPane
suitable forJTables Dimension
, you can usetable.setPreferredScrollableViewportSize(table.getPreferredSize());
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(); } }