Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

java - Table does not update when selecting combobox

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Take a closer look at How to Use Tables for more details


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...