0

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);

    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
krky
  • 1
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). That code *almost* is, but move the `main` method into the `Ğ` class (but give it a sensible, descriptive name) and reduce the fruits to just two. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jun 02 '21 at 21:33
  • 3) `String[] Fruits` should be `String[] fruits`. (Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently.) 4) I'd **approach the problem by displaying the images in a second `JList`.** – Andrew Thompson Jun 02 '21 at 21:36
  • See: https://stackoverflow.com/questions/67755916/how-can-i-change-the-background-color-by-pressing-left-and-right-arrow-keys-from for a solution. – camickr Jun 02 '21 at 22:06

0 Answers0