当使用数据设置新模型时,JTable不会更新/刷新

时间:2017-06-19 10:35:15

标签: java swing

嘿所以我的JTable有问题,当我将模型设置为包含数据的新模型时,它不会更新表。我已经检查过,所有数组中都有数据值,来自另一个类。 cData数组具有所有正确的字符串和值。但它似乎没有更新。这是代码,我评论了我认为它出错的地方:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class DetentionStats extends JPanel
{
    private JLabel title, houseTitle;
    private JPanel leftPanel, rightPanel, dormPanel, housePanel, dormButtonPanel, dormStatsPanel, ADormPanel, BDormPanel, CDormPanel, DDormPanel;
    private JButton ADorm, BDorm, CDorm, DDorm, Update;
    private JTable statsList, houseList;
    private JScrollPane statsListScroll, houseListScroll;
    private final int numberOfColumns = 4;
    private String[] statsColumnNames = {"Name", "Reason"};
    private String[] houseColumnNames = {"Name","Reason","Dorm","Completed"};
    private ArrayList<Detention> completedDetentions;
    private DefaultTableModel statsTable, updateCompleted, houseTable, aDormModel, bDormModel, cDormModel, dDormModel;

    public DetentionStats()
    {

        completedDetentions = new ArrayList<Detention>();

        title = new JLabel("Statistics");
        title.setFont(new Font("Arial", Font.BOLD, 24));
        title.setHorizontalAlignment(JLabel.CENTER);
        title.setVerticalAlignment(JLabel.CENTER);
        setLayout(new BorderLayout());

        ADorm = new JButton("A Dorm");
        DormListener aDormListener = new DormListener();
        ADorm.addActionListener(aDormListener);
        aDormModel = new DefaultTableModel(statsColumnNames, 0);
        ADormPanel = new JPanel();
        ADormPanel.add(ADorm);
        ADormPanel.setPreferredSize(new Dimension(100, 50));
        ADormPanel.setBackground(Color.lightGray);

        BDorm = new JButton("B Dorm");
        DormListener bDormListener = new DormListener();
        BDorm.addActionListener(bDormListener);
        bDormModel = new DefaultTableModel(statsColumnNames, 0);
        BDormPanel = new JPanel();
        BDormPanel.add(BDorm);
        BDormPanel.setPreferredSize(new Dimension(100, 50));
        BDormPanel.setBackground(Color.lightGray);

        CDorm = new JButton("C Dorm");
        DormListener cDormListener = new DormListener();
        CDorm.addActionListener(cDormListener);
        cDormModel = new DefaultTableModel(statsColumnNames, 0);
        CDormPanel = new JPanel();
        CDormPanel.add(CDorm);
        CDormPanel.setPreferredSize(new Dimension(100, 50));
        CDormPanel.setBackground(Color.lightGray);

        DDorm = new JButton("D Dorm");
        DormListener dDormListener = new DormListener();
        DDorm.addActionListener(dDormListener);
        dDormModel = new DefaultTableModel(statsColumnNames, 0);
        DDormPanel = new JPanel();
        DDormPanel.add(DDorm);
        DDormPanel.setPreferredSize(new Dimension(100, 50));
        DDormPanel.setBackground(Color.lightGray);

        dormButtonPanel = new JPanel();
        dormButtonPanel.setLayout(new GridLayout(2,2));
        dormButtonPanel.add(ADormPanel);
        dormButtonPanel.add(BDormPanel);
        dormButtonPanel.add(CDormPanel);
        dormButtonPanel.add(DDormPanel);
        dormButtonPanel.setBackground(Color.lightGray);
        //--------------------------------------------------------------------------------------------------------------------------
        statsTable = new DefaultTableModel(statsColumnNames, 0);
        statsList = new JTable(statsTable);
        statsListScroll = new JScrollPane(statsList);
        statsList.setPreferredScrollableViewportSize(new Dimension(470,260));

        dormStatsPanel = new JPanel();
        dormStatsPanel.add(statsListScroll);
        dormStatsPanel.setBackground(Color.lightGray);

        dormPanel = new JPanel();
        dormPanel.setLayout(new GridLayout(2,1));
        dormPanel.add(dormButtonPanel);
        dormPanel.add(dormStatsPanel);
        //---------------------------------------------------------------------------------------------------------------------------
        houseTitle = new JLabel("Completed Detentions");
        houseTitle.setFont(new Font("Arial", Font.BOLD, 14));
        houseTitle.setHorizontalAlignment(JLabel.CENTER);
        houseTitle.setVerticalAlignment(JLabel.CENTER);

        Update = new JButton("Update");
        UpdateListener uListener = new UpdateListener();
        Update.addActionListener(uListener);

        houseTable = new DefaultTableModel(houseColumnNames, 0);

        houseList = new JTable(houseTable);
        houseListScroll = new JScrollPane(houseList);
        houseList.setPreferredScrollableViewportSize(new Dimension(470,540));

        housePanel = new JPanel();
        housePanel.setLayout(new BorderLayout());
        housePanel.setBackground(Color.lightGray);
        housePanel.add(Update, BorderLayout.EAST);
        housePanel.add(houseTitle, BorderLayout.CENTER);
        housePanel.add(houseListScroll, BorderLayout.SOUTH);

        leftPanel = new JPanel();
        leftPanel.add(dormPanel);
        leftPanel.setBackground(Color.lightGray);
        rightPanel = new JPanel();
        rightPanel.add(housePanel);
        rightPanel.setBackground(Color.lightGray);

        add(title, BorderLayout.NORTH);
        add(leftPanel, BorderLayout.WEST);
        add(rightPanel, BorderLayout.EAST);

        setPreferredSize(new Dimension(1050,670));
        setBackground(Color.lightGray);
        setVisible(true);
    }

    public void completeDetention(Detention d)
    {
        completedDetentions.add(d);
        updateCompletedDetentions();
        dormSort();
    }

    private void dormSort()
    {
        Object[][] dData = new Object[completedDetentions.size()][numberOfColumns];
        for(int i = 0; i < completedDetentions.size(); i++)
        {
            Detention d = completedDetentions.get(i);

            if(d.getDorm() == Detention.Dorm.aDorm)
            {
            dData[i][0] = d.getName();

            dData[i][0] = d.getReason();

            aDormModel = new DefaultTableModel(dData, statsColumnNames);
            }
            else if(d.getDorm() == Detention.Dorm.bDorm)
            {
            dData[i][0] = d.getName();

            dData[i][0] = d.getReason();

            bDormModel = new DefaultTableModel(dData, statsColumnNames);
            }
            else if(d.getDorm() == Detention.Dorm.cDorm)
            {
            dData[i][0] = d.getName();

            dData[i][0] = d.getReason();

            cDormModel = new DefaultTableModel(dData, statsColumnNames);
            }
            else if(d.getDorm() == Detention.Dorm.dDorm)
            {
            dData[i][0] = d.getName();

            dData[i][0] = d.getReason();

            dDormModel = new DefaultTableModel(dData, statsColumnNames);
            }
        }
    }

    private void updateCompletedDetentions()
    {
        Object[][] cData = new Object[completedDetentions.size()][numberOfColumns];
        for(int i = 0; i < completedDetentions.size(); i++)
        {
            Detention d = completedDetentions.get(i);
            //-------------------------------------------
            cData[i][0] = d.getName();
            //-------------------------------------------
            cData[i][1] = d.getReason();
            //-------------------------------------------
            if(d.getDorm() == Detention.Dorm.aDorm)
            {
                cData[i][2] = "A Dorm";
            }
            else if(d.getDorm() == Detention.Dorm.bDorm)
            {
                cData[i][2] = "B Dorm";
            }
            else if(d.getDorm() == Detention.Dorm.cDorm)
            {
                cData[i][2] = "C Dorm";
            }
            else if(d.getDorm() == Detention.Dorm.dDorm)
            {
                cData[i][2] = "D Dorm";
            }
            else
            {
                cData[i][2] = "No Dorm Selected";
            }
            //-------------------------------------------

            cData[i][3] = new Boolean(true);
        }

        //this is where it is going wrong (i think)
        updateCompleted = new DefaultTableModel(cData, houseColumnNames);
        houseList.setModel(updateCompleted);
    }

    private class UpdateListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
                updateCompletedDetentions();
                dormSort();
        }
    }

    private class DormListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            Object source = event.getSource();
            if(source == ADorm)
            {
                statsList.setModel(aDormModel);
            }
            else if(source == BDorm)
            {
                statsList.setModel(bDormModel);
            }
            else if(source == CDorm)
            {
                statsList.setModel(cDormModel);
            }
            else if(source == DDorm)
            {
                statsList.setModel(dDormModel);
            }
        }

    }
}

可运行的示例:

public class Table extends JPanel
{
private DefaultTableModel updateTable;
private String[] columnNames = {"Name","Last Name","Location"};
private JTable table;

public Table()
{
    defaultTable = new JTable(columnNames, 0);
    table = new JTable(defaultTable);
}

public void update()
{
    Object[][] data = new Object[array.size()][numberOfColumns];
    for(int i = 0; i < array.size(); i++)
    {
        Detention c = detentionArray.get(i)
        data[i][0] = c.getFirstName();
        data[i][1] = c.getLastName();
        data[i][2] = c.getLocation();
    }

    updateTable = new DefaultTableModel(data, columnNames);
    table.setModel(updateTable);
}
}

1 个答案:

答案 0 :(得分:0)

您可能需要在updateCompletedDetentions()方法中将其用作最后一行。

fireTableDataChanged();