Software Engineer

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 4
Reputation: KingsKidy is an unknown quantity at this point 
Solved Threads: 0
KingsKidy KingsKidy is offline Offline
Newbie Poster

Software Engineer

 
0
  #1
Apr 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,260
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 493
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Software Engineer

 
0
  #2
Apr 20th, 2009
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...
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 4
Reputation: KingsKidy is an unknown quantity at this point 
Solved Threads: 0
KingsKidy KingsKidy is offline Offline
Newbie Poster

Re: Software Engineer

 
0
  #3
Apr 21st, 2009
Thank you. The following is the cose snippet of what I intended to do:
  1. [//process the Classic button event
  2. cbtn1.addActionListener(new ActionListener(){
  3. public void actionPerformed(ActionEvent evt)
  4. {
  5. //String[] Str1 = {"John","James","Moses","Nick","Paul"};
  6. //trying something new here
  7. String[] cdtit = displayClassicTitles();
  8. titlelisting = new JList(cdtit);
  9.  
  10. titlelisting.setVisibleRowCount(3);
  11.  
  12. //does not allow multiple selections
  13. titlelisting.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  14.  
  15. scrollPane = new JScrollPane(titlelisting);
  16. // frame.add(scrollPane, BorderLayout.CENTER);
  17. ListAndLabelPanel.add(scrollPane,BorderLayout.SOUTH);
  18. // create an image icon
  19.  
  20. ImageIcon cdImage = new ImageIcon("C:/CDGraphics/GospelGifs/EasyStars.Gif");
  21.  
  22. //add the image icon to a label and set label text
  23. lblC = new JLabel(cdImage);
  24. lblC.setText("Pic of selected title");
  25. ListAndLabelPanel.add(lblC,BorderLayout.CENTER);
  26.  
  27. panelC.add(ListAndLabelPanel,BorderLayout.CENTER);
  28.  
  29. //now add the center panel to the frame
  30. frame.add(panelC,BorderLayout.CENTER);
  31.  
  32. frame.setVisible(true);
  33.  
  34. //Handle the JList Event
  35. titlelisting.addListSelectionListener(new ListSelectionListener(){
  36. public void valueChanged(ListSelectionEvent event)
  37. {
  38. if(!event.getValueIsAdjusting())
  39. {
  40. if(titlelisting.getSelectedValue()=="OxbridgeBaby")
  41. {
  42. ImageIcon cdImage1 = new ImageIcon("C:/CDGraphics/ClassicGifs/OxbridgeBaby.Gif");
  43. lblC.setIcon(cdImage1);
  44. ShoppingCart selectedItem = new ShoppingCart();
  45. selectedItem.setSelectedTitle1(titlelisting.getSelectedValue().toString());
  46. selectedItem.setTitlePrice1("$10.00");
  47. selectedItem.setVisible(true);
  48. }else if(titlelisting.getSelectedValue()=="BrainyBaby")
  49. {
  50. ImageIcon cdImage2 = new ImageIcon("C:/CDGraphics/ClassicGifs/BrainyBaby.Gif");
  51. lblC = new JLabel(cdImage2);
  52. ShoppingCart selectedItem = new ShoppingCart();
  53. selectedItem.setSelectedTitle2(titlelisting.getSelectedValue().toString());
  54. selectedItem.setVisible(true);
  55. }else if(titlelisting.getSelectedValue()=="ADozenRoses")
  56. {
  57. ImageIcon cdImage3 = new ImageIcon("C:/CDGraphics/ClassicGifs/ADozenRoses.Gif");
  58. lblC = new JLabel(cdImage3);
  59. ShoppingCart selectedItem = new ShoppingCart();
  60. selectedItem.setSelectedTitle3(titlelisting.getSelectedValue().toString());
  61. }
  62. else{ }
  63.  
  64. }
  65. }
  66. });
  67.  
  68. }
  69. });
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.
Last edited by Ancient Dragon; Apr 21st, 2009 at 9:53 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,647
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Software Engineer

 
0
  #4
Apr 21st, 2009
You cannot compare Strings using '==' you must use the String class's equals method.

if(titlelisting.getSelectedValue()=="OxbridgeBaby")
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Software Engineer

 
0
  #5
Apr 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 4
Reputation: KingsKidy is an unknown quantity at this point 
Solved Threads: 0
KingsKidy KingsKidy is offline Offline
Newbie Poster

Re: Software Engineer

 
0
  #6
Apr 22nd, 2009
Thank you all for helping out.
The String class's equals method did the job. I replaced the '==' with equals() and got the result I wanted.
I am grateful that you are there to help.
---
KingsKidy.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Software Engineer

 
0
  #7
Apr 22nd, 2009
You're going to hard-code all of that data into an if-else structure in a production app? If there is any more than what you posted, you're making a real mess for yourself to maintain.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 4
Reputation: KingsKidy is an unknown quantity at this point 
Solved Threads: 0
KingsKidy KingsKidy is offline Offline
Newbie Poster

Re: Software Engineer

 
0
  #8
Apr 23rd, 2009
Thank you Ezzaral for your last post. However the list elements are not hard-coded into the app. They were actually read in from a file -a separate method handled that. I only used the if-else construct to compare the elements selected from the list with the contents of the file.

KingsKidy.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,260
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 493
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: Software Engineer

 
-1
  #9
Apr 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Software Engineer

 
2
  #10
Apr 23rd, 2009
Originally Posted by KingsKidy View Post
I only used the if-else construct to compare the elements selected from the list with the contents of the file.
Then you're still hard-coding what to do with each item as it's selected and that negates the reason for storing them in a file at all. Here is a very small example of the design I mentioned above. You need an "Item" class to hold the data for each of those items and you can use that directly within the JList
  1. import java.awt.BorderLayout;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.swing.DefaultListModel;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JList;
  8. import javax.swing.event.ListSelectionEvent;
  9. import javax.swing.event.ListSelectionListener;
  10.  
  11. public class ListExample extends JFrame {
  12. JList itemListing;
  13. JLabel pathLabel;
  14.  
  15. public ListExample() {
  16. getContentPane().setLayout(new BorderLayout());
  17. setDefaultCloseOperation(EXIT_ON_CLOSE);
  18.  
  19. // Build your ListModel from the items list.
  20. // You could very easily make this custom
  21. // ListModel class to handle all of the data retrieval.
  22. DefaultListModel model = new DefaultListModel();
  23. List<ListItem> fileItems = getItemsFromFile();
  24. for (ListItem item : fileItems){
  25. model.addElement(item);
  26. }
  27. itemListing = new JList(model);
  28. itemListing.addListSelectionListener(new SelectionListener());
  29. getContentPane().add(itemListing, BorderLayout.NORTH);
  30.  
  31. pathLabel = new JLabel();
  32. getContentPane().add(pathLabel, BorderLayout.SOUTH);
  33.  
  34. setBounds(100,100,300,200);
  35. }
  36.  
  37. /** Just an example, you would retrieve these items from
  38.   * file, database, whatever.
  39.   */
  40. private List<ListItem> getItemsFromFile() {
  41. List<ListItem> items = new ArrayList<ListItem>();
  42. items.add(new ListItem("One thing","c:\\someDir\\oneThing.jpg"));
  43. items.add(new ListItem("Another thing","c:\\someDir\\anotherThing.jpg"));
  44. items.add(new ListItem("Big shiny thing","c:\\someDir\\shiny.jpg"));
  45. return items;
  46. }
  47.  
  48. /** This is your data class. It should hold all of the data that you need
  49.   * to display or work with for each item.
  50.   */
  51. class ListItem {
  52. String description;
  53. String path;
  54.  
  55. public ListItem(String description, String path){
  56. this.description = description;
  57. this.path = path;
  58. }
  59.  
  60. public String getPath(){
  61. return path;
  62. }
  63. public String toString(){
  64. return description;
  65. }
  66. }
  67.  
  68. /** Small listener for your JList */
  69. class SelectionListener implements ListSelectionListener{
  70. public void valueChanged(ListSelectionEvent e) {
  71. if (itemListing.getSelectedIndex()>-1){
  72. ListItem selectedItem = (ListItem)itemListing.getSelectedValue();
  73. pathLabel.setText( selectedItem.getPath() );
  74. }
  75. }
  76. }
  77.  
  78. public static void main(String args[]) {
  79. java.awt.EventQueue.invokeLater(new Runnable() {
  80. public void run() {
  81. new ListExample().setVisible(true);
  82. }
  83. });
  84. }
  85. }
If you already have a class that represents all of those items and cannot use it directly in the list, then you could change the ListItem class to be a tiny wrapper for your real item class that provides the methods needed to work with it in the JList.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 505 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC