I tried to use Jxlayer and PBar extensions (using the response of MadProgrammer from here) to add the zoom capability to my JPanel which is contained in a JScrollPanel. The zoom itself work fine but when the zoomed panel becomes larger than the containing JFrame the scrollbars doesn’t adjust and stay inactive. Here is my code:
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
Integer[] zoomList = {50, 75, 100, 125, 150, 200};
JComboBox<Integer> zoomBox = new JComboBox<>(zoomList);
zoomBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int value = (int) zoomBox.getSelectedItem();
double scale = value / 100d;
transformModel.setScale(scale);
}
});
topPanel.add(zoomBox);
Panel centerPanel = new TestPane();
JScrollPane scrollPane = new JScrollPane(synopticPanel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.getViewport().setOpaque(false);
scrollPane.setOpaque(false);
frame.add(topPanel, BorderLayout.NORTH);
frame.add(synopticPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
and in the Testpane class
public class TestPane extends JPanel {
private JLayer<JComponent> layer;
private JPanel content;
public TestPane() {
content = new JPanel();
content.setLayout(null);
//Adding some components
transformModel = new DefaultTransformModel();
transformModel.setScaleToPreferredSize(true);
layer = TransformUtils.createTransformJLayer(content, transformModel, null);
setLayout(new BorderLayout());
add(layer);
}
Thank you for your help