TableModel未在包含JTable的JScrollPane中调用

时间:2016-07-19 10:48:04

标签: java swing jtable jscrollpane abstracttablemodel

我在Java GUI中遇到了意外行为。我想创建一个包含JScrollPane的{​​{1}},然后将此JTable添加到JScrollPane

以下是代码:

Frame

以下是我的称呼方式:

public class UrlsScrollPanel extends JScrollPane {
private static final long serialVersionUID = 1L;

public UrlsScrollPanel() {

    //setup urls and collections
    ArrayList<URL> urls = URL.getAll();
    ArrayList<Collection> collections = new ArrayList<>();
    for(URL url : urls) collections.add(new Collection(url));

    //table
    String[] columns = {            
        "database",
        "status",
        "consumption",
        "last snapshot date",
        "last message",
        "details",
        "stop collect"
    };  

    AbstractTableModel dataModel = new AbstractTableModel() {
        private static final long serialVersionUID = 1L;

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            System.out.printf("row: %d, column: %d \n", rowIndex, columnIndex); //is never called.
            URL url = urls.get(rowIndex);
            Collection collection = collections.get(rowIndex); 
            ArrayList<Message> lastMessages = collection.getLastSnapshot().getMessages();
            Message lastMessage = lastMessages.get(lastMessages.size() - 1);

            if(columnIndex == 0) return url.toString();
            if(columnIndex == 1) return collection.getStatus(); 
            if(columnIndex == 2) return ConsumptionChart.getChartPanel(collection);
            if(columnIndex == 3) return collection.getLastSnapshot().getDate();
            if(columnIndex == 4) return String.format("[ %s ] %s", lastMessage.getDate(), lastMessage.getBody()); 
            return "Comming soon.";
        }

        @Override
        public int getRowCount() {
            return urls.size();
        }

        @Override
        public int getColumnCount() {
            return columns.length;
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    JTable table = new JTable(dataModel);
    add(table);
    setBackground(Color.red);
    setSize(500, 500);
}
}

结果是框架左上方的红色方块闪烁,然后立即消失。此外,似乎永远不会调用public static void main(String[] args) { JFrame frame = new JFrame(); frame.setVisible(true); frame.setResizable(true); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setExtendedState(JFrame.MAXIMIZED_BOTH ); frame.setLocationRelativeTo(null); UrlsScrollPanel panel = new UrlsScrollPanel(); frame.add(panel, BorderLayout.CENTER); SwingUtilities.updateComponentTreeUI(frame); }

非常感谢您对我所做错事的任何帮助,谢谢您的时间!

1 个答案:

答案 0 :(得分:2)

不要延伸 JScrollPane。相反,请使用table 构建 JScrollPane

frame.add(new JScrollPane(table), BorderLayout.CENTER);

该方法被描述为here;显示了一个完整的示例here;使用setViewportView()的替代方法已经过检查here

  

这两个不相似吗?

JScrollPane panel = new JScrollPane(); panel.add(table);
…
JScrollPane panel = new JScrollPane(table);

没有。如How a Scroll Pane Works所示,第一个公式将table直接添加到JScrollPane,替换占据JViewport中心位置的ScrollPaneLayout组件本来可以用来显示table。第二个公式在内部调用setViewportView(table),它告诉滚动窗格JViewport要显示的组件。