开发者

Click on JTable Model Updates JTextfield

I have a JTable with a custom Abstract Table Model and I would like to select a row in my table and that information to appear in the textboxes to the left. I would like this to happen automatically with out the use use of any buttons. The only approach I can think of involves a button. My table model looks like this:

public class AdminTableModel extends AbstractTableModel {
private ArrayList<Contestant> contestants;
private String[] columns={"First Name", "Last Name", "Entry"};

public AdminTableModel(ArrayList<Contestant> contestants) {
    this.contestants = contestants;
}

public AdminTableModel(List l) {
    contestants.addAll(l);
}

public int getRowCount() {
    return contestants.size();
}

public int getColumnCount() {
    return columns.length;
}

public String getColumnName(int col) {
    return columns[col];
}

public Object getValueAt(int row, int col) {
    Contestant contestant = contestants.get(row);
    switch(col){
        case 0:
            return contestant.getFirst_name();
        case 1:
            return contestant.getLast_name();
        case 2:
            return contestant.getEntry();
        default:
            return null;
    }
}

}

With the code for my table looking like this:

public class Contest开发者_如何学编程antsTable extends JTable{

public ContestantsTable(ArrayList<Contestant> contestants) {

    AdminTableModel atModel = new AdminTableModel(contestants);
    this.setModel(atModel);
    atModel.fireTableDataChanged();
}

}

Here is the panel with the text fields that I want to refresh:

public class ContestantAddPanel extends JPanel {
private JLabel jlFirst_name;
private JLabel jlLast_name;
private JLabel jlEntry;

private JTextField jtfFirst_name;
private JTextField jtfLast_name;
private JTextField jtfEntry;

public ContestantAddPanel() {
    jlFirst_name = new JLabel("First Name: ");
    jlLast_name = new JLabel("Last Name: ");
    jlEntry = new JLabel("Entry: ");

    jtfFirst_name = new JTextField();
    jtfLast_name = new JTextField();
    jtfEntry = new JTextField();

    this.setLayout(new GridLayout(3,2));

    this.add(jlFirst_name);
    this.add(jtfFirst_name);
    this.add(jlLast_name);
    this.add(jtfLast_name);
    this.add(jlEntry);
    this.add(jtfEntry);
}

public String getFirstName(){
    return jtfFirst_name.getText();
}

public String getLastName(){
    return jtfLast_name.getText();
}

public String getEntry(){
    return jtfEntry.getText();
}   
}

I am not looking for you to write me a solution, I am only looking for guidance as I cannot seem to find what I want on google. Such as a link to a tutorial, the oracle one isn't very informative on this matter as I don't want to update the row in the table but in the textfields.


JTable has a ListSelectionModel, which you can access by getSelectionModel(). You can add a listener to it and get notified of row selection events. Listeners would need to implement the javax.swing.event.ListSelectionListener interface and would get notified via valueChanged(..). Your code will be something something similar to this:

public ContestantAddPanel() implements ListSelectionListener {
...

   public valueChanged(ListSelectionEvent e) {
      ...
   }
...
}

And

myTable.getSelectionModel().addListSelectionListener(<some instance of your panel>);

This should be enough to get you started, but if you need more info please post a comment. I don't have any links to a good tutorial, sorry.

Edit:

How to Write a List Selection Listener

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