I'm using spilt panels that has a list of images that can be clicked on the left and then shown on the right, I have somewhat large images. I want to make the images scale-able with the window size. so if I drag my mouse over near the EXIT button and make the window larger then the picture gets larger and vise versa for smaller. at the moment my JFrame is FIXED default window size, but even then the images are too large to be fully seen.
here's my code:
Driver class:
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);
frame.pack();
frame.setSize(500, 300);
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);
frame.getContentPane().add(sp);
frame.setVisible(true);
}
}
ListPanel class:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class ListPanel extends JPanel
{
private JLabel label;
private JList list;
public ListPanel(JLabel imageLabel)
{
label = imageLabel;
String[] fileNames = { "Denali2.jpg",
"denali.jpg",
"MauiLaPerouseBay.jpg",
};
list = new JList(fileNames);
list.addListSelectionListener(new ListListener());
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(list);
setBackground(Color.white);
}
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);
}
}
}
}