This is the code I wrote. It works only problem I can't resize the images. I've tried out getScaledInstance
wasn't working. Any ideas on how to fix this. Also, why did I have to use a container to show the pictures shouldn't the panels be enough to show the results but until I added the container I wasn't getting any outputs.
package split;
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.Container;
public class Split extends JFrame implements ListSelectionListener{
JFrame f;
JSplitPane s1;
JList list1;
JPanel listP,ImgP;
JLabel img;
Container contentPane;
String a[] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
Split(){
list1 = new JList(a);
list1.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
switch(list1.getSelectedIndex()){
case 0:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Mercury_profile_MDIS_MESSENGER.jpg"));
break;
case 1:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Venus_from_Mariner_10.jpg"));
break;
case 2:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download.jpg"));
break;
case 3:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download (1).jpg"));
break;
case 4:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Jupiter_and_its_shrunken_Great_Red_Spot.jpg"));
break;
case 5:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download (2).jpg"));
break;
case 6:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\download (3).jpg"));
break;
case 7:
img.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\Planets\\Neptune_-_Voyager_2_(29347980845)_flatten_crop.jpg"));
break;
}
}
});
listP = new JPanel();
listP.add(list1);
img = new JLabel();
ImgP = new JPanel();
ImgP.add(img);
s1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,listP, ImgP);
s1.setOneTouchExpandable(true);
s1.setDividerLocation(150);
contentPane = getContentPane();
contentPane.add(s1);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
new Split();
}
@Override
public void valueChanged(ListSelectionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}