When the user selects the level of difficulty of the questions to see, I want to display a `JTable1 issues by level of difficulty. There is no error, but the questions are not displayed in the 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");
}
}
});
There are so many aspects to your question which require more information, but basically, once you've loaded your List
of questions, you simply need to wrap a TableModel
around them and then apply them to the JTable
you have
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;
}
}
Then in your ActionListener
, wrap the questions in the model and apply it to the 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);
}
});
You really need to take a look at How to Use Tables which will cover more information into how this works and what you need to do to fill out the remaining information
well i think the problem that you get no display is because you are printing out an object: System.out.println(comboBox.getSelectedItem()) instead of the String: System.out.println(comboBox.getSelectedItem().toString)
and i think you should use a String variable to get the output directly from the comboBox.
if (comboBox.getSelectedIndex() != -1) {
String chaine = ""
+ comboBox.getItemAt
(comboBox.getSelectedIndex());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.