javaswingjsplitpane

Why split pane divider does not move when two panels are added in it?


I have written a simple program. I use a JSplitPane in center of JFrame and I want to show two picture in it's two sides so I use two JLabel components and I put them in a JPanel. but divder does not move when I run the code.

public class A extends JFrame
{
private static final long serialVersionUID = 1L;

public A() throws IOException
{
    getContentPane().setLayout(new BorderLayout(0, 0));

    JSplitPane splitPane = new JSplitPane();
    getContentPane().add(splitPane, BorderLayout.CENTER);

    JPanel left = new JPanel();
    BufferedImage myPicture = ImageIO.read(new File("a.jpg"));
    JLabel lblNewLabel = new JLabel(new ImageIcon(myPicture));
    left.add(lblNewLabel);

    splitPane.setLeftComponent(left);

    JPanel right = new JPanel();
    JLabel label = new JLabel(new ImageIcon(myPicture));
    right.add(label);

    splitPane.setRightComponent(right);
}

public static void main(String[] args) throws IOException
{
    A a = new A();
    a.setSize(700, 700);
    a.show();
}
}

Solution

  • This code enabling to divider moving regardless size of photo :

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JSplitPane;
    
    public class Test extends JFrame {
    
        public Test () throws IOException {
            getContentPane().setLayout(new BorderLayout(0, 0));
    
            JSplitPane splitPane = new JSplitPane();
            getContentPane().add(splitPane, BorderLayout.CENTER);
            splitPane.setOneTouchExpandable(true);
            splitPane.setContinuousLayout(true);
            splitPane.resetToPreferredSizes();
    
            BufferedImage myPicture = ImageIO
                    .read(new URL("http://freecodebank.com/wp-content/uploads/2016/07/joinus-java.png"));
            JLabel lblNewLabel = new JLabel(new ImageIcon(myPicture));
            lblNewLabel.setMinimumSize(new Dimension(100, 80));
            splitPane.setLeftComponent(lblNewLabel);
    
            JLabel label = new JLabel(new ImageIcon(myPicture));
            label.setMinimumSize(new Dimension(100, 80));
            splitPane.setRightComponent(label);
        }
    
        public static void main(String[] args) throws IOException {
            Test a = new Test();
            a.setPreferredSize(new Dimension(1400, 900));
            a.pack();
            a.setVisible(true);
        }
    }
    

    Good luck.