-1

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?

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • 4
    [Examine how to compare string in java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – alex2410 Aug 11 '14 at 11:41
  • @alex2410 Sorry but thats is just a simple prototype application and has nothing to do with the actual implementation. I changed it now in the test program. The thing is the table still does not update! – Carol.Kar Aug 11 '14 at 11:43
  • 2
    The fact that `showTable` creates new components and return a component, and you call it in a _"void"_ way reaks of a code smell. You may want to look into that. – Paul Samsotha Aug 11 '14 at 11:44
  • @MadProgrammer Thx for your answer! I changed it now. However, this is just a simple use case and not the actual problem. My problem is that the table does not udpate, my action! – Carol.Kar Aug 11 '14 at 11:45
  • 1
    @Vivien Be careful, as you can see, we jump on that pretty heavily – MadProgrammer Aug 11 '14 at 11:47

1 Answers1

4

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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366