Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,358 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,672 people online right now. Registration is fast and FREE... Join Now!




Combobox help

 
Reply to this topicStart new topic

Combobox help, loading images from a combobox

THE_RAM
3 Sep, 2008 - 02:01 AM
Post #1

New D.I.C Head
*

Joined: 29 Mar, 2007
Posts: 19


My Contributions
I am busy with a java program section where i want to use a combobox to display images on a label. The images are located in a package called "Druid", which contains a class called "Druids". When I run the program I get a pathe error and just can’t seem to solve it. Here is what I have done:

CODE

package Driud;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JLabel;



public class Druids extends JLabel
{

    private int x;
    private int y;
    
    private int ab[] = new int[5];
    String       Druidrray[]={"Clarion.gif","Pan.gif","Rhino.gif","Wonde.gif","Pluto.gif"};
    public ImageIcon DruidPictures[]={
     new ImageIcon(this.getClass().getResource(Druidrray[0])),
     new ImageIcon(this.getClass().getResource(Druidrray[1])),
     new ImageIcon(this.getClass().getResource(Druidrray[2])),
     new ImageIcon(this.getClass().getResource(Druidrray[3])),
     new ImageIcon(this.getClass().getResource(Druidrray[4]))};
        

    int f=0;

    public Druids()
    {
        super("Druids");
        
      
        
    }
    

    @Override
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    @Override
    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        
        for(int i =0;i<=Druidrray.length;i++)
        DruidPictures[f].paintIcon(this, g,getX(),getY());
      f++;
    }
}


Edited to add the [ code] tag
Please post your code like this code.gif

This post has been edited by pbl: 3 Sep, 2008 - 03:00 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Combobox Help
3 Sep, 2008 - 03:13 AM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
You're class Druid extends JLabel that means 1 JLabel not an array of 5 JLabel
You probably want something like this (there is a lot of method you do not need to overload)
and as they go into a Combo you don't need X and Y, neither do you have to repaaint

CODE

package Driud;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JLabel;



public class Druids extends JLabel
{

        ImageIcon icon;

        Druids(String filename) {
          super("")
          icon = new ImageIcon(filename);
        
}


Now the one who calls this:

CODE

String Druidrray[]={"Clarion.gif","Pan.gif","Rhino.gif","Wonde.gif","Pluto.gif"};
for(int i = 0; i < druids.length; i++) {
   combo.addItem(new Druids(Druidrray[i]);
    
}


As it goes in a Combo it is JComboBox that I would extends:

CODE

class DruidCombo extends JCombo {
    
     DruidCombo() {
         super();
         for(int i = 0; i < Druidrray.length; i++)
             addItem(new JLabel(new ImageIcon(Druidrray[i])));
     }
}


This post has been edited by pbl: 3 Sep, 2008 - 03:20 AM
User is offlineProfile CardPM
+Quote Post

THE_RAM
RE: Combobox Help
11 Sep, 2008 - 09:45 AM
Post #3

New D.I.C Head
*

Joined: 29 Mar, 2007
Posts: 19


My Contributions
QUOTE(pbl @ 3 Sep, 2008 - 04:13 AM) *

You're class Druid extends JLabel that means 1 JLabel not an array of 5 JLabel
You probably want something like this (there is a lot of method you do not need to overload)
and as they go into a Combo you don't need X and Y, neither do you have to repaaint

CODE

package Driud;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JLabel;



public class Druids extends JLabel
{

        ImageIcon icon;

        Druids(String filename) {
          super("")
          icon = new ImageIcon(filename);
        
}


Now the one who calls this:

CODE

String Druidrray[]={"Clarion.gif","Pan.gif","Rhino.gif","Wonde.gif","Pluto.gif"};
for(int i = 0; i < druids.length; i++) {
   combo.addItem(new Druids(Druidrray[i]);
    
}


As it goes in a Combo it is JComboBox that I would extends:

CODE

class DruidCombo extends JCombo {
    
     DruidCombo() {
         super();
         for(int i = 0; i < Druidrray.length; i++)
             addItem(new JLabel(new ImageIcon(Druidrray[i])));
     }
}




QUOTE(pbl @ 3 Sep, 2008 - 04:13 AM) *

You're class Druid extends JLabel that means 1 JLabel not an array of 5 JLabel
You probably want something like this (there is a lot of method you do not need to overload)
and as they go into a Combo you don't need X and Y, neither do you have to repaaint

CODE

package Driud;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JLabel;



public class Druids extends JLabel
{

        ImageIcon icon;

        Druids(String filename) {
          super("")
          icon = new ImageIcon(filename);
        
}


Now the one who calls this:

CODE

String Druidrray[]={"Clarion.gif","Pan.gif","Rhino.gif","Wonde.gif","Pluto.gif"};
for(int i = 0; i < druids.length; i++) {
   combo.addItem(new Druids(Druidrray[i]);
    
}


As it goes in a Combo it is JComboBox that I would extends:

CODE

class DruidCombo extends JCombo {
    
     DruidCombo() {
         super();
         for(int i = 0; i < Druidrray.length; i++)
             addItem(new JLabel(new ImageIcon(Druidrray[i])));
     }
}



User is offlineProfile CardPM
+Quote Post

pbl
RE: Combobox Help
11 Sep, 2008 - 03:41 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
Your reply only quotes my reply
What is your problem ?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 09:19AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month