0

It has been identified as a possible duplicate of the question:

JPanel Window not scaling when resize the main frame

I do not agree, because at that topic the problem is that the JPanel does not resize. In my case, the JPanel has no problems, but the JScrollPane is not getting resized!

I have a JFrame, there is a JPanel inside, containing a JScrollpane, which contains a JTable.

The problem is, that if the JFrame is resized, the JScrollpane stays the same, doesn't matter if the JFrame is made really small, so that the scroll pane does not even fit in the window or really big.

I want to make the JScrollpane adjust itself according to its JPanel, which gets adjusted by resizing of the window. If the same thing is done by using a JTextArea inside the JScrollPane, everything works just fine -- resizing the JPanel resizes the scroll pane.

Do you have any idea why the JTable ruins the automatic resizing of the JScrollPane, and how to get around it? It seems, that the LayoutManager somehow is not able to handle it properly... (I also tried to add images showing what's happening, but I still don't have enough of reputation...)

Here is the code:

JFrame tableFrame = new JFrame("Test Table");
editPanel vadEditPane;
vadEditPane = new editPanel(vadData);
vadEditPane.setOpaque(true); //content panes must be opaque
tableFrame.getContentPane().add(vadEditPane, BorderLayout.CENTER);

public class editPanel extends JPanel {
    public editPanel(dataConv vadData){
        JTable vadTable;
        String[] columnNames = {---SOME COLUMN NAMES---};
        Object[][] data = {---SOME DATA---};
        vadTable = new JTable(data, columnNames);
        vadTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
        vadTable.setFillsViewportHeight(true);
        vadTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        JScrollPane scrollPane = new JScrollPane(vadTable);
        scrollPane.setBackground(Color.RED);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane);
        setBackground(Color.ORANGE); 
        }
    }
Community
  • 1
  • 1
Vytautas
  • 3
  • 2
  • 1
    You should check the default layout of `JPanel`, this is not a `BorderLayout` like JFrame. – AxelH Dec 05 '16 at 07:32
  • 1
    Possible duplicate of [JPanel Window not scaling when resize the main frame](http://stackoverflow.com/questions/33433485/jpanel-window-not-scaling-when-resize-the-main-frame) – AxelH Dec 05 '16 at 07:34

1 Answers1

0

I do not agree, because at that topic the problem is that the JPanel does not resize. In my case, the JPanel has no problems, but the JScrollPane is not getting resized!

If you read the comment in the first answer and take the time to understand the suggestion you will indeed find that the solution is the same.

The answer there states that a JPanel uses a FlowLayout (by default). The FlowLayout respects the preferred size of any component added to it. So the size of the table will never change even as the frame size changes.

So the solution suggests to change the layout manager of the panel so that the child components can be resized as the size of the panel changes. I would suggest using a BorderLayout.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks! I am sorry, lack of experience in JAVA did not let me to understand the suggestion at the first time. "setLayout(new BorderLayout())" solved the problem! – Vytautas Dec 06 '16 at 09:41