Results 1 to 3 of 3
  1. #1
    711groove is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

    Default Why doesn't this work? (multi-type array)

    PHP Code:
    	for (int i = 0 ; i < edits.length - 5 ; i++){
    		if (edits[i] instanceof JTextField){
    			edits[i].setText(db.dbImport(fields[i]));}
    		else{edits[i].setSelectedIndex(db.dbInt(fields[i])-mods[i]);}}
    edits[] is an Object[] containing JTextFields and JComboBoxes
    fields[] is a String[]
    mods[] is an in[]

    I get two compile errors:
    cannot find symbol method setText(java.lang.String) on line three
    cannot find symbol method setSelectedIndex(int) on line four

    By all accounts, this SHOULD work. If the entry in edits[] is a JTextfield, it tries to set the text, if it's a JComboBox, it tries to set the index. What's wrong here? My only guess is that Object doesn't know how to interpret methods specific to it's subclasses, and if that's true, then how can I fix it?

    Thanks in advance!

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    try casting the edits to the desired Component like

    Java Code:
    for (int i = 0 ; i < edits.length - 5 ; i++){
            if (edits[i] instanceof JTextField){
                ((JTextField)edits[i]).setText(db.dbImport(fields[i]));}
            else{
                ((JComboBox)edits[i]).setSelectedIndex(db.dbInt(fields[i])-mods[i]);
            }
    }

  3. #3
    711groove is offline Member
    Join Date
    Dec 2009
    Posts
    15
    Rep Power
    0

Similar Threads

  1. [SOLVED] Multi-dimensional Array further help
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 05-07-2009, 04:50 PM
  2. Refractoring a Multi-Dinmension array
    By thorne_ in forum New To Java
    Replies: 4
    Last Post: 04-15-2009, 06:30 PM
  3. Multi-dimensional array
    By VinTiger in forum New To Java
    Replies: 22
    Last Post: 03-01-2009, 06:51 AM
  4. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 03:34 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •