简体   繁体   English

JTable工作,但JTable子类不会

[英]JTable works but JTable subclass wont

I have this code that works exactly as desired 我有这个代码完全按照需要工作

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) {}

}

}

However, when I change The JTable instance to a SignInSheetTable subclass of JTable, and change the constructor to new SignInSheetTable() the program will still execute, but no JTable will appear. 但是,当我将JTable实例更改为JTable的SignInSheetTable子类,并将构造函数更改为new SignInSheetTable() ,程序仍将执行,但不会出现JTable。 I cannot find any solution and I am completly lost as to why a subclass wont work when a super class will the entire SignInSheetTable class is shown below. 我找不到任何解决方案,并且我完全不知道为什么子类不能工作时超类将整个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);
}

}

Overriding getModel seems to be causing issues with the rest of the program. 覆盖getModel似乎会导致程序其余部分出现问题。 It's not really required for what you are trying to achieve anyway. 无论如何,这并不是你想要实现的目标所必需的。

Personally, I'd not bother with the custom table, you're not adding any new functionality to the class, which can't be managed directly through the model, which is where the management should be carried out anyway 就个人而言,我不打扰自定义表,你没有在类中添加任何新功能,这些功能无法直接通过模型进行管理,无论如何都应该进行管理。

I'd also be worried about the over use of static , as this is likely to cause you some issues as the program becomes bigger 我也担心过度使用static ,因为随着程序变得更大,这可能会给你带来一些问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM