Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2006

Tip: Looking for answers? Try searching our database.

Array info

Thread view: 
john.plomer@gmail.com - 09 Feb 2006 18:13 GMT
I have a gui, in which i want to make my comboboxes more dynamic.

I have created a Cpu class that has a number of params, including a
String name.

I want the combobox to store an array of these names.

I have an array of type Cpu, so how do i go about extracting the names
from the Cpu objects to put them into the combobox? (i have written a
getName method in the Cpu class).

currently this code produces an array of memory addresses:

cpuList[0] = cpu1;
  cpuList[1] = cpu2;
  cpuList[2] = cpu3;
  String []cpuComboList = new String [cpuList.length];

  for (int i = 0; i < cpuList.length; i++){
     cpuComboList[i] = cpuList[i].getName(cpuList[i]);
  }
cpuList is initialised as are the cpu objects.

John
Monique Y. Mudama - 09 Feb 2006 19:15 GMT
> I have a gui, in which i want to make my comboboxes more dynamic.
>
[quoted text clipped - 18 lines]
>    }
> cpuList is initialised as are the cpu objects.

Your problem seems to be in the implementation of getName(), so you
should post it.

Why does getName() take an argument?

You haven't included all of your code, but it sure looks like the
method should simply look like this:

public String getName()
{
    return myName;
}

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

IchBin - 09 Feb 2006 20:40 GMT
> I have a gui, in which i want to make my comboboxes more dynamic.
>
[quoted text clipped - 20 lines]
>
> John

    yourComboBox.removeAllItems();
    for (String cpu : cpuList)  {
        yourComboBox.addItem(cpu);
    }

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
Nigel Wade - 10 Feb 2006 10:05 GMT
> I have a gui, in which i want to make my comboboxes more dynamic.
>
[quoted text clipped - 20 lines]
>
> John

You haven't included the relevant bit of code, which is how you populate the
JComboBox.

A JComboBox uses a ListCellRenderer to display its contents. By default
ListCellRenderer uses the objects' toString() method to render it. You Cpu
class has inherited the toString() method from Object, which returns a string
representation of the objects' reference (and this looks like a memory
address). I suspect you have added your Cpu objects to the JComboBox rather
than the Strings from your cpuComboList array, hence you see their references.

To get the JComboBox to display the Cpu strings all you need to do is override
the toString() method in your Cpu class. Then when the JComboBox renders a
component it will display the string returned by your Cpu.toString() method.
There's no need for the cpuComboList array at all.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Bigval - 11 Feb 2006 12:54 GMT
This is how the combobox is populated by default, after using a GUI
builder:

String []dataMoboCombo = { "Mobo1", "Mobo2", "Mobo3", "Mobo4", "Mobo5",
"Mobo6" };
  cmbMoboCombo = new JComboBox( dataMoboCombo );

with the combobox i am working on i do this:

String []cpuComboBox = new String [cpuList.length];

   for (int i = 0; i < cpuList.length; i++){
   cpuComboBox[i] = cpuList[i].getName();
   }

The objects of type cpu have a string component (name), and a series of
booleans.   All i want to extract is the name Strings.   I dont know
why it wont work.   My getName method in cpu is this:

public String getName(){
return name;
}
I have written a toString method:

public String toString(){
return getName():
}

It still wont work, as it stands, the combobox is being populated with
nothing :S as opposed to the mem addresses.

Is it possible that as cpu is a superclass of Intel and Amd, and these
2 subclasses have more fields (more booleans) it is causing trouble?
a cpu object is declared thus:
Cpu cpu1 = new Amd( "cpu1", false, true, true, false, false, false,
3000);
Cpu cpu2 = new Intel( "cpu2", true, false, true, false, false, 4000);

John
IchBin - 11 Feb 2006 22:14 GMT
> This is how the combobox is populated by default, after using a GUI
> builder:
[quoted text clipped - 35 lines]
>
> John

