0

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);
         }
      }
   }
}  
Usman Bashiru
  • 125
  • 2
  • 9
  • Possible duplicate of [Scale the ImageIcon automatically to label size](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size) – Titus Apr 09 '16 at 16:13

1 Answers1

0
  JPanel imagePanel = new JPanel();
  imagePanel.add(imageLabel);

First of all a JPanel uses a FlowLayout by default. So components are displayed at their preferred size, so you label will never resize.

So you need to change the layout so that the label can resize to the space available:

  JPanel imagePanel = new JPanel( new BorderLayout() );

Then you can use the Stretch Icon. This Icon will automatically scale itself to fill the space available in its parent component.

Another option is to paint the image on a panel yourself then you can scale the image as you paint it. You need to override the paintComponent() method of your panel:

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Dimension d = getSize();
    g.drawImage(image, 0, 0, d.width, d.height, this);

}

Read the section from the Swing tutorial on Custom Painting for more information and examples.

Or you can check out the Background Panel which is a fancier implementation of this approach. It allows you to paint the image 1) scaled, 2) tiled 3) at its original size.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • does the paintComponent method go inside the ListListener event class or is it it's own entity? and The only error I'm getting is that in the paintComponent method cannot find image symbol – Usman Bashiru Apr 09 '16 at 18:22
  • @UsmanBashiru, read the Custom Painting tutorial and download the demo code and test the demo code so you understand the basics. Then you can customize it. Keep a link to the Swing tutorial for a reference to Swing basics that you can learn on your own. Or you can download the BackgroundPanel and look at the code there to see how custom painting works. – camickr Apr 09 '16 at 18:29