0

我正在使用一个向量来存储 JList 中显示的信息,并且我想使用所选索引的索引将该行的数据复制到 JTextField 和 JTextArea 中。我遇到了一些问题,其中所选项目未显示在 JTextField/Area 中,而是显示先前所选索引的数据。每个第二个索引都有显示错误数据的问题,2、4、6、8、10 然后是 11。如果您在程序刚刚执行时选择第一个,它们中的每一个都只会显示正确的数据。我添加到 ListSelectionEvent 侦听器的打印行显示了所选索引的正确数据,但它没有正确地将其复制到 JTextArea/JTextField 中。

在程序执行后立即选择。 在程序执行后立即选择。

我选择了其他一些索引,然后尝试再次选择它 我选择了其他一些索引,然后尝试再次选择它

代码:

public class EditFlashcardGui implements ListSelectionListener {

private JFrame frame;
private JPanel listPanel;
private Vector<Vector> flashcardMasterVector = new Vector<Vector>();
private JList list;
private JPanel tablePanel, dataPanel, textFieldPanel;
private JButton submitButton, cancelButton;
private JTextField frontTextField;
private JTextArea reverseTextArea;
private GridBagLayout gridBagLayout;
private GridBagConstraints constraints;
private JLabel frontTextLabel, reverseTextLabel;
Vector<Vector> masterVector = new Vector<Vector>();

public EditFlashcardGui() {

    frame = new JFrame("Edit / Delete Flashcards");
    frame.setSize(500, 200);

    Container con = frame.getContentPane();
    con.setLayout(new BorderLayout());

    listPanel = new JPanel(new BorderLayout());

    Vector<String> columnNames = new Vector<String>();

    columnNames.add("id");
    columnNames.add("front text");
    columnNames.add("reverse text");

    list = new JList(populateList());
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.addListSelectionListener(this);

    Font font1 = new Font("SansSerif", Font.BOLD, 20);
    list.setFont(font1);

    tablePanel = new JPanel();
    tablePanel.add(list);

    JScrollPane scrollPane = new JScrollPane(list);

    con.add(scrollPane, BorderLayout.NORTH);

    // south panel

    gridBagLayout = new GridBagLayout();
    constraints = new GridBagConstraints();
    textFieldPanel = new JPanel(gridBagLayout);

    // cancelButton = new JButton("Cancel");
    // dataPanel.add(cancelButton, BorderLayout.SOUTH);

    frontTextLabel = new JLabel("Front Text");
    constraints.ipadx = 1;
    constraints.ipady = 1;
    constraints.gridx = 0;
    constraints.gridy = 0;
    gridBagLayout.setConstraints(frontTextLabel, constraints);
    textFieldPanel.add(frontTextLabel);

    frontTextField = new JTextField();
    frontTextField.setColumns(30);
    constraints.ipadx = 1;
    constraints.ipady = 1;
    constraints.gridx = 0;
    constraints.gridy = 1;
    gridBagLayout.setConstraints(frontTextField, constraints);
    textFieldPanel.add(frontTextField);

    reverseTextLabel = new JLabel("Reverse Text");
    constraints.ipadx = 1;
    constraints.ipady = 1;
    constraints.gridx = 0;
    constraints.gridy = 2;
    gridBagLayout.setConstraints(reverseTextLabel, constraints);
    textFieldPanel.add(reverseTextLabel);

    reverseTextArea = new JTextArea(3, 30);
    constraints.ipadx = 1;
    constraints.ipady = 1;
    constraints.gridx = 0;
    constraints.gridy = 3;
    gridBagLayout.setConstraints(reverseTextArea, constraints);
    textFieldPanel.add(reverseTextArea);

    submitButton = new JButton("Submit");
    constraints.ipadx = 1;
    constraints.ipady = 1;
    constraints.gridx = 0;
    constraints.gridy = 4;
    gridBagLayout.setConstraints(submitButton, constraints);
    textFieldPanel.add(submitButton);

    con.add(textFieldPanel, BorderLayout.CENTER);

    frame.setResizable(false);
    frame.pack();
    frame.setVisible(true);

}

public Vector<Vector> populateList() {
    //flashcardMasterVector = flashcardDB.getList();
    //Vector<String> flashcardVector = new Vector<String>();

    Integer temp = 0;

    for(int i = 0; i <= 20; i++) {
        Vector<String> flashcardVector = new Vector<String>();
        flashcardVector.add(temp.toString());
        flashcardVector.add(temp.toString());
        flashcardVector.add(temp.toString());
        masterVector.add(i, flashcardVector);
        temp++;
    }



    return masterVector;
}

public static void main(String[] args) {
    EditFlashcardGui gui = new EditFlashcardGui();
}

@Override
public void valueChanged(ListSelectionEvent e) {
    if (! e.getValueIsAdjusting())
    {
        System.out.print("id : " );
        System.out.println(masterVector.get(list.getSelectedIndex()).get(0));
        System.out.print("front text : ");
        System.out.println(masterVector.get(list.getSelectedIndex()).get(1));
        frontTextField.setText(masterVector.get(e.getFirstIndex())
                .get(1).toString());
        System.out.print("reverse text : ");
        System.out.println(masterVector.get(list.getSelectedIndex()).get(2));
        reverseTextArea.setText(masterVector.get(e.getFirstIndex())
                .get(2).toString());
    }
}
}
4

2 回答 2

2
  • 必须阅读有关如何使用 JList的 Oracles 教程

  • 不能Vector<Vector>直接JList

  • put value and to use XxxListModelonly,但在这种情况下必须创建疯狂的解决方法AbstractListModel,使用简单数组代替

  • JList是基于一维数组必须使用Vector<Object (or String or Double...)>


  • 不要把简单的事情复杂化,把这个数组放到JTable(到XxxTableModel ),删除所有同步列,JTableHeader如果需要,删除,然后输出到 GUI 看起来像JList

  • JTable.removeColumn仅从视图中删除列,所有数据仍会显示并存储到其中XxxTableModel,无需任何更改

  • 然后选择(必须测试-1 == none of row is selected)行(必须convertViewToModel在排序或过滤的情况下JTable)返回所有需要的数据XxxTableModel

于 2013-02-20T13:34:48.163 回答
1

使用 list.getSelectedIndex() 而不是 e.getFirstIndex() 方法。

代码 :

        frontTextField.setText( masterVector.get(list.getSelectedIndex() ).get(1).toString() );
        reverseTextArea.setText(masterVector.get( list.getSelectedIndex()).get(2).toString() );

清洁代码:

public void valueChanged(ListSelectionEvent e) {

    if (!e.getValueIsAdjusting()) {
        Vector masterVec = masterVector.get( list.getSelectedIndex() );

        System.out.print("id : ");
        System.out.println(masterVec.get(0));

        System.out.print("front text : ");
        System.out.println(masterVec.get(1));

        System.out.print("reverse text : ");
        System.out.println(masterVec.get(2));

        frontTextField.setText(masterVec.get(1).toString());
        reverseTextArea.setText(masterVec.get(2).toString());;
    }
}
于 2013-02-20T14:24:07.727 回答