0

I have several GUI elements added to a JPanel. The JPanel is added to a JScrollPane. The JScrollPane is added to a JFrame (CENTER section of a BorderLayout).

At times I need to remove the JScrollPane and make the space available for other elements. I've provided a method for that. Want to make sure that this method disposes of all resources used by the old JScrollPane and makes them available for Garbage Collection. Please see code below. Is my clearCenter() method sufficient for this task? Is there a better way to do it?

Thanks.

import java.awt.*;
import javax.swing.*;

public class MyGui extends JFrame {
    private JScrollPane scroll;
    private JPanel panel;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;

    // Constructor
    public MyGui() {
        super("Playback");
        setSize(250, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BorderLayout layout = new BorderLayout();
        setLayout(layout);

        panel = new JPanel();
        GridLayout grid = new GridLayout(0, 1, 30, 30);
        panel.setLayout(grid);

        button1 = new JButton("Button1");
        button2 = new JButton("Button2");
        button3 = new JButton("Button3");
        button4 = new JButton("Button4");
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);

        scroll = new JScrollPane(panel,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(scroll, BorderLayout.CENTER);
        add(new JLabel("South", JLabel.CENTER),BorderLayout.SOUTH);
        add(new JLabel("North", JLabel.CENTER),BorderLayout.NORTH);
        add(new JLabel("East", JLabel.CENTER),BorderLayout.EAST);
        add(new JLabel("West", JLabel.CENTER),BorderLayout.WEST);
        setVisible(true);
    }

    public void clearCenter() {
        button1 = null;
        button2 = null;
        button3 = null;
        button4 = null;
        panel = null;
        remove(scroll);
        scroll =  null;
    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Greg Valvo
  • 1,069
  • 1
  • 9
  • 13

1 Answers1

2

At times I need to remove the JScrollPane and make the space available for other elements.

Use a CardLayout as shown in this answer. And I would not worry too much about disposing of a scroll pane, keep it in memory. When it is next needed, update the scroll pane contents then flip back to that card in the layout.

Resetting the content of the scroll-pane can be done like below. Activate the first button to see the button panel replaced by the yellow panel as the view of the scroll-pane. Note that this code is a 'ready to run' MCVE (with a main(String[]) to show it onscreen). Please post MCVE code in future.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MyGui extends JFrame {

    private JScrollPane scroll;
    private JPanel panel;
    private JPanel brightPanel;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;

    // Constructor
    public MyGui() {
        super("Playback");
        setSize(250, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BorderLayout layout = new BorderLayout();
        setLayout(layout);

        // create the panel, but don't add it yet.
        brightPanel = new JPanel();
        brightPanel.setBackground(Color.YELLOW);

        panel = new JPanel();
        GridLayout grid = new GridLayout(0, 1, 30, 30);
        panel.setLayout(grid);

        button1 = new JButton("Button1");
        button2 = new JButton("Button2");
        button3 = new JButton("Button3");
        button4 = new JButton("Button4");
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);

        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                changeViews();
            }
        };
        button1.addActionListener(listener);

        scroll = new JScrollPane(panel,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(scroll, BorderLayout.CENTER);
        add(new JLabel("South", JLabel.CENTER), BorderLayout.SOUTH);
        add(new JLabel("North", JLabel.CENTER), BorderLayout.NORTH);
        add(new JLabel("East", JLabel.CENTER), BorderLayout.EAST);
        add(new JLabel("West", JLabel.CENTER), BorderLayout.WEST);
        setVisible(true);
    }

    public void changeViews() {
        scroll.setViewportView(brightPanel);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new MyGui();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks. If not too much trouble, can you outline the steps necessary to clear and reload the JScrollPlane with new content? – Greg Valvo Feb 25 '15 at 15:14
  • Will strive to provide MCVE in the future. Didn't want to clutter up my question with extra classes or files to kick things off. I'm studying you technique of spawning new thread right from inside `main()`. I saw that you used the same technique in the `CardLayout` example code that you referenced. My only question is what does `new Runnable()` mean? Since `Runnable` is an Interface and not a Class (and I don't see an `implements` keyword), how is an actual Object created from this statement? Thanks for your indulgence. – Greg Valvo Feb 27 '15 at 02:31
  • *"Will strive to provide MCVE in the future. Didn't want to clutter up my question with extra classes or files to kick things off. I'm studying you technique of spawning new thread right from inside main()."* My example did not require extra classes or files, now, did it? – Andrew Thompson Feb 27 '15 at 03:12
  • Exactly, that why I'm I'm struggling to understand how it works, hence the remainder of my question that you didn't quote. After further googling, it appears to be an Anonymous Inner Class. Fine. It threw a curve ball to this newbie. If this isn't a site for the less experienced to learn from the more experienced, then maybe there's a more appropriate one? – Greg Valvo Feb 27 '15 at 03:28
  • *"If this isn't a site for the less experienced to learn from the more experienced.."* It sure is, but you should really try to implement suggestions yourself rather than ask someone to do it for you. But then, perhaps I should have mentioned that instead of *giving* an example. – Andrew Thompson Feb 27 '15 at 03:32