当用户选择要查看的问题的难度级别时,我想按难度级别显示“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");
}
}
});
最佳答案
您的问题有很多方面需要更多信息,但基本上,一旦您加载了问题的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其中将涵盖有关其工作原理以及您需要做什么来填写其余信息的更多信息
关于java - 从 JComboBox 显示 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36816335/