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);
}
}