2016-04-23 20 views
0

Wenn der Benutzer den Schwierigkeitsgrad der Fragen auswählt, die angezeigt werden sollen, möchte ich ein JTable1-Problem nach Schwierigkeitsgrad anzeigen. Es gibt keinen Fehler, aber die Fragen werden nicht in der Tabelle angezeigt.Anzeige einer JTable aus einer JComboBox

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

so u jtables in Ihrem Combobox – Priyamal

+0

Nein ich will ausfüllen zu tun Möchten Sie die Frage mit dem ausgewählten Element der jcombobox – Daly

+0

anzeigen, können Sie das Thema dann ändern – Priyamal

Antwort

2

Es gibt so viele Aspekte auf Ihre Frage, die mehr Informationen benötigen, aber im Grunde, wenn Sie Ihre List Fragen geladen haben, müssen Sie einfach ein TableModel um sich herum wickeln und wenden sie dann auf die JTable Sie haben

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


} 

Dann in Ihrem ActionListener, wickeln sie die Fragen in dem Modell und es auf den Tisch.

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

Sie wirklich einen Blick auf How to Use Tables nehmen müssen, die weitere Informationen decken, wie das funktioniert und was Sie brauchen die restlichen Informationen

0

gut ich denke, das Problem, dass Sie keine Anzeige bekommen, weil Sie Ausdrucken ein Objekt sind: System.out.println (comboBox.getSelectedItem()) anstelle der String: System.out .println (comboBox.getSelectedItem(). toString)

und ich denke, Sie sollten eine String-Variable verwenden, um die Ausgabe direkt aus der ComboBox zu erhalten.

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

Sorry, aber es hat nicht funktioniert – Daly