943,810 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1850
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 11th, 2009
0

JLabel image isn't updating when JComboBox selection changes.

Expand Post »
So I have a JComboBox, which contains recipe names. I want, when the recipe name is changed, for the label that is an icon of the rating (1 to 5) stars, based on my recipe object, to change as well.

My code seems like it should work perfect but it doesn't. Any help please?

java Syntax (Toggle Plain Text)
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.util.ArrayList;
  4.  
  5. import javax.swing.BoxLayout;
  6. import javax.swing.DefaultComboBoxModel;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JComboBox;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. import javax.swing.JTabbedPane;
  13. import javax.swing.WindowConstants;
  14.  
  15. // GUI Class
  16. // Will contain all the code that will display my GUI
  17. // Will use the recipe class.
  18.  
  19. public class gui {
  20.  
  21. recipe test = new recipe();
  22.  
  23. DefaultComboBoxModel recipeNames = new DefaultComboBoxModel();
  24. ArrayList<recipe> recipes = new ArrayList<recipe>();
  25. ArrayList<String> recipeNamesArray = new ArrayList<String>();
  26.  
  27. ImageIcon stars0 = createImageIcon("amazonstar0C.gif");
  28. ImageIcon stars1 = createImageIcon("amazonstar1C.gif");
  29. ImageIcon stars2 = createImageIcon("amazonstar2C.gif");
  30. ImageIcon stars3 = createImageIcon("amazonstar3C.gif");
  31. ImageIcon stars4 = createImageIcon("amazonstar4C.gif");
  32. ImageIcon stars5 = createImageIcon("amazonstar5C.gif");
  33.  
  34. JLabel ratingImage = new JLabel();
  35. JComboBox recipeList = new JComboBox(recipeNames);
  36.  
  37. // Constructor will initialize everything.
  38. public gui() {
  39.  
  40. test.setName("test");
  41. test.setRating(3);
  42. test.addIngredient("1/4 cup milk");
  43. test.addDirection("Get milk.");
  44. test.setNote("testNote");
  45. test.setPeople(4);
  46. test.setTime("00:01");
  47.  
  48. recipes.add(test);
  49. recipeNamesArray.add(test.getName());
  50. recipeNames.addElement(test.getName());
  51.  
  52. // The main JFrame that will hold my whole GUI.
  53. JFrame main = new JFrame("E-Chef");
  54.  
  55. // The main tabbed pane that will run my program.
  56. // Everything is done from this.
  57. JTabbedPane menu = new JTabbedPane();
  58.  
  59. // All 3 panels for each tab.
  60. JPanel mainMenu = new JPanel();
  61. mainMenu.setLayout(new BoxLayout(mainMenu, BoxLayout.X_AXIS));
  62.  
  63. JPanel add = new JPanel();
  64. JPanel search = new JPanel();
  65.  
  66. // All 3 tabbed icons.
  67. ImageIcon icon1 = createImageIcon("menucooking.png");
  68. ImageIcon icon2 = createImageIcon("addcooking.png");
  69. ImageIcon icon3 = createImageIcon("searchcooking.png");
  70.  
  71. // Each tab.
  72. menu.addTab("Main", icon1, mainMenu, "The Main Menu");
  73. menu.addTab("Add", icon2, add, "Add a Recipe");
  74. menu.addTab("Search", icon3, search, "Search for Recipe");
  75.  
  76. // Panel 1 -- mainMenu
  77. // Creating two more panels.
  78. // One to store recipe info, the other to store 3 navigation buttons.
  79. JPanel recipeInfo = new JPanel();
  80. recipeInfo.setLayout(new BoxLayout(recipeInfo, BoxLayout.Y_AXIS));
  81. JPanel navigation = new JPanel();
  82. navigation.setLayout(new BoxLayout(navigation, BoxLayout.X_AXIS));
  83. // Add things to each panel.
  84. JLabel title = new JLabel("Recipe List");
  85. recipeList.setEditable(true);
  86. MyActionListener recipeChange = new MyActionListener();
  87. recipeList.addActionListener(recipeChange);
  88. JLabel rating = new JLabel("Rating");
  89. // Add things to recipeInfo
  90. recipeInfo.add(title);
  91. recipeInfo.add(recipeList);
  92. recipeInfo.add(rating);
  93. recipeInfo.add(ratingImage);
  94. // Add panels to mainMenu panel.
  95. mainMenu.add(recipeInfo);
  96. mainMenu.add(navigation);
  97.  
  98. // Add tabbed pane to the frame.
  99. main.add(menu);
  100.  
  101. // Setting the frame visible and displaying it.
  102. main.pack();
  103. main.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  104. main.setVisible(true);
  105. }
  106.  
  107. protected static ImageIcon createImageIcon(String path) {
  108. java.net.URL imgURL = gui.class.getResource(path);
  109. if (imgURL != null) {
  110. return new ImageIcon(imgURL);
  111. } else {
  112. System.err.println("Couldn't find file: " + path);
  113. return null;
  114. }
  115. }
  116.  
  117. class MyActionListener implements ActionListener {
  118. Object oldItem;
  119.  
  120. public void actionPerformed(ActionEvent evt) {
  121. JComboBox cb = (JComboBox) evt.getSource();
  122. String newItem = (String)cb.getSelectedItem();
  123.  
  124. int position = recipeNamesArray.indexOf(newItem);
  125. int rating = recipes.get(position).getRating();
  126.  
  127. switch (rating) {
  128. case 0: ratingImage.setIcon(stars0); break;
  129. case 1: ratingImage.setIcon(stars1); break;
  130. case 2: ratingImage.setIcon(stars2); break;
  131. case 3: ratingImage.setIcon(stars3); break;
  132. case 4: ratingImage.setIcon(stars4); break;
  133. case 5: ratingImage.setIcon(stars5); break;
  134. default: ratingImage.setIcon(stars0);break;
  135. }
  136.  
  137.  
  138. }
  139. }
  140.  
  141. }

