从JComboBox显示JTable

时间:2016-04-23 20:59:10

标签: java swing jtable jcombobox

当用户选择要查看的问题的难易程度时,我想按难易程度显示`JTable1问题。 没有错误,但问题没有显示在表格中。

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");
    }       
  }
});

2 个答案:

答案 0 :(得分:2)

您的问题有很多方面需要更多信息,但基本上,一旦您加载了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中,将问题包装在模型中并将其应用到表格中。

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,其中将详细介绍其工作原理以及填写剩余信息所需执行的操作

答案 1 :(得分:0)

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

我认为您应该使用String变量直接获取输出 来自comboBox。

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