Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm developing an application that has methods of this kind:

attackIfIsFar();
protectIfIsNear();
helpAfterDeadOf();
helpBeforeAttackOf();

etc.

The initialization of my application for n players is something like

player1.attackIfIsFar(player2);
player2.protectIfIsNear(player4);
player3.helpAfterDeadOf(player1);
player4.helpBeforeAttackOf(player3);

etc.

I don't know how to configure a jtable that that can allow me to set the equivalent of this code-block

In others words I need simply a way to create a jtable with 3 column and n row, were I can set in the column 1 and 3, the player, and in the central column one of the available methods that each player on the column 1 must invoke on each player of column 3

share|improve this question
    
Can you add more info? Does player1 do all of those things to all other players? The same with the other players? Is it any combination of playerX - action - playerY? Can Player1 to X and Y to Player 2? –  Paul Jun 19 '12 at 19:46
    
what do the players is not important... I need simply a way to create a jtable with 3 column and n row were I can set in the column 1 and 3, the player, and in the central column the methods that each player on the column 1 must invoke on each player of column 3 –  Asgard Jun 19 '12 at 19:54
1  
Hmm... Having only seen what you've posted: this code is going to become a huge mess very fast, if it isn't already. If you haven't, do yourself a huge favour and take a look at the state pattern and goal-based agent behaviour(s). –  Steve Evers Jun 20 '12 at 0:35
    
I know the state pattern... why I should use the state pattern in this case? –  Asgard Jun 20 '12 at 15:46

1 Answer 1

up vote 2 down vote accepted

First you need an class to represent the expression.

public class Expression {
  private Person lhs;
  private Person rhs;
  private Action action;
}

Then you need to make a TableModel that is backed by a list of Expressions. Column count will be 3 and row count will be the size of the list. The rest of the methods are fairly straight forward to implement by mapping the row and/or column to the respective Expression and/or field.

Then you need a JTable that sets the TableCellEditor for each column. Each of which would be a combo box with the available options for the respective field. If you need more help with tables, read this tutorial.

Once all the values are configured, you will need to create some logic that will evaluate the expressions and calls the correct methods.

Edit
Here is a very basic cell editor example. You could take steps to not populate the combo boxes with a value that will allow the user to select an invalid value.

@Override
public TableCellEditor getCellEditor(int row, int col)
{
  switch (col) {
  case 0:
  case 2:
    return new DefaultCellEditor(new JComboBox(LIST_OF_PEOPLE));
  case 1:
    return new DefaultCellEditor(new JComboBox(LIST_OF_ACTIONS));
  default:
    return super.getCellEditor(row, col);
  }
}
share|improve this answer
    
could you give me more details about setting of TableCellEditor? thanks –  Asgard Jun 19 '12 at 23:31
    
I haven't understand, how to integrate the lists in cells of table. –  Asgard Jun 20 '12 at 15:43
    
@Asgard: I had a small issue, DefaultCellEditor takes a JComboBox not a list. As for the values in the cells, you need to make a list of Expression. Then row 0 maps to the item at index 0 in the list. –  unholysampler Jun 20 '12 at 16:11
    
Could you give me more code? Where should I put the @Override public TableCellEditor getCellEditor(int row, int col){? The class with my table extends JFrame. Thanks –  Asgard Jun 20 '12 at 17:02
    
@Asgard: You should make a new class that extends JTable that implements getCellEditor(), then use that class in your JFrame. –  unholysampler Jun 20 '12 at 17:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.