我的 JTable 不显示我的列名,我不明白为什么。谁能帮我告诉我为什么?提前谢谢你。
package projet.ihm;
import java.awt.Color;
public class VueOccupations extends JPanel {
class MonModeleTableOccupation extends AbstractTableModel {
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
private JoursHoraire jours;
// initialise ma liste de joueurs
public MonModeleTableOccupation( JoursHoraire jour) {
super();
this.jours=jour;
///périodes = périodes;
//jours = JoursHoraire.valueOf(jour);
}
// @Override
// public void setValueAt(Object aValue, int l, int c) {
// JoursHoraire[] jours=JoursHoraire.values();
// Joueur j = joueurs.get(l);
// switch (c) {
//
// case 1:
// j.setPrenom((String) aValue);
// break;
//case 2:
// j.setDate((Date) aValue);
//}
//}
@Override
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
@Override
public String getColumnName(int c) {
// TODO Auto-generated method stub
switch (c) {
case 0:
return "périodes";
case 1:
return "Jour";
case 2:
return "Heure Debut";
case 3:
return "Heure Fin";
case 4:
return "Local";
}
return "Vide";
}
@Override
public int getColumnCount() {
return 5;
}
@Override
public int getRowCount() {
return Periode.NB_PERIODES;
}
@Override
public Object getValueAt(int l, int c) {
JoursHoraire jour = jours.JEUDI;
// System.out.println(l+" "+c+" "+j);
switch (c) {
case 0:
return jour.values();
case 1:
return jour.values();
case 2:
// return sdf.format(j.getDate());
return jour.values();
}
return "Vide";
}
}
private ControleurJHOccupation ctrl;
private JTable table;
private JTable table_1;
private JComboBox cbModule;
private JComboBox cbPeriode;
private JComboBox<JoursHoraire> cbJour;
private JComboBox<HeureMinute> cbHeureD;
private JComboBox<HeureMinute> cbHeureFin;
private ModèleJHOccupation modèleJHOccupation;
public VueOccupations(ControleurJHOccupation ctrl)throws HeadlessException {
modèleJHOccupation = new ModèleJHOccupation(ctrl);
// super(new ModèleJHProfesseur(ctrl),
// ctrl.getFacade().getIdsProfesseurs());
this.ctrl=ctrl;
JLabel lblModule = new JLabel("Choix Module");
cbModule = new JComboBox();
table = new JTable(modèleJHOccupation);
cbPeriode = new JComboBox();
cbJour = new JComboBox();
cbHeureD = new JComboBox();
cbHeureFin = new JComboBox();
String[] listeModule = ctrl.getFacade().getIdsModules();
for (int p = 0; p < listeModule.length; p++)
cbModule.addItem(listeModule[p]);
HeureMinute[] listeHDeb=SlotsJour.getHeuresJournée();
for (int p = 0; p < listeHDeb.length ; p++)
cbHeureD.addItem(listeHDeb[p]);
HeureMinute[] listeHFin=SlotsJour.getHeuresJournée();
for (int p = 0; p < listeHFin.length ; p++)
cbHeureFin.addItem(listeHFin[p]);
JoursHoraire[] jour=JoursHoraire.values();
for (int p = 0; p < jour.length ; p++)
cbJour.addItem(jour[p]);
List<Periode> période =ctrl.getFacade().getListePériode();
for (int i = 0; i < période.size(); i++){
cbPeriode.addItem( période.get(i).getId());
}
initGui();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 500);
setVisible(true);
JButton btnAjoutOcc = new JButton("Ajout Occupation");
JScrollPane scrollPane = new JScrollPane();
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(18)
.addComponent(lblModule)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(cbModule, 0, 466, Short.MAX_VALUE))
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap(44, Short.MAX_VALUE)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(table_1, GroupLayout.PREFERRED_SIZE, 514, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(cbPeriode, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(cbJour, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(cbHeureD, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(cbHeureFin, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(btnAjoutOcc, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE)))))
.addGap(42))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(lblModule)
.addComponent(cbModule, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(table_1, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(42)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
.addGap(47)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(cbPeriode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(cbJour, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(cbHeureD, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(cbHeureFin, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(btnAjoutOcc))
.addContainerGap(227, Short.MAX_VALUE))
);
setLayout(groupLayout);
}
private void initGui() {
//monModeleTableOccupation = new MonModeleTableOccupation(JoursHoraire jour);
table_1 = new JTable(monModeleTableOccupation);
//table.getColumnModel().getColumn(2).setCellRenderer(new Rend);
table_1.setAutoCreateRowSorter(true);
table_1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table_1.setBackground(Color.cyan);
table_1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged ( ListSelectionEvent evt ) {
if ( evt . getValueIsAdjusting ()) return ;
int i =table . getSelectedRow ();
if (i >= 0) {
i =table.convertRowIndexToModel (i);
System.out.println(i);
} else
{}}}
);
JScrollPane scp = new JScrollPane(table);
//getRootPane().getContentPane().add(scp);
add(scp, BorderLayout.CENTER);
//JPanel panelSud = new JPanel();
//getContentPane().add(panelSud, BorderLayout.NORTH);
}
private MonModeleTableOccupation monModeleTableOccupation;
private void setDefaultCloseOperation(int exitOnClose) {
// TODO Auto-generated method stub
}
}
显示列的一种简单方法是将JTable
放入JScrollPane
的视口中。
JScrollPane tablePane = new JScrollPane(jtable);
并将此JScrollPane
添加到JPanel
中。
正如 JTable 的 javadoc 所说:
如果此 JTable 是封闭 JScrollPane 的视口视图(通常的情况),则配置此滚动窗格的方式包括将表的 tableHeader 安装为滚动窗格的列标题视图等。 ....
我看不出你的table_1
被JScrollPane
包裹着.我想这会导致问题。