2015-04-05 35 views
1

我有這樣的代碼,工程完全按照所需JTable的作品,但JTable的子類不會

package com.grantbroadwater.signInAssistant.view; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextField; 

import org.junit.Test; 

import com.grantbroadwater.school.Student; 

public class SignInSheetTableTest implements ActionListener{ 

static SignInSheetTableModel model; 
static JTextField tfFirst, tfLast; 
static JTable table; 

public SignInSheetTableTest() { 
    // TODO Auto-generated constructor stub 
} 

private static JPanel createContentPanel(){ 
    JPanel panel = new JPanel(new GridLayout(1, 0)); 

    table = new JTable(new SignInSheetTableModel()); 
    model = (SignInSheetTableModel)table.getModel(); 
    table.setPreferredScrollableViewportSize(new Dimension(500, 80)); 
    table.setFillsViewportHeight(true); 

    JScrollPane scrollPane = new JScrollPane(table); 

    panel.add(scrollPane); 

    return panel; 
} 

private static void createAndShowGUI(){ 
    JFrame frame = new JFrame("Table Test"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel panel = SignInSheetTableTest.createContentPanel(); 
    panel.setOpaque(true); 

    JPanel entryPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
    tfFirst = new JTextField(8); 
    tfLast = new JTextField(8); 
    JButton btnAdd = new JButton("Add"); 
    btnAdd.addActionListener(new SignInSheetTableTest()); 
    JButton btnDelete = new JButton("Delete"); 
    btnDelete.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      int rowIndex = table.getSelectedRow(); 
      model.deleteRow(rowIndex); 
     } 
    }); 

    entryPanel.add(tfFirst); 
    entryPanel.add(tfLast); 
    entryPanel.add(btnAdd); 
    entryPanel.add(btnDelete); 

    JPanel housingPanel = new JPanel(new BorderLayout()); 
    housingPanel.add(entryPanel, BorderLayout.NORTH); 
    housingPanel.add(panel, BorderLayout.CENTER); 

    frame.setContentPane(housingPanel); 

    frame.pack(); 
    frame.setVisible(true); 
} 

public static void main(String[] args) { 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    String first = tfFirst.getText(); 
    String last = tfLast.getText(); 

    Student s = new Student(first, last, "11111"); 

    model.addStudent(s); 

    table.scrollRectToVisible(table.getCellRect(table.getRowCount() - 1, 0, true)); 
} 

@Test 
public void test(){ 
    main(null); 

    try { 
     Thread.sleep(15000); 
    } catch (InterruptedException e) {} 

} 

} 

但是,當我改變JTable的實例爲SignInSheetTable子類的JTable,並改變構造函數new SignInSheetTable()程序仍會執行,但不會出現JTable。我無法找到任何解決方案,而我完全失去了爲什麼一個子類不會工作,當一個超類會整個SignInSheetTable類如下所示。

package com.grantbroadwater.signInAssistant.view; 

import javax.swing.JTable; 

import com.grantbroadwater.school.Student; 
import com.grantbroadwater.util.Log; 
import com.grantbroadwater.util.Log.LogType; 

public class SignInSheetTable extends JTable{ 

private static final long serialVersionUID = 1L; 
private SignInSheetTableModel model; 

public SignInSheetTable() { 
    super(new SignInSheetTableModel()); 
    model = (SignInSheetTableModel) super.getModel(); 

    this.setFillsViewportHeight(true); 
    new Log(LogType.DEBUG, "new Sign In sheet"); 
} 

public SignInSheetTableModel getModel() { 
    return model; 
} 

public void addStudent(Student student) { 
    model.addStudent(student); 
} 

public void deleteRow(int rowIndex) { 
    model.deleteRow(rowIndex); 
} 

} 
+0

有沒有真正的點覆蓋getModel – MadProgrammer 2015-04-05 03:03:14

+0

真的,我想我沒想到關於這一點,你認爲這就是我的JTable沒有顯示的原因嗎? – Grant 2015-04-05 03:04:56

+1

在十多年的Swing編程中,我需要將*** JTable精確地延長到*** 0 ***次。 – 2015-04-05 03:24:22

回答

3

覆蓋getModel似乎是造成該計劃其餘部分的問題。無論如何,這並不是真正需要的。

就我個人而言,我不打算使用自定義表格,也沒有爲類別添加任何新功能,這些功能無法通過模型直接管理,無論如何,這都是管理層應該執行的地方

我還擔心過度使用static,因爲這很可能給你造成一些問題,因爲程序變得更大

+0

謝謝你,似乎是這個問題。我真的只是添加相當數量的委託方法來提高抽象級別,所以使用表的類只需要使用實例化表而不是表和模型,但我可以不用。感謝您的幫助 – Grant 2015-04-05 03:31:51

+0

我只是專注於圍繞模型進行管理,而這正是我所做的一切 – MadProgrammer 2015-04-05 03:32:47