我的JTable有一個問題:它看起來像emty,沒有從Vector傳遞給它的對象中獲取任何數據。我檢查了相關的主題,但遺憾的是我沒有找到任何理由解決這個問題,幾乎相同的代碼實際上爲我工作過一次。 爲了進一步測試,我在按鈕上單擊創建對象,將它們添加到我的Vector中,然後嘗試刷新表格。 Printlns顯示實際上將對象添加到Vector中,但表格仍爲空。我認爲,問題必須在表格代碼中的某處。 我發佈我的測試班和相關的,希望你能幫助我。JTable沒有從Vector獲取數據
測試類:
public class TEST_Table extends JFrame {
private PPControl ppc;
private EntryFilePM epm;
private JTable entryTable;
private JPanel entryTablePanel;
private JButton addButton;
private JButton saveButton;
private JButton loadButton;
private JPanel buttonsPanel;
private JLabel kursl;
private JTextField kurst;
private JLabel cnumberl;
private JTextField cnumbert;
private JLabel namel;
private JTextField namet;
private JLabel datel;
private JTextField datet;
private JLabel addressl;
private JTextField addresst;
private JLabel pdatestl;
private JTextField pdatestt;
private JLabel pdateendl;
private JTextField pdateendt;
private JLabel roadl;
private JTextField roadt;
private JLabel studyfl;
private JTextField studyft;
private JLabel naprl;
private JTextField naprt;
private JLabel ratingl;
private JTextField ratingt;
private JLabel averagel;
private JTextField averaget;
private JLabel profl;
private JTextField proft;
private JLabel pbasel;
private JTextField pbaset;
private JLabel homel;
private JTextField homet;
private JPanel horriblePanel;
private JButton saveEntryButton;
private JButton cancelEntryButton;
private JPanel entryButtonsPanel;
private JPanel entryEditPanel;
int idCounter = 1;
public static void main(String[] args) throws EntryExistsException {
try {
new TEST_Table();
} catch (IOException e) {
e.printStackTrace();
}
}
public int getIdCounterNext(){
return idCounter++;
}
public TEST_Table() throws IOException, EntryExistsException {
super("TEST");
ppc = new PPControl();
initialize();
}
private void initialize() {
setSize(1200,800);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLayout(new BorderLayout());
createEntryTable();
createButtonsPanel();
createEntryTab();
add(entryTablePanel,BorderLayout.CENTER);
add(entryEditPanel, BorderLayout.WEST);
add(buttonsPanel, BorderLayout.SOUTH);
this.setVisible(true);
this.pack();
}
private void createEntryTable() {
final Vector<String> columns = new Vector<String>();
columns.add("Std");
columns.add("Contr #");
columns.add("Name");
columns.add("Birth dt");
columns.add("Address");
columns.add("Pr date start");
columns.add("Pr date end");
columns.add("Road");
columns.add("Std form");
columns.add("№ way");
columns.add("Rating");
columns.add("Avrg");
columns.add("Prof");
columns.add("Pr base");
columns.add("Home");
final EntryTableModel entryModel = new EntryTableModel(ppc.giveAllEntry(), columns);
entryTable = new JTable();
entryTable.setModel(entryModel);
entryTable.setShowGrid(true);
// entryTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// entryTable.getSelectionModel().addListSelectionListener(new TableSelectionListener());
JScrollPane entryScrollPane = new JScrollPane(entryTable);
entryTablePanel = new JPanel();
entryTablePanel.setLayout(new GridLayout());
entryTablePanel.add(entryScrollPane, BorderLayout.CENTER);
entryTablePanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
}
private void createButtonsPanel() {
addButton = new JButton("Add");
saveButton = new JButton("Save");
saveButton.addActionListener(new ButtonsListener());
loadButton = new JButton("Load");
loadButton.addActionListener(new ButtonsListener());
buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(1, 3));
buttonsPanel.add(addButton);
buttonsPanel.add(saveButton);
buttonsPanel.add(loadButton);
}
private void createEntryTab(){
JLabel kursl = new JLabel ("kurs");
kurst = new JTextField();
//otherlabels/tfields
JLabel ratingl = new JLabel("rating.double");
ratingt = new JTextField();
JLabel averagel = new JLabel("average.double");
averaget = new JTextField();
JLabel profl = new JLabel("profession");
proft = new JTextField();
JLabel pbasel = new JLabel("prak.base");
pbaset = new JTextField();
JLabel homel = new JLabel("home");
homet = new JTextField();
horriblePanel = new JPanel();
JScrollPane horribleScrollPane = new JScrollPane(horriblePanel);
horriblePanel.setLayout(new GridLayout(15,2));
// horriblePanel.add(horribleScrollPane, BorderLayout.CENTER);
horriblePanel.add(kursl);
horriblePanel.add(kurst);
//other labels/tfields
horriblePanel.add(homel);
horriblePanel.add(homet);
saveEntryButton = new JButton("Save");
saveEntryButton.addActionListener(new ButtonsListener());
cancelEntryButton = new JButton("Cancel");
entryButtonsPanel = new JPanel();
entryButtonsPanel.setLayout(new GridLayout());
entryButtonsPanel.add(saveEntryButton);
entryButtonsPanel.add(cancelEntryButton);
entryEditPanel = new JPanel();
entryEditPanel.setLayout(new GridLayout());
entryEditPanel.add(horriblePanel);
entryEditPanel.add(entryButtonsPanel);
}
class TableSelectionListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent e) {
int rowIndex = -1;
if (e.getSource().equals(entryTable.getSelectionModel())
&& entryTable.getSelectedRow() != -1) {
rowIndex = entryTable.convertRowIndexToModel(entryTable
.getSelectedRow());
EntryTableModel em = (EntryTableModel) entryTable.getModel();
Entry entry = em.giveEntryInRow(rowIndex);
}
}
}
class ButtonsListener implements ActionListener{
public void actionPerformed(final ActionEvent ae) {
if(ae.getSource().equals(addButton)){
}
if (ae.getSource().equals(saveButton)){
try {
epm.exportTable(entryTable);
} catch (IOException e) {
e.printStackTrace();
}
}
if(ae.getSource().equals(loadButton)){
try {
epm.importTable("C:/test.xls");
} catch (BiffException | IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ae.getSource().equals(saveEntryButton)) {
Calendar c = new GregorianCalendar();
int studyYear = 23;// Integer.parseInt(kurst.getText());
int contractNumber = getIdCounterNext();
contractNumber++;// Integer.parseInt(cnumbert.getText());
String stname = "Uano";// namet.getText();
Date birthDate = c.getTime();
// birthDate = epm.convertToDate(datet.getText());
String address = "abc asd fg 123";// addresst.getText();
Date practiceDateStart = c.getTime();
// practiceDateStart = epm.convertToDate(pdatestt.getText());
Date practiceDateEnd = c.getTime();
// practiceDateEnd = epm.convertToDate(pdateendt.getText());
String railway = "bad road";// roadt.getText();
String studyForm = "bad form";// studyft.getText();
int directionNumber = 1232; // =
// Integer.parseInt(naprt.getText());
double rating = 3.4;// Double.parseDouble(ratingt.getText());
double average = 4.5;// Double.parseDouble(averaget.getText());
String profession = "fcki";// proft.getText();
String practiceBase = "fckh";// pbaset.getText();
String inhabitation = "fckf2";// homet.getText();
try {
ppc.addEntry(studyYear, contractNumber, stname, birthDate,
address, practiceDateStart, practiceDateEnd, railway,
studyForm, directionNumber, rating, average,
profession, practiceBase, inhabitation);
for (Entry e : ppc.giveAllEntry()) {
System.out.println(e.getContractNumber());
System.out.println(e.getName());
System.out.println(e.getBirthDate());
}
final EntryTableModel em = (EntryTableModel) entryTable.getModel();
System.out.println("a");
em.setDataVector(ppc.giveAllEntry());
entryTable.updateUI();
System.out.println("b");
} catch (EntryExistsException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
爲TableModel類:
,讓我的對象的功能載體
public class EntryControl {
private Vector <Entry> entryList = new Vector <Entry>();
public void addEntry(Entry e) throws EntryExistsException{
if (entryList.contains(e))
throw new EntryExistsException(e, "");
else
entryList.add(e);
}
public Vector<Entry> listAllEntry(){
return entryList;
}
//reference in main control class
public Vector<Entry> giveAllEntry(){
return entryControl.listAllEntry();
}
輸出: http://i57.tinypic.com/125geac.jpg
一如往常,一個愚蠢的錯誤。謝謝! – reick