Question: I need helping adding three pictures into this split pane program. I have the correct name of the images appearing on the left side of
I need helping adding three pictures into this split pane program. I have the correct name of the images appearing on the left side of the pane, but I need to click on them and have them appear on the right which I can't figure out. THIS PROGRAM IS IN JAVA. THIS CODE NEEDS TO BE USED. Not an entirely new program.
// Demonstrates the use a split pane and a list.
import java.awt.; import javax.swing.; public class PickImage{ //----------------------------------------------------------------- // Creates and displays a frame containing a split pane. The // user selects an image name from the list to be displayed. //----------------------------------------------------------------- public static void main(String[] args) { JFrame frame = new JFrame("Pick Image"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel imageLabel = new JLabel(); JPanel imagePanel = new JPanel();
imagePanel.add(imageLabel); imagePanel.setBackground(Color.white); ListPanel imageList = new ListPanel(imageLabel); JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, imageList, imagePanel); sp.setOneTouchExpandable( true ); sp.setDividerLocation(150);
frame.getContentPane().add(sp); frame.pack(); frame.setVisible( true ); } } // Demonstrates the use a split pane and a list. //******************************************************************** import java.awt.; import javax.swing.; import javax.swing.event.*; public class ListPanel extends JPanel {
private JLabel label; private JList list;
//----------------------------------------------------------------- // Loads the list of image names into the list. //----------------------------------------------------------------- public ListPanel(JLabel imageLabel) {
label = imageLabel; String[] fileNames = { "monster1.gif", "monster2.gif", "monster3.gif", };
list = new JList(fileNames); list.addListSelectionListener(new ListListener()); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); add(list); setBackground(Color.white); } //***************************************************************** // Represents the listener for the list of images. //***************************************************************** private class ListListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent event) { if (list.isSelectionEmpty()) label.setIcon(null); else { String fileName = (String)list.getSelectedValue(); ImageIcon image = new ImageIcon(fileName); label.setIcon(image); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