Here is the code of my recipe class just in case you need to look at it. All my main class does is it creates a new instance of my gui class, that's it.

java Syntax (Toggle Plain Text)
  1. import java.util.ArrayList;
  2.  
  3. // Recipe Class
  4. // Will contain code for recipe objects.
  5. // These are the main objects that my system will implement.
  6.  
  7. public class recipe {
  8.  
  9. // Each recipe has a name, which is a String.
  10. private String name = "";
  11.  
  12. // Each recipe contains a list of ingredients.
  13. // Will use an ArrayList because of their functionality.
  14. private ArrayList<String> ingredients = new ArrayList<String>();
  15.  
  16. // Another ArrayList will be used to contain each direction for making that recipe.
  17. private ArrayList<String> directions = new ArrayList<String>();
  18.  
  19. // A String of notes for the recipe.
  20. // Things you should know that might not directly affect its outcome.
  21. private String notes = "";
  22.  
  23. // Three Strings for the filename of each picture for the recipe.
  24. // A beginning picture of the ingredients, middle of the process, and finished recipe.
  25. // This will be optional.
  26. private String picFile1 = "";
  27. private String picFile2 = "";
  28. private String picFile3 = "";
  29.  
  30. // Difficulty rating as an integer.
  31. private int difficulty = 0;
  32.  
  33. // Time it takes to prepare the entire dish from beginning to end.
  34. // Usually an estimate.
  35. // Using a String for this because it should be in HH:MM format.
  36. private String time = "";
  37.  
  38. // How many people does it serve on average?
  39. private int peopleServed = 0;
  40.  
  41. // Recipe rating.
  42. private int rating = 0;
  43.  
  44. // Class Functions //
  45.  
  46. // The constructor will do nothing in this case.
  47. // All the attributes default values are set already.
  48. // Everything else will be set and retrieved through getters and setters.
  49. public recipe() {
  50.  
  51. }
  52.  
  53. // Getters and Setters //
  54.  
  55. public String getName() {
  56. return name;
  57. }
  58.  
  59. public void setName(String recipeName) {
  60. name = recipeName;
  61. }
  62.  
  63. public void addIngredient(String newIngredient) {
  64. ingredients.add(newIngredient);
  65. }
  66.  
  67. public void deleteIngredient(String removeIngredient) {
  68. int position = ingredients.indexOf(removeIngredient);
  69. ingredients.remove(position);
  70. }
  71.  
  72. public int numIngredients() {
  73. return ingredients.size();
  74. }
  75.  
  76. public String getIngredient(int number) {
  77. int index = number - 1;
  78. return ingredients.get(index);
  79. }
  80.  
  81. public boolean hasIngredient(String thisIngredient) {
  82. return ingredients.contains(thisIngredient);
  83. }
  84.  
  85. public void addDirection(String newDirection) {
  86. directions.add(newDirection);
  87. }
  88.  
  89. public void deleteDirection(String removeDirection) {
  90. int position = directions.indexOf(removeDirection);
  91. directions.remove(position);
  92. }
  93.  
  94. public int numDirections() {
  95. return directions.size();
  96. }
  97.  
  98. public String getDirection(int number) {
  99. int index = number - 1;
  100. return directions.get(index);
  101. }
  102.  
  103. public void setNote(String note) {
  104. notes = note;
  105. }
  106.  
  107. public String getNote() {
  108. return notes;
  109. }
  110.  
  111. public void setBeginningPic(String filename) {
  112. picFile1 = filename;
  113. }
  114.  
  115. public void setMiddlePic(String filename) {
  116. picFile2 = filename;
  117. }
  118.  
  119. public void setEndPic(String filename) {
  120. picFile3 = filename;
  121. }
  122.  
  123. public String getBeginningPic() {
  124. return picFile1;
  125. }
  126.  
  127. public String getMiddlePic() {
  128. return picFile2;
  129. }
  130.  
  131. public String getEndPic() {
  132. return picFile3;
  133. }
  134.  
  135. public void setDifficulty(int number) {
  136. difficulty = number;
  137. }
  138.  
  139. public int getDifficulty() {
  140. return difficulty;
  141. }
  142.  
  143. public void setTime(String length) {
  144. time = length;
  145. }
  146.  
  147. public String getTimeDefault() {
  148. return time;
  149. }
  150.  
  151. public int getTimeMinutes() {
  152. String hours = time.substring(0, 2);
  153. int intHours = Integer.valueOf( hours ).intValue();
  154. String minutes = time.substring(3, 5);
  155. int intMinutes = Integer.valueOf( minutes ).intValue();
  156. return ((intHours * 60) + intMinutes);
  157. }
  158.  
  159. public void setPeople(int amount) {
  160. peopleServed = amount;
  161. }
  162.  
  163. public int getPeople() {
  164. return peopleServed;
  165. }
  166.  
  167. public void setRating(int number) {
  168. rating = number;
  169. }
  170.  
  171. public int getRating() {
  172. return rating;
  173. }
  174. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
Aug 12th, 2009
0

Re: JLabel image isn't updating when JComboBox selection changes.

? You only have one recipe entered. It won't change because there's nothing to change it to! Code in another recipe, with a different rating, and try then.
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Aug 12th, 2009
0

Re: JLabel image isn't updating when JComboBox selection changes.

Click to Expand / Collapse  Quote originally posted by llemes4011 ...
? You only have one recipe entered. It won't change because there's nothing to change it to! Code in another recipe, with a different rating, and try then.
But that one recipe that is in there has a rating of 3/5 stars. The initial rating picture the label uses is the 0/5 stars one. Shouldn't it load with the 3/5 stars picture from that code? Even if I go into the drop down list again, and click the same recipe, it should load the 3/5 stars picture shouldn't it?
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
Aug 12th, 2009
0

Re: JLabel image isn't updating when JComboBox selection changes.

Click to Expand / Collapse  Quote originally posted by gotm ...
But that one recipe that is in there has a rating of 3/5 stars. The initial rating picture the label uses is the 0/5 stars one. Shouldn't it load with the 3/5 stars picture from that code? Even if I go into the drop down list again, and click the same recipe, it should load the 3/5 stars picture shouldn't it?

It DOES change to three stars when you pull down the combo box and change it (I downloaded some PNG files. I assume GIF would work too). Your code is sort of hard to follow due to rating being a local variable of type int and also a JLabel or something somewhere else. And you also have a JLabel called ratingImage, where you do a switch on rating in actionPerformed. So some more descriptive variable names would be helpful. It's pretty confusing looking at a switch statement based on rating when I thought rating was a JLabel. That said, I don't think you are INITIALIZING any JLabels to contain stars, so they don't until you do the drop-down. I did have to RESIZE the frame in order to see the stars, but my PNG's were pretty big.

Also, I had to add a main () function.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Aug 12th, 2009
0

Re: JLabel image isn't updating when JComboBox selection changes.

It DOES change to three stars when you pull down the combo box and change it (I downloaded some PNG files. I assume GIF would work too). Your code is sort of hard to follow due to rating being a local variable of type int and also a JLabel or something somewhere else. And you also have a JLabel called ratingImage, where you do a switch on rating in actionPerformed. So some more descriptive variable names would be helpful. It's pretty confusing looking at a switch statement based on rating when I thought rating was a JLabel. That said, I don't think you are INITIALIZING any JLabels to contain stars, so they don't until you do the drop-down. I did have to RESIZE the frame in order to see the stars, but my PNG's were pretty big.

Also, I had to add a main () function.
yours changes? lol mine doesn't i dont know what you are doing differently,
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
Aug 12th, 2009
0

Re: JLabel image isn't updating when JComboBox selection changes.

you might want to try getting rid of the call to pack(), and use the setSize(int, int) method, there might not be enough room to see them
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Aug 12th, 2009
0

Re: JLabel image isn't updating when JComboBox selection changes.

My changes were these. Changed this line:

Java Syntax (Toggle Plain Text)
  1. ImageIcon stars3 = createImageIcon("amazonstar3C.gif");

to this:

Java Syntax (Toggle Plain Text)
  1. ImageIcon stars3 = createImageIcon("amazonstar3C.png");

I did this because mine was a PNG image, not a GIF image. I would imagine you woulnd't need to if yours is a GIF.

I added this function in the gui class:

Java Syntax (Toggle Plain Text)
  1. public static void main (String args[])
  2. {
  3. new gui ();
  4. }

I ran the code. I then resized the JFrame with a mouse drag to make it much bigger. I then pulled down the combo box, selected the one and only option, and clicked it. After doing so, the three stars came up.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Aug 12th, 2009
0

Re: JLabel image isn't updating when JComboBox selection changes.

ok I had a seperate main class that just ran my gui(). would that have anything to do with it?
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
Aug 12th, 2009
0

Re: JLabel image isn't updating when JComboBox selection changes.

Click to Expand / Collapse  Quote originally posted by gotm ...
ok I had a seperate main class that just ran my gui(). would that have anything to do with it?
Depends on what's in the main function. If it's the same as the main I have, I wouldn't imagine it would make a difference.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008
Aug 12th, 2009
0

Re: JLabel image isn't updating when JComboBox selection changes.

I assume you have no messages like this:

Quote ...
Couldn't find file: amazonstar3C.gif
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,374 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: What is the easiest and most efficient vision library...
Next Thread in Java Forum Timeline: Java swing gui





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC