| | |
Software Engineer
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 4
Reputation:
Solved Threads: 0
Hi All. Please I need your help.
I have just started work as a Software Engr.
I have a project at hand which includes placing different image icons on a JLabel component depending on which element of a JList is selected.
I used single selection model. I tried using if-else construct to specify which imageicon should be placed on the label if a particular element is selected from the JList, but this did not work. I then commented out the if-else statementst to see if I could get this work. Selection occured but the problem is that only one imageicon is being displayed for whichever element selected.
I have been on this problem for some time now while trying to read up every thing I could find on JList component handling. The deadline for me to come up with the solution to this project is next week, so I need as much help as I can get to come up with the solution.
Any help would be highly appreciated.
Thank you.
KingsKidy.
I have just started work as a Software Engr.
I have a project at hand which includes placing different image icons on a JLabel component depending on which element of a JList is selected.
I used single selection model. I tried using if-else construct to specify which imageicon should be placed on the label if a particular element is selected from the JList, but this did not work. I then commented out the if-else statementst to see if I could get this work. Selection occured but the problem is that only one imageicon is being displayed for whichever element selected.
I have been on this problem for some time now while trying to read up every thing I could find on JList component handling. The deadline for me to come up with the solution to this project is next week, so I need as much help as I can get to come up with the solution.
Any help would be highly appreciated.
Thank you.
KingsKidy.
Some coding would be welcome, reading from magic ball to locate your code issues is difficult these days 
If you can provide some code we will try to help you...

