1

I create a JTable with a customized model and characteristics. I have a long written class that carries out the set up and rendering properly. I see that the .getSelectedRows() method is not working and never evaluates to a value. I was trying to get the index of the first row selected and the last. Here is my code.

public void actionPerformed(ActionEvent event) {

    String command = event.getActionCommand();

    rows = table.getSelectedRows();

    firstRow = rows[0];

    int rowCount = rows.length;

    lastRow = rows[(rowCount - 1)];

    if (command.equals("Run Threw")) {

    }else if (command.equals("Shield Bash")) {
        this.attack(firstRow, lastRow, command);
    }

        public boolean block (int defendersRow) {
}

public boolean fumble (int attackersRow) {   
}

public boolean dodge (int defendersRow) {
}

public boolean critical (int attackersRow, int attackRoll) {
}


public void attack(int firstRow, int lastRow, String command) {
    command = this.command;
    firstRow = this.firstRow;
    lastRow = this.lastRow;

    if (command == "Bludgeon" || command == "React" || command == "ShieldBash") {
        attackersRow = this.lastRow;
        defendersRow = this.firstRow;
    }else if(command == "Attack" || command == "Skill") {
        attackersRow = this.firstRow;
        defendersRow = this.lastRow;
    }else {

    }
    table.setValueAt(true, attackersRow, 16);
    fumbled = this.fumble(attackersRow);
    if (fumbled == true) {
        outputString = "fumbled";
    }
    Object maxDamageObject = table.getValueAt(attackersRow, 10);
    int maxDamage = (Integer) maxDamageObject;
    attackRoll = generator.nextInt(100) + 1;
    this.critical(attackersRow, attackRoll);
    if (criticaled == true) {
        outputString = "criticaled";
    }
    dodged = this.dodge(defendersRow);
    if (dodged == true) {
        outputString = "dodged";
    }
    blocked = this.block(defendersRow);
    if (blocked == true) {
        outputString = "blocked";
    }
    int defenseRoll = generator.nextInt(100) + 1;
    Object attackBaseObject = table.getValueAt(attackersRow, 6);
    Object defenseBaseObject = table.getValueAt(defendersRow, 11);
    int attackBase = (Integer) attackBaseObject;
    int defenseBase = (Integer) defenseBaseObject;
    int attack = attackRoll + attackBase;
    int defense = defenseRoll + defenseBase;
    Object minDamageObject = table.getValueAt(attackersRow, 9);
    int minDamage = (Integer) minDamageObject;
    damage = generator.nextInt((maxDamage - minDamage))+minDamage;

    if (criticaled == true) {
        damage = maxDamage * 2;
    }else if (attack >= (defense + 50)) {
        damage = damage * 2;
    }else if (attack >= defense) {
        damage = damage;
    }else {
        damage = 0;
    }

    this.outputSelection(outputString, attackersRow, defendersRow, command, damage);
    this.damage(defendersRow, damage);
}

private void damage(int defendersRow, int damage) {
}

private void outputSelection(String outputString, int attackersRow, int defendersRow, String command, int damage) {


}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    Wow, that's a lot of context...`JTable#getSelectedRows` may return an empty array if nothing is selected, you should be checking `JTable#getSelectedRowCount` or `rows.length` before trying to access the array... – MadProgrammer Jan 16 '13 at 04:55
  • I agree with you but since I am doing the testing say I know I have selected rows and its still coming up as nothing so anything else? – Luke Greenleaf Jan 16 '13 at 05:02
  • edit and add executable code in your question. – vels4j Jan 16 '13 at 05:03
  • 1
    You have the wrong table reference...nothing is selected...dolphins are playing in the sink... – MadProgrammer Jan 16 '13 at 05:03
  • Can you make a http://sscce.org/ – Roger Jan 16 '13 at 05:06
  • Anyone see anything wrong with it now? – Luke Greenleaf Jan 16 '13 at 05:16
  • 1
    @LukeGreenleaf I see one mistake, I don't know its related to your question but use `String.equals()` method for String equality in your `attack` method. Moreover if you can access variables and method without `this` then better you use it without `this`, Also `fumbled` and `critical` are `boolean` then you don't need to check them with `==` – Smit Jan 16 '13 at 05:45
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jan 16 '13 at 17:32

2 Answers2

2

I doesn't see problem with code. May be you can cross check following points:

  1. Are you able to select single / multiple row(s)? Or check what jTableObject.getRowSelectionAllowed() returns
  2. Does your code gets called only after selecting rows. Your code should be executed after some rows are selected so that it can return you expected values.
  3. Listener which calls above mentioned code, is it added to table.

Small points but better to cross check :).

Yogesh Ralebhat
  • 1,376
  • 1
  • 13
  • 29
2

As shown in this working example, you can add a ListSelectionListener to your table's ListSelectionModel and see if anything comes through. It may help pin down where things have gone awry: for example, a local table reference that shadows the intended one.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045