1

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();
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hosseinmp76
  • 317
  • 2
  • 12

2 Answers2

1

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.

Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
  • Awesome. wow it worked really thanks. but can you tell me why – Hosseinmp76 May 24 '17 at 19:26
  • @Hosseinmp76 Yes, because the picture size is fixed and JSplitpane cannot change it so just I added minimum size for label to when you want to move the split doesn't look at label size.(small hack).Dont forget set the answer to correct please. – Coder ACJHP May 24 '17 at 19:29
  • 1
    *"so just I added minimum size for label ...(small hack)."* It's a large hack and it will come back to bite the programmer in the posterior. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) Note that the OP ***seems*** to be trying to use a **background image**, indicating their entire approach is wrong. – Andrew Thompson May 24 '17 at 19:39
0

It's related to image on jlabel.

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class A extends JFrame {

    private static final long serialVersionUID = 1L;

    public A() {
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);

        JPanel left = new JPanel();
        BufferedImage myPicture = null;
        try {
            myPicture = ImageIO.read(new File("a.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(A.class.getName()).log(Level.SEVERE, null, ex);
        }
        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);

        add(splitPane, BorderLayout.CENTER);
        setSize(700, 700);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        splitPane.setDividerLocation(0.5);

    }

    public static void main(String[] args) {
        new A();
    }
}
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38