Register / Login
Win a copy of
this week in the
this forum made possible by our volunteer staff, including ...
Stephan van Hulst
Swing / AWT / SWT
re: Could I add a scroll bar to my JTable?
posted 2 years ago
My JTable is getting too big where I have all eight columns to display , May I add an horizontal or vertical scroll bar to my JTable?
May I explain to you my situation so your may have a better understanding of my situation.
I have a Jframe . In the Jframe, I create a North Panel, a West Panel, and a South Panel for display different kind of data.
In my West Panel, I have a GridBagConstraint called tableGC as in the code below. TableGC have two rows. The first row contains my table, and the
second row contain the submit button.
Dimension dim = tablePanel.getPreferredSize(); dim.width = 600; westPanel.setPreferredSize(dim); westPanel.setLayout(new GridBagLayout()); GridBagConstraints tableGC = new GridBagConstraints(); // ////////// First row /////////////////////////////////// tableGC.weightx = 1; tableGC.weighty = 0.1; tableGC.gridx = 0; tableGC.gridy = 0; tableGC.fill = GridBagConstraints.NONE; tableGC.anchor = GridBagConstraints.CENTER; tableGC.insets = new Insets(0, 0, 0, 5); table.setPreferredScrollableViewportSize(new Dimension(598, 10)); table.setFillsViewportHeight(true); westPanel.add(table, tableGC); // //////////Second row /////////////////////////////////// tableGC.weightx = 1; tableGC.weighty = 0.1; tableGC.gridx = 0; tableGC.gridy = 1; tableGC.insets = new Insets(0, 0, 0, 5); tableGC.anchor = GridBagConstraints.CENTER; westPanel.add(submitBtn, tableGC);
My question is that could I make my table scroll-able on both horizontal and vertical direction . I was googling and someone told me that I need
to create a JScrolPanel and instantiated it with my Jtable and, add the new tableScrollPanel to my westPanel as my gridbaglayout element.
I followed them and did what I was taught as followed in the following code
JScrollPane tableScrollPane = new JScrollPane(table); westPanel.add(tableScrollPane, tableGC);
But it did not work, it only showed me a very little bit tiny table on my westPanel.
So I would like to seek your help and expertise on how should I make my table scroll-able and my table is in the first row of a gridbaglayout , and the
gridbaglayout is in the westPanel?
Many Thanks for all of your helps!!
posted 2 years ago
If you want horizontal scrolling then you need to use:
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
Don't get me started about those stupid
Boost this thread!
Problem displaying JTable inside a JScrollPane
How to keep a GridBagLayout horizontal-axis responsive on resize and always show a vertical scroller
jtable rendering problem