1

I need a program that displays many images and I need window that can be scrolled for that. I read the documentation and searched on the forum but I still didn't manage to do it. I tried with JScrollPane and JFrame as you can see below.

JScrollPane class:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class EmojiWindow extends JScrollPane {
    
    private void newImg(String emojiLocation, String emojiName) {
        JLabel emoji = new JLabel(new ImageIcon(emojiLocation));
        Emoji.setToolTipText(emojiName);
        add(emoji);
        
        Emoji.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent arg0) {}
            public void mouseEntered(MouseEvent arg0) {}
            public void mouseExited(MouseEvent arg0) {}
            public void mouseReleased(MouseEvent arg0) {}
            
            @Override
            public void mousePressed(MouseEvent e) {
                if(SwingUtilities.isLeftMouseButton(e))
                {
                    JFrame frame = new JFrame("new frame");
                    frame.setSize(300, 10);
                    frame.setVisible(true);
                }
            }
        });
    }

    public EmojiWindow(){
        newImg("fbike.png", "Riding a bike");
        newImg("fdizzy.png", "Dizzy");
        newImg("fcubehead.png", "Cube head");
        newImg("fhappy.png", "Happy");
    }
}

Main:

import java.awt.*;
import javax.swing.*;

public class Main {
    
    public static void main(String[] args)
    {
        EmojiWindow scrollPane = new EmojiWindow();
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        JFrame window = new JFrame();
        
        window.add(scrollPane, BorderLayout.SOUTH);
        window.setSize(300, 400);
        window.setVisible(true);
    }
}

Edit: Changed the variables' and methods' names to camel-case to stop triggering people.

KON
  • 91
  • 12
  • set properties to `VERTICAL_SCROLLBAR_AS_NEEDED` for horizontal scrolling `HORIZONTAL_SCROLLBAR_AS_NEEDED` for vertical scrolling – Ulug Toprak May 04 '17 at 14:26
  • 1
    1) [This answer](http://stackoverflow.com/a/9544652/418556) shows many images in a `JList` in a `JScrollPane`. It is best suited to images that are all the same size. While for differently sized or large images we could implement a `ListCellRenderer` that scales image to a certain maximum width and/or height. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 3) `// TODO Auto-generated method stub` Can you guess what you're supposed **to do** with these comments after code that is **not** auto-generated exists? – Andrew Thompson May 04 '17 at 16:29
  • 1
    .. 4) `JFrame Window = new JFrame();` Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. Further, don't use the name of an existing class of the standard API, make it a name that is more specific to the use. It is very confusing. 5) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson May 04 '17 at 16:33
  • 1
    .. 6) Rather than use a `JLabel`/`MouseListener` combo. for displaying the images, I'd recommend using a (possibly undecorated) `JButton` with an `ActionListener`. The button can gain keyboard focus by default, and the action listener will react to both mouse **and** keyboard input. – Andrew Thompson May 04 '17 at 16:37

1 Answers1

3

First of all, learn and follow Java naming conventions. Variable names should NOT start with an upper case character. Any example you find in this forum or text book uses this convention. Learn by example!!!

Don't extend JScrollPane. There is no need to do this as you are not adding any new functionality to the class.

Also, never add components to a JScrollPane. A single component is added to the JViewPort of the scroll pane.

So in this case you would create a JPanel using an appropriate layout manager. Then you add the panel to the viewport of the scroll pane.

So the basic code might be something like:

JPanel imagePanel = new JPanel();
imagePanel.add( label1 );
imagePanel.add( label2 );
...
JScrollPane scrollPane = new JScrollPane( imagePanel );
window.add( scrollPane );

Read the Swing Tutorial for working examples of all the Swing basics.

Edit:

You can also try the Wrap Layout which will cause components to wrap to the next line when a horizontal row is full.

camickr
  • 321,443
  • 19
  • 166
  • 288