I'm quite new to Java Programming and I found this code on the internet. It works fine there is no problem. I downloaded the png's of every fruit and I want to see them when I click on them instead of seeing their names. For example, if I click only apple, I want to see the png of apple. But if I use shift and multiple select grape and banana, I want to see their pngs at the right. (If it is possible of course)
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class Ğ extends JFrame
{
//Sample 02: Create a Label
JLabel label = new JLabel();
public Ğ(String title) throws HeadlessException
{
super(title);
//Sample 01: Set Size and Position
setBounds(100, 100, 200, 200);
Container ControlHost = getContentPane();
ControlHost.setLayout(new GridLayout(1,1));
//Sample 03: Create List of Fruit Items
String[] Fruits = new String[10];
Fruits[0] = "Apple";
Fruits[1] = "Mango";
Fruits[2] = "Banana";
Fruits[3] = "Grapes";
Fruits[4] = "Cherry";
Fruits[5] = "Lemon";
Fruits[6] = "Orange";
Fruits[7] = "Strawberry";
Fruits[8] = "Watermelon";
Fruits[9] = "Ananas";
//Sample 04: Create JList to Show Fruit Name
JList ListFruits = new JList(Fruits);
ListFruits.setVisibleRowCount(10);
//Sample 05: Hand-Over the JList to ScrollPane & Display
JScrollPane jcp = new JScrollPane(ListFruits);
ControlHost.add(jcp);
ControlHost.add(label);
ListFruits.setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
//Sample 06: Handle the JList Event
ListFruits.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
String strFruits = "";
List<String> SelectedFruits = ListFruits.getSelectedValuesList();
for(String Fruit: SelectedFruits)
strFruits = strFruits + Fruit + ",";
strFruits = strFruits.substring(0, strFruits.length()-1);
label.setText(strFruits);
}
});
}
}
And this is the main class of course
import javax.swing.JFrame;
public class Fruits {
public static void main(String[] args) {
// TODO Auto-generated method stub
Ğ frame = new Ğ ("Fruits");
frame.setVisible(true);
}
}