选择组合框时表未更新 [英] Table does not update when selecting combobox

查看:87
本文介绍了选择组合框时表未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,当我选择组合框时JTable不会更新.了下面给出的方案应删除所有数据()时,选择时.该表不会更新.

My problem is that a JTable does not update when I select my combobox. The program I present below should delete all data (data = null;), when LA is selected. The table does not update.

 public class minimumExample extends JFrame {

    private JTabbedPane tabbedPane;
    private FilteredTabPanel filteredTabPanel;

    public void createTabBar() {

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);

        filteredTabPanel = new FilteredTabPanel();
        tabbedPane.addTab("Test", filteredTabPanel.createLayout());

        add(tabbedPane);
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }

    private void makeLayout() {

        setTitle("Test App");
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(1000, 500));
        createTabBar();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

    }

    public void start() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                makeLayout();   
            }
        });
    }

    public static void main(String[] args) throws IOException {
        minimumExample ex = new minimumExample();
        ex.start();
    }

    public class FilteredTabPanel extends JPanel {

        private JPanel selectionArea;
        private JLabel lCity;
        private JComboBox cityBox;
        private JTable filterTable;
        String[] columnNames = {"Cities"};
        String[][] data = {
                {"NY"}, {"NY"}, {"NY"}, {"NY"}, {"LA"}, {"LA"},{"Columbia"},{"DC"},{"DC"},{"DC"},{"DC"},{"DC"},{"DC"}
            };

        private JScrollPane scrollPane;

        public JPanel createLayout() {
            JPanel panel = new JPanel(new GridLayout(0, 1));
            //add panels to the layout
            panel.add(addButtons());    
            panel.add(showTable());

            repaint();
            revalidate();

            return panel;
        }

        public JPanel addButtons(){

            selectionArea = new JPanel(new FlowLayout(FlowLayout.LEFT));

            lCity = new JLabel("City");

            String[] fillings = {"NY", "LA", "Columbia", "DC"};
            cityBox = new JComboBox(fillings);

            cityBox.addActionListener(new ActionListener() {

                private String cityFilter;

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    //2. get data
                    cityFilter = cityBox.getSelectedItem().toString();

                    if(cityFilter.equals("LA")) {
                        data = null;
                    }

                    showTable();
                    repaint();
                }
            });

            selectionArea.add(lCity);
            selectionArea.add(cityBox);

            selectionArea.repaint();

            return selectionArea;
        }

        private JScrollPane showTable() {

            filterTable =new JTable(data, columnNames);
            filterTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

            scrollPane = new JScrollPane(filterTable);

            scrollPane.repaint();
            scrollPane.validate();

            return scrollPane;
        }
    }
}

如您所见,该表未更新.有什么建议我做错了吗?

As you can see the table does not update. Any recommendations what I am doing wrong?

推荐答案

不是通过调用showTable(永远不会以任何方式添加到屏幕上)来创建对象的新实例,这只会使整个混乱设置对象引用,尝试重置TableModel,例如...

Instead of creating new instance of you objects by calling showTable (which never get added to the screen in any way), which is just going to completely mess up your object references, try resetting the TableModel, for example...

if ("LA".equals(cityFilter)) {
    filterTable.setModel(new DefaultTableModel(null, columnNames));
}

仔细研究如何使用表更多详情

这篇关于选择组合框时表未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