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.