1

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

Community
  • 1
  • 1
Aymen
  • 11
  • 1
  • hmm ... JLayer or JXLayer? Not sure if the transforms will work on the former – kleopatra Apr 25 '14 at 10:42
  • isn't JXLayer now forms part of the JDK7 as JLayer? – Aymen Apr 25 '14 at 11:01
  • yeah, but slightly different. And Piet's transform code is based on the old JXLayer. So I would be surprised if you could mix it without problems (had to adjust quite a bit, last time I looked) - might work, though, did nothing with it recently, just saying ... :-) – kleopatra Apr 25 '14 at 11:05

0 Answers0