从 JComboBox 显示 JTable

Display a JTable from a JComboBox

当用户选择要看的问题的难度时,我想按难度显示一个`JTable1 问题。 没有报错,只是题目没有显示在table.

comboBox = new JComboBox();
comboBox.setFont(new Font("Times New Roman", Font.PLAIN, 13));
comboBox.setModel(new DefaultComboBoxModel(new String[] {
  "Afficher les questions faciles", 
  "Afficher les questions moyens", 
  "Afficher les questions difficiles"}));   

comboBox.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    Object selected = comboBox.getSelectedItem();
    if(selected.toString().equals("Afficher les questions faciles")) {
      questions=GestionQuestionDelegate.doFindAllQuestionsByNiveauDeDifficulte("Facile");
      System.out.println(comboBox.getSelectedItem());
    } else if(selected.toString().equals("Afficher les questions moyens")) {
      System.out.println(comboBox.getSelectedItem());
      questions=GestionQuestionDelegate.doFindAllQuestionsByNiveauDeDifficulte("Moyen");
    } else if(selected.toString().equals("Afficher les questions difficiles")) {
      System.out.println(comboBox.getSelectedItem());
      questions=GestionQuestionDelegate.doFindAllQuestionsByNiveauDeDifficulte("Difficile");
    }       
  }
});

嗯,我认为你没有显示的问题是因为你 打印出一个对象: System.out.println(comboBox.getSelectedItem()) 而不是字符串: System.out.println(comboBox.getSelectedItem().toString)

而且我认为您应该使用字符串变量直接获取输出 来自组合框。

 if (comboBox.getSelectedIndex() != -1) {                     
           String chaine = "" 
              + comboBox.getItemAt
                (comboBox.getSelectedIndex());             
        }              

你的问题有很多方面需要更多信息,但基本上,一旦你加载了 List 个问题,你只需要将它们包裹起来 TableModel 然后将它们应用于您拥有的JTable

public class QuestionTableModel extends AbstractTableModel {
    private List<Question> questions;

    public QuestionTableModel(List<Question> questions) {
        this.questions = questions;
    }

    @Override
    public int getRowCount() {
        return questions == null ? 0 : questions.size();
    }

    @Override
    public int getColumnCount() {
        return // the number of columns you want to display
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return // The object class appropriate for the column/class property 
                // you want to display
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object value = null;
        if (questions != null) {
            Question question = questions.get(rowIndex);
            // Get the column value from the question
            // based on the columnIndex and the
            // question properties
        }
        return value;
    }


}

然后在您的 ActionListener 中,将问题包装在模型中并将其应用于 table。

comboBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Object selected = comboBox.getSelectedItem();
        if (selected.toString().equals("Afficher les questions faciles")) {
            questions = GestionQuestionDelegate.doFindAllQuestionsByNiveauDeDifficulte("Facile");
            System.out.println(comboBox.getSelectedItem());
        } else if (selected.toString().equals("Afficher les questions moyens")) {
            System.out.println(comboBox.getSelectedItem());
            questions = GestionQuestionDelegate.doFindAllQuestionsByNiveauDeDifficulte("Moyen");
        } else if (selected.toString().equals("Afficher les questions difficiles")) {
            System.out.println(comboBox.getSelectedItem());
            questions = GestionQuestionDelegate.doFindAllQuestionsByNiveauDeDifficulte("Difficile");
        }
        QuestionTableModel model = new QuestionTableModel(questions);
        table.setModel(model);
    }
});

您确实需要看一看 How to Use Tables,其中将包含有关其工作原理的更多信息以及您需要做什么来填写剩余信息