If you can provide some code we will try to help you...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Apr 2009
Posts: 4
Reputation:
Solved Threads: 0
Thank you. The following is the cose snippet of what I intended to do:
Plesae I want to know whether the if-else construct is the best approah in this regard; if not how else should I approach it.
Thank you.
----
KingsKidy.
Java Syntax (Toggle Plain Text)
[//process the Classic button event cbtn1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt) { //String[] Str1 = {"John","James","Moses","Nick","Paul"}; //trying something new here String[] cdtit = displayClassicTitles(); titlelisting = new JList(cdtit); titlelisting.setVisibleRowCount(3); //does not allow multiple selections titlelisting.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); scrollPane = new JScrollPane(titlelisting); // frame.add(scrollPane, BorderLayout.CENTER); ListAndLabelPanel.add(scrollPane,BorderLayout.SOUTH); // create an image icon ImageIcon cdImage = new ImageIcon("C:/CDGraphics/GospelGifs/EasyStars.Gif"); //add the image icon to a label and set label text lblC = new JLabel(cdImage); lblC.setText("Pic of selected title"); ListAndLabelPanel.add(lblC,BorderLayout.CENTER); panelC.add(ListAndLabelPanel,BorderLayout.CENTER); //now add the center panel to the frame frame.add(panelC,BorderLayout.CENTER); frame.setVisible(true); //Handle the JList Event titlelisting.addListSelectionListener(new ListSelectionListener(){ public void valueChanged(ListSelectionEvent event) { if(!event.getValueIsAdjusting()) { if(titlelisting.getSelectedValue()=="OxbridgeBaby") { ImageIcon cdImage1 = new ImageIcon("C:/CDGraphics/ClassicGifs/OxbridgeBaby.Gif"); lblC.setIcon(cdImage1); ShoppingCart selectedItem = new ShoppingCart(); selectedItem.setSelectedTitle1(titlelisting.getSelectedValue().toString()); selectedItem.setTitlePrice1("$10.00"); selectedItem.setVisible(true); }else if(titlelisting.getSelectedValue()=="BrainyBaby") { ImageIcon cdImage2 = new ImageIcon("C:/CDGraphics/ClassicGifs/BrainyBaby.Gif"); lblC = new JLabel(cdImage2); ShoppingCart selectedItem = new ShoppingCart(); selectedItem.setSelectedTitle2(titlelisting.getSelectedValue().toString()); selectedItem.setVisible(true); }else if(titlelisting.getSelectedValue()=="ADozenRoses") { ImageIcon cdImage3 = new ImageIcon("C:/CDGraphics/ClassicGifs/ADozenRoses.Gif"); lblC = new JLabel(cdImage3); ShoppingCart selectedItem = new ShoppingCart(); selectedItem.setSelectedTitle3(titlelisting.getSelectedValue().toString()); } else{ } } } }); } });
Thank you.
----
KingsKidy.
Last edited by Ancient Dragon; Apr 21st, 2009 at 9:53 pm. Reason: add code tags
I would recommend making a class to represent those items that appear in the list. The toString() method of that class should return the description you wish to see in the list. On selection, you can access that object through getSelectedValue() and read the image path from that object.
HashMap would be another possibility if the only data needed for each item is simply the title and image.
HashMap would be another possibility if the only data needed for each item is simply the title and image.
If I'm right in what are Ezzaral intentions then with use of specifically build class for above task, store it in ArrayList, you could use for each to take on job of iterating through and checking which value is selected. Depending on that manipulate rest of the action flow.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
•
•
I only used the if-else construct to compare the elements selected from the list with the contents of the file.
java Syntax (Toggle Plain Text)
import java.awt.BorderLayout; import java.util.ArrayList; import java.util.List; import javax.swing.DefaultListModel; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class ListExample extends JFrame { JList itemListing; JLabel pathLabel; public ListExample() { getContentPane().setLayout(new BorderLayout()); setDefaultCloseOperation(EXIT_ON_CLOSE); // Build your ListModel from the items list. // You could very easily make this custom // ListModel class to handle all of the data retrieval. DefaultListModel model = new DefaultListModel(); List<ListItem> fileItems = getItemsFromFile(); for (ListItem item : fileItems){ model.addElement(item); } itemListing = new JList(model); itemListing.addListSelectionListener(new SelectionListener()); getContentPane().add(itemListing, BorderLayout.NORTH); pathLabel = new JLabel(); getContentPane().add(pathLabel, BorderLayout.SOUTH); setBounds(100,100,300,200); } /** Just an example, you would retrieve these items from * file, database, whatever. */ private List<ListItem> getItemsFromFile() { List<ListItem> items = new ArrayList<ListItem>(); items.add(new ListItem("One thing","c:\\someDir\\oneThing.jpg")); items.add(new ListItem("Another thing","c:\\someDir\\anotherThing.jpg")); items.add(new ListItem("Big shiny thing","c:\\someDir\\shiny.jpg")); return items; } /** This is your data class. It should hold all of the data that you need * to display or work with for each item. */ class ListItem { String description; String path; public ListItem(String description, String path){ this.description = description; this.path = path; } public String getPath(){ return path; } public String toString(){ return description; } } /** Small listener for your JList */ class SelectionListener implements ListSelectionListener{ public void valueChanged(ListSelectionEvent e) { if (itemListing.getSelectedIndex()>-1){ ListItem selectedItem = (ListItem)itemListing.getSelectedValue(); pathLabel.setText( selectedItem.getPath() ); } } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ListExample().setVisible(true); } }); } }
![]() |
Similar Threads
- Software Engineer/Programmer/Architect (Software Development Job Offers)
- Java/J2EE Senior Software Engineer (Software Development Job Offers)
- Software Engineer (Software Development Job Offers)
- Senior Embedded Software Engineer Sunnyvale, CA (Software Development Job Offers)
- Senior Linux Software Engineer Sunnyvale CA (Software Development Job Offers)
- Front-end Java Software Engineer for Digital Video/Media Market Leader (Software Development Job Offers)
- Senior Software Engineer (Java) (Web Development Job Offers)
- Software Engineer (Software Development Job Offers)
- Java Software Engineer (Software Development Job Offers)
Other Threads in the Java Forum
- Previous Thread: Mysterious ways of java applet execution from remote desktop.
- Next Thread: I am very stumped. :(
Views: 505 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth chat class classes client code component csv database draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie number object objects oracle oriented panel print printf problem program programming project projects recursion replaydirector reporting researchinmotion return robot rotatetext scanner score screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows






