Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-03-2008, 03:58 PM
Member
 
Join Date: Jan 2008
Posts: 7
Cymro is on a distinguished road
ArrayList problem with images
I have this code:
Code:
public static ArrayList<ImageIcon> imRay = new ArrayList<ImageIcon>(); ImageIcon one = new ImageIcon("0.png"); ImageIcon two = new ImageIcon("1.png"); public Test() { initComponents(); this.setLocationRelativeTo(null); imRay.add(one); imRay.add(two); }
Then, later on, under a button:

Code:
int i = 0; i = jComboBox1.getSelectedIndex(); jLabel3.setIcon(imRay.get(i));
However, whilst it doesn't throw an error (unless I select a value in the jComboBox that is higher than currently allowed), it doesn't change the icon in jlabel3.

Where am I going wrong?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-05-2008, 09:24 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
This works okay.
Code:
import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import java.util.*; import java.util.List; import javax.imageio.ImageIO; import javax.swing.*; public class ImageSwap implements ActionListener { JLabel label; public List<ImageIcon> imRay = new ArrayList<ImageIcon>(); public void actionPerformed(ActionEvent e) { int index = ((JComboBox)e.getSource()).getSelectedIndex(); label.setIcon(imRay.get(index)); } private JLabel getLabel(BufferedImage[] images) { for(int j = 0; j < images.length; j++) imRay.add(new ImageIcon(images[j])); label = new JLabel(imRay.get(0)); label.setHorizontalAlignment(JLabel.CENTER); return label; } private JPanel getComboPanel() { JComboBox combo = new JComboBox(); for(int j = 0; j < imRay.size(); j++) combo.addItem(Integer.valueOf(j)); combo.addActionListener(this); JPanel panel = new JPanel(); panel.add(combo); return panel; } public static void main(String[] args) throws IOException { String[] ids = { "--g--", "---h-" }; BufferedImage[] images = new BufferedImage[ids.length]; for(int j = 0; j < images.length; j++) { String path = "images/geek/geek" + ids[j] + ".gif"; images[j] = ImageIO.read(new File(path)); } ImageSwap test = new ImageSwap(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getLabel(images)); f.add(test.getComboPanel(), "North"); f.setSize(400,400); f.setLocationRelativeTo(null); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-05-2008, 08:22 PM
Member
 
Join Date: Jan 2008
Posts: 7
Cymro is on a distinguished road
Thanks, but I don't know which parts I would need for my already existing class. Is there any chance that you could add some comments to the vital parts, so I can have some idea of what to do with it?

EDIT: I have found a rather unelegant possible solution which involves invisible labels containing the images necessary, then, instead of loading the images on command, it simply gets the label to use the other label's icon. It's quite slow, but I'll have to try and incorporate some elements of your code into it to speed it up a bit.

Last edited by Cymro : 02-05-2008 at 10:45 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Project Trouble: Searching one ArrayList with another ArrayList BC2210 New To Java 2 04-21-2008 01:43 PM
ArrayList problem khamuruddeen New To Java 7 12-22-2007 07:46 AM
ArrayList problem (finding largest no) bugger New To Java 3 12-12-2007 02:47 PM
Help with images... toby Java Applets 1 08-04-2007 07:25 AM
Images in JSP Daniel JavaServer Pages (JSP) and JSTL 1 06-05-2007 08:01 AM


All times are GMT +3. The time now is 04:45 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org