0

I'm trying to clear everything in my JPanel in an easier way than hard coding it all. I have a for loop which works great for JTextFields but nothing else. The commented out block makes everything work but I wanted to see if I could add the JPanels contained within on the fly as well.

List<Component> controls = new ArrayList<>();
controls.addAll(Arrays.asList(this.getComponents()));
/*controls.addAll(Arrays.asList(jPanel1.getComponents()));
controls.addAll(Arrays.asList(jPanel2.getComponents()));
controls.addAll(Arrays.asList(jPanel3.getComponents()));
controls.addAll(Arrays.asList(jPanel4.getComponents()));
controls.addAll(Arrays.asList(jPanel5.getComponents()));
controls.addAll(Arrays.asList(jPanel6.getComponents()));
controls.addAll(Arrays.asList(jPanel7.getComponents()));
controls.addAll(Arrays.asList(jPanel8.getComponents()));
controls.addAll(Arrays.asList(jPanel9.getComponents()));
controls.addAll(Arrays.asList(jPanel10.getComponents()));
controls.addAll(Arrays.asList(jPanel11.getComponents()));
controls.addAll(Arrays.asList(jPanel12.getComponents()));
controls.addAll(Arrays.asList(jPanel13.getComponents()));*/
for(Component control : controls) {
    if(control instanceof JTextField) {
        System.out.println(control.toString());
        JTextField ctrl = (JTextField) control;
        ctrl.setText("");
    } else if (control instanceof JList) {
        System.out.println(control.toString());
        JList list = (JList) control;
        for (int i = 0; i < list.getModel().getSize(); i++) {
            list.remove(i);
        }
    } else if (control instanceof JTable) {
        System.out.println(control.toString());
        JTable table = (JTable) control;
        for (int i=0;i < table.getRowCount();i++) {
            table.getModel().setValueAt("", i, 0);
        }
        for (int i=0;i < table.getColumnCount();i++) {
            table.getModel().setValueAt("", 0, i);
        }
    } else if (control instanceof JRadioButton) {
        System.out.println(control.toString());
        JRadioButton radio = (JRadioButton) control;
        radio.setSelected(false);
    } else if (control instanceof JPanel) {
        JPanel panel = (JPanel) control;
        controls.addAll(Arrays.asList(panel.getComponents()));
    }
}
Andrew
  • 175
  • 1
  • 2
  • 12
  • 2
    What's wrong with `panel.removeAll()?`. Or maybe you want to check out [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html), which will allow you to swap views(panels) – Paul Samsotha Jul 21 '14 at 13:49
  • Ah peeskillet! I am using CardLayout per your suggestion a few days ago. I'm still learning what all it can do. What does swap views mean exactly? – Andrew Jul 21 '14 at 13:53
  • It just means you can switch components. like a frame has a main panel, you can swap the main panel for another panel. That's all. I figured that's what you're trying to do by removing all the components from the panel. It really makes not sense to me the way you are attempting. Is this just for practice? – Paul Samsotha Jul 21 '14 at 13:59
  • We're trying to switch from a terminal text based system of database management to a GUI system and all of my experience with GUI's have been small programs. I have a jframe with a tree on the left and a jpanel(card layout) on the right and depending on what they select on the tree the jpanel switches to the correct card. well right now the panel has an inventory maintenance panel in it and if someone is editing part info and doesn't want to save their work I have a clear button which I'd like to clear all of the components so they can select a different part to edit. – Andrew Jul 21 '14 at 14:08
  • I don't fully understand the requirement, but to remove all the component from a panel, use `panel.removeAll(); panel.revalidate(); panel.repaint();` That will remove everything. Same with adding, you need to revalidate and repaint after adding new components. Keep in mind, You can use CardLayout at any level. If that inventory maintenance panel needs to be replaced with another panel something else, you can use a cardlayout for that panel. – Paul Samsotha Jul 21 '14 at 14:14

0 Answers0