1

I created a table using JTable but it appears smaller than the panel as you see in the picture bellow ...

enter image description here

the code is

            tableModel1 = new DefaultTableModel(data1,column_header1);

            table_1 = new JTable(tableModel1);
            JScrollPane scrollPane1 = new JScrollPane(table_1);


            scrollPane1.setBounds(1, 1, 1050, 500);
            panel_3.add(scrollPane1);

mKorbel
  • 109,525
  • 20
  • 134
  • 319
monaalhumud
  • 57
  • 1
  • 7
  • 2
    What is the layout manager on `panel_3`? It should probably be `BorderLayout` and you should be adding the scroll pane at `BorderLayout.CENTER`. – TT. May 08 '17 at 11:30

2 Answers2

2

We really need to know the layout manager you are using on "panel_3". The best place to put a JScrollPane is in the CENTER position of a BorderLayout. This is because the CENTER position gets all remaining space. If you resize the window it will grow/shrink as necessary.

On panel_3 set your layout to BorderLayout. Then add your JScrollPane to the CENTER position:

panel_3.setLayout(new BorderLayout());
panel_3.add(scrollPane1, BorderLayout.CENTER);

Then remove that call to setBounds().

Michael
  • 2,683
  • 28
  • 30
-1

I Assume your jtable contents are fit to jpanel.. Try to add more rows in the jtable so that vertical scrollbars can possibly appear. Technically vertical/horizondal scroll bars appears on neccessary situation . In your case no possibility of vertical scrollbar since you have only 4 rows and no horizondal also since your columns width are fit to the panel width... Add the property in your code and verify..

scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
PrabaharanKathiresan
  • 1,099
  • 2
  • 17
  • 32