I have the following scenario: a JFrame which contains several JPanels that are selected using a CardLayout. One JPanel contains a pretty large JTable which I have wrapped in a JScrollPane and added the JScrollPane to the main JPanel:
package com.javaswingcomponents.demo;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class MyPanel extends JPanel {
private DefaultTableModel model = new DefaultTableModel();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setBounds(100, 100, 450, 535);
frame.getContentPane().add(new MyPanel());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyPanel() {
JTable table = new JTable(model);
JScrollPane scrollPaneParte = new JScrollPane(table);
scrollPaneParte.setPreferredSize(new Dimension(600, 600));
scrollPaneParte.setBounds(10, 92, 867, 92);
add(scrollPaneParte);
scrollPaneParte.setViewportView(table);
addColumns(model);
}
public void addColumns(DefaultTableModel model) {
for (int ii=0;ii<10;ii++)
model.addColumn("Field"+ii);
}
}
I initially thought the problem was the XY (null) layout used, but even changing to BorderLayout nothing happens, that is, the ScrollBar is not displayed and only a part of the JTable is shown.
Any idea how to solve this issue?
EDIT: I have included a full working example which reproduces the issue.