For me, just post the code that you are have a problem with. That is,
the methods and all required references.  I still do not know what type
cpuList and cpu1, cpu2 and cpu3 are yet. I can work from your code to
resolve problem unless someone else has the answer.
Signature


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

Bigval - 12 Feb 2006 11:16 GMT
Of course, will post some code later:

cpuList is an array of type Cpu
cpu1-3 are Cpu objects

Cpu(String name, boolean intel, boolean amd, String clockSpeed) this is
a superclass of Intel and Amd (which contain similar params, plus a
load of booleans representing the relevent socket types for each
manufacturer).

John
Roedy Green - 12 Feb 2006 11:52 GMT
>Cpu(String name, boolean intel, boolean amd, String clockSpeed) this is
>a superclass of Intel and Amd (which contain similar params, plus a
>load of booleans representing the relevent socket types for each
>manufacturer).

In ancient China, it was considered extremely improper for a doctor to
examine or touch a patient. They had little dolls where the female
patient could point to the source of the trouble.

Come on. Don't be coy. Post the code.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Bigval - 12 Feb 2006 12:25 GMT
overall superclass
public class Hardware {

    public String name;

    public Hardware(String name)
    {
    }
cpu class
public class Cpu extends Hardware
{

    public String clockSpeed;

    public boolean amd;

    public boolean intel;

    public String name;

    public Cpu(String name, boolean intel, boolean amd, String
clockSpeed){
        super(name);

    }

                    public String getSpeed(){
        return clockSpeed;

    }

    public String getName(){
    return name;
    }

    public String toString(){
  return getName();
    }
}
amd class, subclass of cpu

public class Amd extends Cpu
{

    public boolean socket754;

    public boolean socket939;

    public boolean socket940;

    public boolean socketA;

    public String clockSpeed;

    public Amd(String name, boolean intel, boolean amd, boolean
isSocket754, boolean isSocket939, boolean isSocket940,
                boolean isSocketA, String clockSpeed){
       
        super(name, intel, amd, clockSpeed);
}
Ian Mills - 12 Feb 2006 12:33 GMT
> overall superclass
> public class Hardware {
[quoted text clipped - 56 lines]
>         super(name, intel, amd, clockSpeed);
> }

It looks as though name is never getting set. In the constructor of
Hardware you need to do this.name = name. Having a String called name in
the constructor will not automatically set the class variable name.
Ian Mills - 12 Feb 2006 12:38 GMT
> overall superclass
> public class Hardware {
[quoted text clipped - 56 lines]
>         super(name, intel, amd, clockSpeed);
> }

One other thing I would note is that you should not really make your
class variables public as this allows encapsulation to be broken.
Ideally you should make them private with the relevant getters and setters.
Roedy Green - 12 Feb 2006 12:40 GMT
>public class Hardware {

ok. just some general comments first:

public class Amd extends Cpu
  {

  public boolean socket754;

  public boolean socket939;

  public boolean socket940;

  public boolean socketA;

  public String clockSpeed;

  public Amd(String name, boolean intel, boolean amd, boolean
             isSocket754, boolean isSocket939, boolean isSocket940,
             boolean isSocketA, String clockSpeed)
     {

     super(name, intel, amd, clockSpeed);
     }
}

I hope you are aware that your public fields are not used for
anything. you don't save the params to the constructor in them as is
traditional.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 12 Feb 2006 12:43 GMT
>public class Hardware {

public class Cpu extends Hardware
  {

  public String clockSpeed;

  public boolean amd;

  public boolean intel;

  public String name;

  public Cpu(String name, boolean intel, boolean amd, String
             clockSpeed)
     {
     super(name);

     }

  public String getSpeed()
     {
     return clockSpeed;

     }

  public String getName()
     {
     return name;
     }

  public String toString()
     {
     return getName();
     }
  }

In this code, you discard 3 of your 4 constructor parameters. I am
begriming to wonder if you imagine java automatically copies
constructor parameters to the matching instance variables.

You code offers no way to set the public instance variables, unless
you count aCpu.name = "Sempron";

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.